From 9cbae8c7071fc2e2529fd64f9bb29e72635c0f1f Mon Sep 17 00:00:00 2001 From: Mark McDonald Date: Sat, 1 Apr 2017 01:24:16 -0400 Subject: [PATCH] Decklist testing (#2537) --- common/decklist.cpp | 6 +- common/decklist.h | 2 +- tests/CMakeLists.txt | 3 +- tests/loading_from_clipboard/CMakeLists.txt | 39 ++++++ .../loading_from_clipboard_test.cpp | 128 ++++++++++++++++++ .../loading_from_clipboard_test.h | 0 6 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 tests/loading_from_clipboard/CMakeLists.txt create mode 100644 tests/loading_from_clipboard/loading_from_clipboard_test.cpp create mode 100644 tests/loading_from_clipboard/loading_from_clipboard_test.h diff --git a/common/decklist.cpp b/common/decklist.cpp index e23fcc3f..3c913fce 100644 --- a/common/decklist.cpp +++ b/common/decklist.cpp @@ -173,11 +173,11 @@ float InnerDecklistNode::recursivePrice(bool countTotalCards) const bool InnerDecklistNode::compare(AbstractDecklistNode *other) const { switch (sortMethod) { - case 0: + case ByNumber: return compareNumber(other); - case 1: + case ByName: return compareName(other); - case 2: + case ByPrice: return comparePrice(other); } return 0; diff --git a/common/decklist.h b/common/decklist.h index d9e0e220..5006cf25 100644 --- a/common/decklist.h +++ b/common/decklist.h @@ -13,7 +13,7 @@ #include #include -#include "pb/move_card_to_zone.pb.h" +#include class CardDatabase; class QIODevice; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5591a991..67cd7ef1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -37,4 +37,5 @@ endif() include_directories(${GTEST_INCLUDE_DIRS}) target_link_libraries(dummy_test ${GTEST_BOTH_LIBRARIES}) -add_subdirectory(carddatabase) \ No newline at end of file +add_subdirectory(carddatabase) +add_subdirectory(loading_from_clipboard) diff --git a/tests/loading_from_clipboard/CMakeLists.txt b/tests/loading_from_clipboard/CMakeLists.txt new file mode 100644 index 00000000..46aaabc6 --- /dev/null +++ b/tests/loading_from_clipboard/CMakeLists.txt @@ -0,0 +1,39 @@ +ADD_DEFINITIONS("-DCARDDB_DATADIR=\"${CMAKE_CURRENT_SOURCE_DIR}/data/\"") +add_executable(loading_from_clipboard_test + loading_from_clipboard_test.cpp + ../../common/decklist.cpp + ) + +if(NOT GTEST_FOUND) + add_dependencies(loading_from_clipboard_test gtest) +endif() + +target_link_libraries(loading_from_clipboard_test ${GTEST_BOTH_LIBRARIES}) +target_link_libraries(loading_from_clipboard_test cockatrice_common) + +add_test(NAME loading_from_clipboard_test COMMAND loading_from_clipboard_test) + +#### I feel like the rest of this file should not be necessary and +#### is (effective) cargo culting of tests/carddatabase/CMakeLists.txt. +#### Ideally we would only need to say "hey this test is against something from cockatrice_common", +#### and cockatrice_common would declare all of it's dependencies. I need to learn more about CMake. + +# qt5 stuff +include_directories(${Qt5Widgets_INCLUDE_DIRS}) +list(APPEND COCKATRICE_LIBS Widgets) + +# QtConcurrent +find_package(Qt5Concurrent) +if(Qt5Concurrent_FOUND) + include_directories(${Qt5Concurrent_INCLUDE_DIRS}) + list(APPEND ORACLE_LIBS Concurrent) +endif() + +# QtNetwork +find_package(Qt5Network) +if(Qt5Network_FOUND) + include_directories(${Qt5Network_INCLUDE_DIRS}) + list(APPEND COCKATRICE_LIBS Network) +endif() + +qt5_use_modules(loading_from_clipboard_test ${COCKATRICE_LIBS}) \ No newline at end of file diff --git a/tests/loading_from_clipboard/loading_from_clipboard_test.cpp b/tests/loading_from_clipboard/loading_from_clipboard_test.cpp new file mode 100644 index 00000000..c1f69a3a --- /dev/null +++ b/tests/loading_from_clipboard/loading_from_clipboard_test.cpp @@ -0,0 +1,128 @@ +#include "gtest/gtest.h" +#include "loading_from_clipboard_test.h" +#include +#include "../../common/decklist.h" + +DeckList *fromClipboard(QString *clipboard); +DeckList *fromClipboard(QString *clipboard) { + DeckList *deckList = new DeckList; + QTextStream *stream = new QTextStream(clipboard); + deckList->loadFromStream_Plain(*stream); + return deckList; +} + +using CardRows = QMap; + +struct DecklistBuilder { + CardRows actualMainboard; + CardRows actualSideboard; + + explicit DecklistBuilder() : actualMainboard({}), actualSideboard({}) {} + + void operator()(const InnerDecklistNode *innerDecklistNode, const DecklistCardNode *card) { + if (innerDecklistNode->getName() == "main") { + actualMainboard[card->getName()] += card->getNumber(); + } else if (innerDecklistNode->getName() == "side") { + actualSideboard[card->getName()] += card->getNumber(); + } else { + FAIL(); + } + } + + CardRows mainboard() { + return actualMainboard; + } + + CardRows sideboard() { + return actualSideboard; + } +}; + +namespace { + TEST(LoadingFromClipboardTest, EmptyDeck) { + DeckList *deckList = fromClipboard(new QString("")); + ASSERT_TRUE(deckList->getCardList().isEmpty()) << "Deck should be empty"; + } + + TEST(LoadingFromClipboardTest, EmptySideboard) { + DeckList *deckList = fromClipboard(new QString("Sideboard")); + ASSERT_TRUE(deckList->getCardList().isEmpty()) << "Deck should be empty"; + } + + TEST(LoadingFromClipboardTest, QuantityPrefixed) { + QString *clipboard = new QString( + "1 Mountain\n" + "2x Island\n" + ); + DeckList *deckList = fromClipboard(clipboard); + + DecklistBuilder decklistBuilder = DecklistBuilder(); + deckList->forEachCard(decklistBuilder); + + CardRows expectedMainboard = CardRows({{"Mountain", 1}, + {"Island", 2}}); + CardRows expectedSideboard = CardRows({}); + + ASSERT_EQ(expectedMainboard, decklistBuilder.mainboard()); + ASSERT_EQ(expectedSideboard, decklistBuilder.sideboard()); + } + + TEST(LoadingFromClipboardTest, CommentsAreIgnored) { + QString *clipboard = new QString( + "//1 Mountain\n" + "//2x Island\n" + "//SB:2x Island\n" + ); + + DeckList *deckList = fromClipboard(clipboard); + + DecklistBuilder decklistBuilder = DecklistBuilder(); + deckList->forEachCard(decklistBuilder); + + CardRows expectedMainboard = CardRows({}); + CardRows expectedSideboard = CardRows({}); + + ASSERT_EQ(expectedMainboard, decklistBuilder.mainboard()); + ASSERT_EQ(expectedSideboard, decklistBuilder.sideboard()); + } + + TEST(LoadingFromClipboardTest, SideboardPrefix) { + QString *clipboard = new QString( + "1 Mountain\n" + "SB: 1 Mountain\n" + "SB: 2x Island\n" + ); + DeckList *deckList = fromClipboard(clipboard); + + DecklistBuilder decklistBuilder = DecklistBuilder(); + deckList->forEachCard(decklistBuilder); + + CardRows expectedMainboard = CardRows({{"Mountain", 1}}); + CardRows expectedSideboard = CardRows({{"Mountain", 1}, + {"Island", 2}}); + + ASSERT_EQ(expectedMainboard, decklistBuilder.mainboard()); + ASSERT_EQ(expectedSideboard, decklistBuilder.sideboard()); + } + + TEST(LoadingFromClipboardTest, UnknownCardsAreNotDiscarded) { + QString *clipboard = new QString( + "1 CardThatDoesNotExistInCardsXml\n" + ); + DeckList *deckList = fromClipboard(clipboard); + + DecklistBuilder decklistBuilder = DecklistBuilder(); + deckList->forEachCard(decklistBuilder); + + CardRows expectedMainboard = CardRows({{"CardThatDoesNotExistInCardsXml", 1}}); + CardRows expectedSideboard = CardRows({}); + + ASSERT_EQ(expectedMainboard, decklistBuilder.mainboard()); + ASSERT_EQ(expectedSideboard, decklistBuilder.sideboard()); + } +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/tests/loading_from_clipboard/loading_from_clipboard_test.h b/tests/loading_from_clipboard/loading_from_clipboard_test.h new file mode 100644 index 00000000..e69de29b