Decklist testing (#2537)
This commit is contained in:
parent
ef7670a1e6
commit
9cbae8c707
6 changed files with 173 additions and 5 deletions
|
@ -173,11 +173,11 @@ float InnerDecklistNode::recursivePrice(bool countTotalCards) const
|
||||||
bool InnerDecklistNode::compare(AbstractDecklistNode *other) const
|
bool InnerDecklistNode::compare(AbstractDecklistNode *other) const
|
||||||
{
|
{
|
||||||
switch (sortMethod) {
|
switch (sortMethod) {
|
||||||
case 0:
|
case ByNumber:
|
||||||
return compareNumber(other);
|
return compareNumber(other);
|
||||||
case 1:
|
case ByName:
|
||||||
return compareName(other);
|
return compareName(other);
|
||||||
case 2:
|
case ByPrice:
|
||||||
return comparePrice(other);
|
return comparePrice(other);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
#include <QtCore/QXmlStreamReader>
|
#include <QtCore/QXmlStreamReader>
|
||||||
#include <QtCore/QXmlStreamWriter>
|
#include <QtCore/QXmlStreamWriter>
|
||||||
|
|
||||||
#include "pb/move_card_to_zone.pb.h"
|
#include <common/pb/move_card_to_zone.pb.h>
|
||||||
|
|
||||||
class CardDatabase;
|
class CardDatabase;
|
||||||
class QIODevice;
|
class QIODevice;
|
||||||
|
|
|
@ -38,3 +38,4 @@ include_directories(${GTEST_INCLUDE_DIRS})
|
||||||
target_link_libraries(dummy_test ${GTEST_BOTH_LIBRARIES})
|
target_link_libraries(dummy_test ${GTEST_BOTH_LIBRARIES})
|
||||||
|
|
||||||
add_subdirectory(carddatabase)
|
add_subdirectory(carddatabase)
|
||||||
|
add_subdirectory(loading_from_clipboard)
|
||||||
|
|
39
tests/loading_from_clipboard/CMakeLists.txt
Normal file
39
tests/loading_from_clipboard/CMakeLists.txt
Normal file
|
@ -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})
|
128
tests/loading_from_clipboard/loading_from_clipboard_test.cpp
Normal file
128
tests/loading_from_clipboard/loading_from_clipboard_test.cpp
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "loading_from_clipboard_test.h"
|
||||||
|
#include <QTextStream>
|
||||||
|
#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<QString, int>;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
Loading…
Reference in a new issue