mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-11 20:43:25 -08:00
Merge upstream master and move the reveal docs into docs/
Upstream moved the documentation sources from doc/ to docs/ (#7584), so the page this branch adds now belongs under docs/doxygen/pages/user_documentation/ gameplay/. The index.md edit auto-merged onto the moved file, but the new page stayed at the old path, and GitHub's merge engine reported a conflict in index.md because the file it edits was deleted upstream. No source changes: the merge is upstream/master as-is plus the doc relocation.
This commit is contained in:
commit
b64e168026
179 changed files with 4162 additions and 832 deletions
|
|
@ -21,10 +21,12 @@ cockatrice_add_test(
|
|||
)
|
||||
|
||||
# ------------------------
|
||||
# Filter String Test
|
||||
# (guard must match the condition for libcockatrice_filters in the root CMakeLists.txt)
|
||||
# Guard to match condition for libcockatrice_filters in the root CMakeLists.txt
|
||||
# ------------------------
|
||||
if(WITH_ORACLE OR WITH_CLIENT)
|
||||
# ------------------------
|
||||
# Filter String Test
|
||||
# ------------------------
|
||||
cockatrice_add_test(
|
||||
NAME
|
||||
filter_string_test
|
||||
|
|
@ -36,6 +38,34 @@ if(WITH_ORACLE OR WITH_CLIENT)
|
|||
libcockatrice_filters
|
||||
)
|
||||
|
||||
# ------------------------
|
||||
# Filter Tree Test
|
||||
# ------------------------
|
||||
cockatrice_add_test(
|
||||
NAME
|
||||
filter_tree_test
|
||||
SOURCES
|
||||
${VERSION_STRING_CPP}
|
||||
filter_tree_test.cpp
|
||||
mocks.cpp
|
||||
LIBS
|
||||
libcockatrice_filters
|
||||
)
|
||||
|
||||
# ------------------------
|
||||
# Filter Mode Test
|
||||
# ------------------------
|
||||
cockatrice_add_test(
|
||||
NAME
|
||||
filter_mode_test
|
||||
SOURCES
|
||||
${VERSION_STRING_CPP}
|
||||
filter_mode_test.cpp
|
||||
mocks.cpp
|
||||
LIBS
|
||||
libcockatrice_filters
|
||||
)
|
||||
|
||||
# ------------------------
|
||||
# Visual Display Model Test
|
||||
# ------------------------
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include "../support/scoped_file_size_limit.h"
|
||||
#include "libcockatrice/card/database/card_database.h"
|
||||
#include "libcockatrice/card/database/card_database_loader.h"
|
||||
#include "libcockatrice/card/database/card_database_querier.h"
|
||||
|
|
@ -36,12 +37,6 @@
|
|||
#include <libcockatrice/utility/card_ref.h>
|
||||
#include <string>
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
#include <csignal>
|
||||
#include <sys/resource.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
|
|
@ -247,21 +242,14 @@ TEST(CardDatabaseTest, Xml4ShortWriteKeepsInstalledFile)
|
|||
ASSERT_EQ(installed.write("installed database"), 18);
|
||||
installed.close();
|
||||
|
||||
// A real filesystem write failure, isolated in the death-test child so the
|
||||
// process-wide file limit never affects other cases or the parent runner.
|
||||
ASSERT_EXIT(
|
||||
{
|
||||
std::signal(SIGXFSZ, SIG_IGN);
|
||||
struct rlimit limit;
|
||||
limit.rlim_cur = 128;
|
||||
limit.rlim_max = 128;
|
||||
if (setrlimit(RLIMIT_FSIZE, &limit) != 0) {
|
||||
_exit(2);
|
||||
}
|
||||
CockatriceXml4Parser writer(&controller);
|
||||
_exit(writer.saveToFile({}, sets, cards, fileName) ? 1 : 0);
|
||||
},
|
||||
::testing::ExitedWithCode(0), "");
|
||||
// A real filesystem write failure: the soft file-size cap makes the writer's own
|
||||
// flush fail without filling a disk. The cap is scoped rather than forked, because a
|
||||
// death-test child under RLIMIT_FSIZE is killed by SIGSEGV on macOS.
|
||||
test_support::ScopedFileSizeLimit fileSizeCap(128);
|
||||
ASSERT_TRUE(fileSizeCap.isValid());
|
||||
|
||||
CockatriceXml4Parser writer(&controller);
|
||||
EXPECT_FALSE(writer.saveToFile({}, sets, cards, fileName));
|
||||
|
||||
ASSERT_TRUE(installed.open(QIODevice::ReadOnly));
|
||||
EXPECT_EQ(installed.readAll(), "installed database");
|
||||
|
|
|
|||
147
tests/carddatabase/filter_mode_test.cpp
Normal file
147
tests/carddatabase/filter_mode_test.cpp
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
#include "libcockatrice/filters/filter_mode.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class FilterModeTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
~FilterModeTest() override
|
||||
{
|
||||
for (CardFilter *filter : owned) {
|
||||
delete filter;
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Create a filter owned by the fixture and append it to the set under test. */
|
||||
const CardFilter *
|
||||
add(const QString &term, CardFilter::Type type, CardFilter::Attr attr = CardFilter::Attr::AttrColor)
|
||||
{
|
||||
owned.append(new CardFilter(term, type, attr));
|
||||
filters.append(owned.last());
|
||||
return owned.last();
|
||||
}
|
||||
|
||||
QList<CardFilter *> owned;
|
||||
QList<const CardFilter *> filters;
|
||||
};
|
||||
|
||||
TEST(FilterModeInfoTest, EachModeDescribesItsOwnFilters)
|
||||
{
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::Includes).includeType, CardFilter::Type::TypeOr);
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::Includes).excludeType, CardFilter::Type::TypeEnd);
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::Includes).attr, CardFilter::Attr::AttrColor);
|
||||
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::Excludes).includeType, CardFilter::Type::TypeAndNot);
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::Excludes).excludeType, CardFilter::Type::TypeEnd);
|
||||
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::IncludeExclude).includeType, CardFilter::Type::TypeOr);
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::IncludeExclude).excludeType, CardFilter::Type::TypeAndNot);
|
||||
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::ExactMatch).includeType, CardFilter::Type::TypeAnd);
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::ExactMatch).excludeType, CardFilter::Type::TypeAndNot);
|
||||
|
||||
// Color identity only excludes: the selected colors must be the identity, so nothing is included positively.
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::ColorIdentity).includeType, CardFilter::Type::TypeEnd);
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::ColorIdentity).excludeType, CardFilter::Type::TypeAndNot);
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::ColorIdentity).attr, CardFilter::Attr::AttrColorIdentity);
|
||||
|
||||
EXPECT_EQ(getFilterModeInfo(FilterMode::Custom).attr, CardFilter::Attr::AttrEnd);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, EmptyFilterSetKeepsACompatibleMode)
|
||||
{
|
||||
// Includes and Color identity both allow an empty set, so the live mode is not swapped for another.
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Includes), FilterMode::Includes);
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::ColorIdentity), FilterMode::ColorIdentity);
|
||||
}
|
||||
|
||||
// Include / Exclude and Exact match always hold one filter per color, so an empty set cannot have come from
|
||||
// them and falls back to the first mode that can describe it.
|
||||
TEST_F(FilterModeTest, EmptyFilterSetFallsBackWhenTheModeNeedsOneFilterPerColor)
|
||||
{
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::IncludeExclude), FilterMode::Includes);
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::ExactMatch), FilterMode::Includes);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, IncludesFromOrFilters)
|
||||
{
|
||||
add("W", CardFilter::Type::TypeOr);
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Includes), FilterMode::Includes);
|
||||
// A single OR filter cannot come from Excludes, so the first mode that can produce it is selected.
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Excludes), FilterMode::Includes);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, ExcludesFromAndNotFilters)
|
||||
{
|
||||
add("W", CardFilter::Type::TypeAndNot);
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Excludes), FilterMode::Excludes);
|
||||
// Includes needs an OR or an end filter, so an AND NOT filter selects Excludes.
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Includes), FilterMode::Excludes);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, IncludeExcludeNeedsOneFilterPerColor)
|
||||
{
|
||||
add("W", CardFilter::Type::TypeOr);
|
||||
for (const QChar color : QString("UBRG")) {
|
||||
add(QString(color), CardFilter::Type::TypeAndNot);
|
||||
}
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::IncludeExclude), FilterMode::IncludeExclude);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, IncludeExcludeRejectsAnIncompleteColorSet)
|
||||
{
|
||||
add("W", CardFilter::Type::TypeOr);
|
||||
add("U", CardFilter::Type::TypeAndNot);
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::IncludeExclude), FilterMode::Custom);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, ExactMatchFromAndAndAndNotFilters)
|
||||
{
|
||||
add("W", CardFilter::Type::TypeAnd);
|
||||
for (const QChar color : QString("UBRG")) {
|
||||
add(QString(color), CardFilter::Type::TypeAndNot);
|
||||
}
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::ExactMatch), FilterMode::ExactMatch);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, ColorIdentityFromIdentityAndNotFilters)
|
||||
{
|
||||
for (const QChar color : QString("BRG")) {
|
||||
add(QString(color), CardFilter::Type::TypeAndNot, CardFilter::Attr::AttrColorIdentity);
|
||||
}
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::ColorIdentity), FilterMode::ColorIdentity);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, MixedAttributesAreCustom)
|
||||
{
|
||||
add("W", CardFilter::Type::TypeOr, CardFilter::Attr::AttrColor);
|
||||
add("U", CardFilter::Type::TypeAndNot, CardFilter::Attr::AttrColorIdentity);
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Includes), FilterMode::Custom);
|
||||
}
|
||||
|
||||
TEST_F(FilterModeTest, CustomLeavesAsSoonAsAModeCanExplainTheFilters)
|
||||
{
|
||||
add("W", CardFilter::Type::TypeOr);
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Custom), FilterMode::Includes);
|
||||
}
|
||||
|
||||
// Exact match with no color selected stores one AND NOT filter per color. That is a legitimate Exact match
|
||||
// state, so the live mode is kept; a mode that cannot produce the set re-derives to Excludes, which produces
|
||||
// the identical filters.
|
||||
TEST_F(FilterModeTest, FullyExcludedColorSetIsExactMatchOrExcludes)
|
||||
{
|
||||
for (const QChar color : QString("WUBRG")) {
|
||||
add(QString(color), CardFilter::Type::TypeAndNot);
|
||||
}
|
||||
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::ExactMatch), FilterMode::ExactMatch);
|
||||
EXPECT_EQ(deriveFilterMode(filters, FilterMode::Includes), FilterMode::Excludes);
|
||||
}
|
||||
|
|
@ -123,6 +123,25 @@ CardInfoPtr greenIdentityCard()
|
|||
return CardInfo::newInstance("Identity Cat", "", false, {{"colors", "G"}, {"coloridentity", "G"}}, {}, {}, {}, {});
|
||||
}
|
||||
|
||||
// A card whose color identity differs from its colors, so that a query reading the wrong one is visible.
|
||||
CardInfoPtr splitIdentityCard()
|
||||
{
|
||||
return CardInfo::newInstance("Split Identity", "", false, {{"colors", "G"}, {"coloridentity", "W"}}, {}, {}, {},
|
||||
{});
|
||||
}
|
||||
|
||||
// "ci!" (exactly these color identities) used to read the camelCase "colorIdentity" property instead of the
|
||||
// lowercase "coloridentity" key, so it matched the empty string and rejected every card.
|
||||
TEST_F(CardQuery, ColorIdentityExactReadsTheColorIdentityProperty)
|
||||
{
|
||||
const CardData card = splitIdentityCard();
|
||||
ASSERT_TRUE(FilterString("ci!w").check(card));
|
||||
ASSERT_FALSE(FilterString("ci!g").check(card));
|
||||
// "c!" still reads colors, not identity.
|
||||
ASSERT_TRUE(FilterString("c!g").check(card));
|
||||
ASSERT_FALSE(FilterString("c!w").check(card));
|
||||
}
|
||||
|
||||
// Whitespace belongs around the separator, never inside an operator token: "c i: g" must not
|
||||
// be read as the color-identity operator "ci" (it is a generic "c" AND a field query on "i").
|
||||
TEST_F(CardQuery, ColorIdentityOperatorIsNotSplitByWhitespace)
|
||||
|
|
@ -260,6 +279,22 @@ TEST_F(SetQuery, ComparisonMatchesByReleaseDate)
|
|||
EXPECT_FALSE(FilterString("set<EOE").check(inBothSets));
|
||||
}
|
||||
|
||||
TEST(FilterStringError, InvalidExpressionIsInvalidAndReportsAPosition)
|
||||
{
|
||||
const FilterString filter("t:(");
|
||||
|
||||
EXPECT_FALSE(filter.valid());
|
||||
EXPECT_TRUE(filter.error().contains("position")) << "unexpected error text: " << filter.error().toStdString();
|
||||
}
|
||||
|
||||
TEST(FilterStringError, ValidExpressionHasNoError)
|
||||
{
|
||||
const FilterString filter("t:creature");
|
||||
|
||||
EXPECT_TRUE(filter.valid());
|
||||
EXPECT_TRUE(filter.error().isEmpty());
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
|
|
|
|||
204
tests/carddatabase/filter_tree_test.cpp
Normal file
204
tests/carddatabase/filter_tree_test.cpp
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
|
||||
#include "libcockatrice/filters/filter_tree.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <libcockatrice/card/card_localization.h>
|
||||
#include <libcockatrice/filters/filter_string.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
class FilterTreeTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
language = CardSearchLanguage{};
|
||||
|
||||
island = CardInfo::newInstance("Island", "", false, {{"colors", ""}, {"coloridentity", "U"}}, {}, {}, {}, {});
|
||||
artifact =
|
||||
CardInfo::newInstance("Artifact", "", false, {{"colors", ""}, {"coloridentity", ""}}, {}, {}, {}, {});
|
||||
truth = CardInfo::newInstance("Truth", "", false, {{"colors", "U"}, {"coloridentity", "U"}}, {}, {}, {}, {});
|
||||
notDead =
|
||||
CardInfo::newInstance("Not Dead", "", false, {{"colors", "B"}, {"coloridentity", "B"}}, {}, {}, {}, {});
|
||||
wallOfDenial = CardInfo::newInstance("Wall of Denial", "", false, {{"colors", "WU"}, {"coloridentity", "WU"}},
|
||||
{}, {}, {}, {});
|
||||
maelstromAngel = CardInfo::newInstance("Maelstrom Archangel", "", false,
|
||||
{{"colors", "WUBRG"}, {"coloridentity", "WUBRG"}}, {}, {}, {}, {});
|
||||
}
|
||||
|
||||
CardSearchLanguage language;
|
||||
|
||||
CardData island; // c/u
|
||||
CardData artifact; // c
|
||||
CardData truth; // u
|
||||
CardData notDead; // b
|
||||
CardData wallOfDenial; // wu
|
||||
CardData maelstromAngel; // wubrg
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// =====================================================================================================================
|
||||
// acceptColor
|
||||
// =====================================================================================================================
|
||||
|
||||
TEST_F(FilterTreeTest, Color)
|
||||
{
|
||||
FilterTree filterTree;
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "wu");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(island, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(artifact, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
}
|
||||
|
||||
TEST_F(FilterTreeTest, ColorColorless)
|
||||
{
|
||||
FilterTree filterTree;
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "c");
|
||||
ASSERT_TRUE(filterTree.acceptsCard(island, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
}
|
||||
|
||||
TEST_F(FilterTreeTest, ColorNot)
|
||||
{
|
||||
FilterTree filterTree;
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAndNot, "b");
|
||||
ASSERT_TRUE(filterTree.acceptsCard(island, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
}
|
||||
|
||||
TEST_F(FilterTreeTest, ColorAliases)
|
||||
{
|
||||
FilterTree filterTree;
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "green");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "grn");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "blue");
|
||||
ASSERT_TRUE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "blu");
|
||||
ASSERT_TRUE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "black");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(notDead, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "blk");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(notDead, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "red");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "white");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "wht");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "colorless");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "colourless");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
filterTree.clear();
|
||||
|
||||
filterTree.termNode(CardFilter::Attr::AttrColor, CardFilter::Type::TypeAnd, "none");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
filterTree.clear();
|
||||
}
|
||||
|
||||
// =====================================================================================================================
|
||||
// acceptColorIdentity
|
||||
// =====================================================================================================================
|
||||
|
||||
TEST_F(FilterTreeTest, ColorIdentity)
|
||||
{
|
||||
FilterTree filterTree;
|
||||
filterTree.termNode(CardFilter::Attr::AttrColorIdentity, CardFilter::Type::TypeAnd, "wu");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(island, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(artifact, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
}
|
||||
|
||||
TEST_F(FilterTreeTest, ColorIdentityColorless)
|
||||
{
|
||||
FilterTree filterTree;
|
||||
filterTree.termNode(CardFilter::Attr::AttrColorIdentity, CardFilter::Type::TypeAnd, "c");
|
||||
ASSERT_FALSE(filterTree.acceptsCard(island, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
}
|
||||
|
||||
TEST_F(FilterTreeTest, ColorIdentityNot)
|
||||
{
|
||||
FilterTree filterTree;
|
||||
filterTree.termNode(CardFilter::Attr::AttrColorIdentity, CardFilter::Type::TypeAndNot, "b");
|
||||
ASSERT_TRUE(filterTree.acceptsCard(island, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
}
|
||||
|
||||
TEST_F(FilterTreeTest, ColorIdentityMultiple)
|
||||
{
|
||||
// Simulate a WU commander.
|
||||
FilterTree filterTree;
|
||||
filterTree.termNode(CardFilter::Attr::AttrColorIdentity, CardFilter::Type::TypeAndNot, "b");
|
||||
filterTree.termNode(CardFilter::Attr::AttrColorIdentity, CardFilter::Type::TypeAndNot, "r");
|
||||
filterTree.termNode(CardFilter::Attr::AttrColorIdentity, CardFilter::Type::TypeAndNot, "g");
|
||||
ASSERT_TRUE(filterTree.acceptsCard(island, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(artifact, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(truth, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(notDead, language));
|
||||
ASSERT_TRUE(filterTree.acceptsCard(wallOfDenial, language));
|
||||
ASSERT_FALSE(filterTree.acceptsCard(maelstromAngel, language));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@
|
|||
#include <libcockatrice/game/zones/hand_zone_logic.h>
|
||||
#include <libcockatrice/game/zones/view_zone_logic.h>
|
||||
#include <libcockatrice/utility/card_ref.h>
|
||||
#include <libcockatrice/utility/zone_names.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
|
@ -105,3 +106,30 @@ TEST(CardZoneLogicTest, ReorganizeSignalFiresOnAdd)
|
|||
|
||||
EXPECT_EQ(reorganizeSpy.count(), 1);
|
||||
}
|
||||
|
||||
TEST(CardZoneLogicTest, LibraryEdgeViewIsLimitedToOneEndOfTheLibrary)
|
||||
{
|
||||
HandZoneLogic deck(nullptr, QString::fromLatin1(ZoneNames::DECK), false, true, true);
|
||||
HandZoneLogic grave(nullptr, QString::fromLatin1(ZoneNames::GRAVE), false, true, true);
|
||||
|
||||
ZoneViewZoneLogic fullDeckView(nullptr, &deck, -1, false, false, false);
|
||||
EXPECT_FALSE(fullDeckView.isLibraryEdgeView());
|
||||
EXPECT_FALSE(fullDeckView.showsTopOfLibrary());
|
||||
EXPECT_FALSE(fullDeckView.showsBottomOfLibrary());
|
||||
|
||||
ZoneViewZoneLogic emptyDeckView(nullptr, &deck, 0, false, false, false);
|
||||
EXPECT_FALSE(emptyDeckView.isLibraryEdgeView());
|
||||
|
||||
ZoneViewZoneLogic topView(nullptr, &deck, 3, false, false, false);
|
||||
EXPECT_TRUE(topView.isLibraryEdgeView());
|
||||
EXPECT_TRUE(topView.showsTopOfLibrary());
|
||||
EXPECT_FALSE(topView.showsBottomOfLibrary());
|
||||
|
||||
ZoneViewZoneLogic bottomView(nullptr, &deck, 3, false, false, true);
|
||||
EXPECT_TRUE(bottomView.isLibraryEdgeView());
|
||||
EXPECT_FALSE(bottomView.showsTopOfLibrary());
|
||||
EXPECT_TRUE(bottomView.showsBottomOfLibrary());
|
||||
|
||||
ZoneViewZoneLogic graveView(nullptr, &grave, 3, false, false, false);
|
||||
EXPECT_FALSE(graveView.isLibraryEdgeView());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,12 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <initializer_list>
|
||||
#include <libcockatrice/network/client/abstract/latency_tracker.h>
|
||||
#include <libcockatrice/rng/rng_abstract.h>
|
||||
#include <string>
|
||||
|
||||
// Referenced by other translation units in the server_remote library.
|
||||
RNG_Abstract *rng = nullptr;
|
||||
|
||||
TEST(LatencyTrackerTest, EmptyTrackerYieldsZeroedStats)
|
||||
{
|
||||
LatencyTracker tracker;
|
||||
|
|
|
|||
|
|
@ -23,10 +23,14 @@
|
|||
#include <libcockatrice/card/printing/exact_card.h>
|
||||
#include <libcockatrice/card/set/card_set.h>
|
||||
#include <libcockatrice/interfaces/noop_card_set_priority_controller.h>
|
||||
#include <libcockatrice/rng/rng_abstract.h>
|
||||
#include <libcockatrice/settings/paths_settings.h>
|
||||
#include <qnamespace.h>
|
||||
#include <string>
|
||||
|
||||
// Referenced by other translation units in the server_remote library.
|
||||
RNG_Abstract *rng = nullptr;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "../../oracle/src/database_writer.h"
|
||||
#include "../../oracle/src/oracleimporter.h"
|
||||
#include "../../oracle/src/raw_json_scanner.h"
|
||||
#include "../support/scoped_file_size_limit.h"
|
||||
#include "libcockatrice/card/card_info.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
|
@ -33,12 +34,6 @@
|
|||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
#include <csignal>
|
||||
#include <sys/resource.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
class OracleImporterTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
|
|
@ -1336,20 +1331,16 @@ TEST(OracleDatabaseWriter, ShortWriteKeepsPreviouslyInstalledDatabase)
|
|||
original.close();
|
||||
const QByteArray data = "<cockatrice_carddatabase version=\"4\"><!--" + QByteArray(16384, 'x') +
|
||||
"--><cards/></cockatrice_carddatabase>";
|
||||
// A real filesystem write failure, isolated in the death-test child so the
|
||||
// process-wide file limit never affects other cases or the parent runner.
|
||||
ASSERT_EXIT(
|
||||
{
|
||||
std::signal(SIGXFSZ, SIG_IGN);
|
||||
struct rlimit limit;
|
||||
limit.rlim_cur = 128;
|
||||
limit.rlim_max = 128;
|
||||
if (setrlimit(RLIMIT_FSIZE, &limit) != 0) {
|
||||
_exit(2);
|
||||
}
|
||||
_exit(OracleDatabase::saveXml(path, data) ? 1 : 0);
|
||||
},
|
||||
::testing::ExitedWithCode(0), "");
|
||||
// A real filesystem write failure: the soft file-size cap makes the writer's own
|
||||
// flush fail without filling a disk. The cap is scoped rather than forked, because a
|
||||
// death-test child under RLIMIT_FSIZE is killed by SIGSEGV on macOS.
|
||||
test_support::ScopedFileSizeLimit fileSizeCap(128);
|
||||
ASSERT_TRUE(fileSizeCap.isValid());
|
||||
|
||||
QString error;
|
||||
EXPECT_FALSE(OracleDatabase::saveXml(path, data, &error));
|
||||
EXPECT_FALSE(error.isEmpty());
|
||||
|
||||
ASSERT_TRUE(original.open(QIODevice::ReadOnly));
|
||||
EXPECT_EQ(original.readAll(), "installed database");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,13 @@
|
|||
#include <QString>
|
||||
#include <gtest/gtest.h>
|
||||
#include <libcockatrice/network/server/remote/game/server_counter.h>
|
||||
#include <libcockatrice/rng/rng_abstract.h>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
// Referenced by other translation units in the server_remote library.
|
||||
RNG_Abstract *rng = nullptr;
|
||||
|
||||
TEST(ServerCounter, IncrementDoesNotOverflow)
|
||||
{
|
||||
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::max());
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "client/network/update/card_spoiler/spoiler_file.h"
|
||||
#include "support/scoped_file_size_limit.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
|
|
@ -6,12 +7,6 @@
|
|||
#include <QTemporaryDir>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
#include <csignal>
|
||||
#include <sys/resource.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
class SpoilerFileTest : public ::testing::Test
|
||||
|
|
@ -123,25 +118,16 @@ TEST_F(SpoilerFileTest, FileInParentPathReportsFailureWithoutModifyingIt)
|
|||
#ifdef Q_OS_UNIX
|
||||
TEST_F(SpoilerFileTest, ShortWriteLeavesExistingFileIntact)
|
||||
{
|
||||
// Limit only the child process: a real short write exercises QSaveFile's
|
||||
// buffered flush failure without depending on disk capacity or file ownership.
|
||||
const QByteArray changed = "<cockatrice_carddatabase version=\"4\"><!--" + QByteArray(100000, 'x') +
|
||||
"--><cards/></cockatrice_carddatabase>";
|
||||
EXPECT_EXIT(
|
||||
{
|
||||
struct rlimit limit;
|
||||
if (getrlimit(RLIMIT_FSIZE, &limit) != 0) {
|
||||
_exit(2);
|
||||
}
|
||||
limit.rlim_cur = 128;
|
||||
if (setrlimit(RLIMIT_FSIZE, &limit) != 0) {
|
||||
_exit(3);
|
||||
}
|
||||
std::signal(SIGXFSZ, SIG_IGN);
|
||||
const auto result = save(changed);
|
||||
_exit(result == SpoilerFile::SaveResult::Failed && !error.isEmpty() ? 0 : 1);
|
||||
},
|
||||
::testing::ExitedWithCode(0), "");
|
||||
// A real short write exercises QSaveFile's buffered flush failure without depending
|
||||
// on disk capacity. The cap is scoped rather than forked, because a death-test child
|
||||
// under RLIMIT_FSIZE is killed by SIGSEGV on macOS.
|
||||
test_support::ScopedFileSizeLimit fileSizeCap(128);
|
||||
ASSERT_TRUE(fileSizeCap.isValid());
|
||||
|
||||
EXPECT_EQ(save(changed), SpoilerFile::SaveResult::Failed);
|
||||
EXPECT_FALSE(error.isEmpty());
|
||||
EXPECT_EQ(read(), original);
|
||||
EXPECT_EQ(QDir(directory.path()).entryList(QDir::Files), QStringList{"spoiler.xml"});
|
||||
}
|
||||
|
|
|
|||
83
tests/support/scoped_file_size_limit.h
Normal file
83
tests/support/scoped_file_size_limit.h
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#ifndef COCKATRICE_TEST_SCOPED_FILE_SIZE_LIMIT_H
|
||||
#define COCKATRICE_TEST_SCOPED_FILE_SIZE_LIMIT_H
|
||||
|
||||
#include <QtGlobal>
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <sys/resource.h>
|
||||
|
||||
namespace test_support
|
||||
{
|
||||
/**
|
||||
* @brief Caps how large a single file may grow, so a writer's short-write path is reachable.
|
||||
*
|
||||
* Lowering RLIMIT_FSIZE makes the next write past the cap fail with EFBIG instead of
|
||||
* filling a filesystem, which is the only way to fail an install step mid-write without
|
||||
* depending on free disk space or file ownership. Only the soft limit is lowered, so the
|
||||
* destructor can hand the process back exactly as it found it: ctest runs one case per
|
||||
* process, but a whole test binary must not carry the cap into its neighbours.
|
||||
*
|
||||
* Do not use this inside a gtest death test. A forked child under RLIMIT_FSIZE is killed
|
||||
* by SIGSEGV on the macOS runners, while the same case passes on Linux, so the fork is
|
||||
* what breaks - not the writer. The guard alone isolates the failure just as well.
|
||||
*/
|
||||
class ScopedFileSizeLimit
|
||||
{
|
||||
public:
|
||||
explicit ScopedFileSizeLimit(rlim_t maxBytes)
|
||||
{
|
||||
if (getrlimit(RLIMIT_FSIZE, &previousLimit) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct sigaction ignore = {};
|
||||
ignore.sa_handler = SIG_IGN;
|
||||
sigemptyset(&ignore.sa_mask);
|
||||
if (sigaction(SIGXFSZ, &ignore, &previousAction) != 0) {
|
||||
return;
|
||||
}
|
||||
actionSaved = true;
|
||||
|
||||
struct rlimit capped = previousLimit;
|
||||
capped.rlim_cur = std::min(maxBytes, previousLimit.rlim_max);
|
||||
if (setrlimit(RLIMIT_FSIZE, &capped) != 0) {
|
||||
return;
|
||||
}
|
||||
active = true;
|
||||
}
|
||||
|
||||
~ScopedFileSizeLimit()
|
||||
{
|
||||
if (active) {
|
||||
setrlimit(RLIMIT_FSIZE, &previousLimit);
|
||||
}
|
||||
if (actionSaved) {
|
||||
sigaction(SIGXFSZ, &previousAction, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
ScopedFileSizeLimit(const ScopedFileSizeLimit &) = delete;
|
||||
ScopedFileSizeLimit &operator=(const ScopedFileSizeLimit &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Whether the cap is in force; a test that needs the failure must assert this.
|
||||
*/
|
||||
[[nodiscard]] bool isValid() const
|
||||
{
|
||||
return active;
|
||||
}
|
||||
|
||||
private:
|
||||
bool active = false;
|
||||
bool actionSaved = false;
|
||||
struct rlimit previousLimit = {};
|
||||
struct sigaction previousAction = {};
|
||||
};
|
||||
} // namespace test_support
|
||||
|
||||
#endif // Q_OS_UNIX
|
||||
|
||||
#endif // COCKATRICE_TEST_SCOPED_FILE_SIZE_LIMIT_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue