2 //! @brief File containing the Test Class and the Test Methods for the MapSaveBuilder
6 #include <cppunit/TestCase.h>
7 #include <cppunit/TestFixture.h>
8 #include <cppunit/ui/text/TextTestRunner.h>
9 #include <cppunit/extensions/HelperMacros.h>
10 #include <cppunit/extensions/TestFactoryRegistry.h>
11 #include <cppunit/TestResult.h>
12 #include <cppunit/TestResultCollector.h>
13 #include <cppunit/TestRunner.h>
14 #include <cppunit/BriefTestProgressListener.h>
15 #include <cppunit/CompilerOutputter.h>
16 #include <cppunit/XmlOutputter.h>
17 #include <cppunit/ui/text/TestRunner.h>
18 #include "MapSaveBuilder.h"
19 #include "MapDirector.h"
20 using namespace CppUnit
;
24 //! Test Class for the MapSaveBuilder class
25 //! It must be a subclass of CppUnit::TestFixture
26 //! It then uses CPPUNIT_TEST_SUITE() to create the test suite, and CPPUNIT_TEST() to include the test methods in the test suite.
27 //! CPPUNIT_TEST_SUITE_REGISTRATION() is then used to register the test class in the test registry.
29 //! Test Class for the LevelMapBuilder class contains main test method
30 class TestMapSaveBuilder
: public CppUnit::TestFixture
32 CPPUNIT_TEST_SUITE(TestMapSaveBuilder
);
33 CPPUNIT_TEST(testValidMonsters
);
34 CPPUNIT_TEST(testValidTreasures
);
35 CPPUNIT_TEST_SUITE_END();
37 void testValidMonsters(void);
38 void testValidTreasures(void);
41 //!Register for running the test
42 CPPUNIT_TEST_SUITE_REGISTRATION(TestMapSaveBuilder
);//most important
44 //! Tests the Builder pattern creation of a map from a saved file
45 //! This method tests whether or not the correct amount of monsters are created
46 //! A monster is denoted by an 'm' on the map. There are three 'm's so we expect 3 monsters to be created.
47 void TestMapSaveBuilder::testValidMonsters(void)
49 MapDirector mapDirector
;
50 MapSaveBuilder
* mapSaveBuilder
= new MapSaveBuilder();
51 mapDirector
.setMapBuilder(mapSaveBuilder
);
52 mapDirector
.constructMap("map.txt");
53 Map
* map
= mapSaveBuilder
->getMap();
54 vector
<Monster
>* monsters
= mapSaveBuilder
->getMonsters();
55 // There are 3 'm's in the example.txt so we expect two monsters to be made
56 CPPUNIT_ASSERT(monsters
->size() == 3);
59 //! Tests the Builder pattern creation of a map from a saved file
60 //! Treasure is a container for items
61 //! This method tests whether or not the correct amount of treasures are created
62 //! A treasure is denoted by an 't' on the map. There are two 't's so we expect 2 treasures to be created.
63 void TestMapSaveBuilder::testValidTreasures(void)
65 MapDirector mapDirector
;
66 MapSaveBuilder
* mapSaveBuilder
= new MapSaveBuilder();
67 mapDirector
.setMapBuilder(mapSaveBuilder
);
68 mapDirector
.constructMap("map.txt");
69 Map
* map
= mapSaveBuilder
->getMap();
70 vector
<vector
<Item
>>* treasureList
= mapSaveBuilder
->getTreasure();
71 // There are 2 't's in the example.txt so we expect two treasures to be made
72 CPPUNIT_ASSERT(treasureList
->size() == 2);
75 // Uncomment to run tests
77 int main(int ac, char **av)
79 //--- Create the event manager and test controller
80 CPPUNIT_NS::TestResult controller;
82 //--- Add a listener that colllects test result
83 CPPUNIT_NS::TestResultCollector result;
84 controller.addListener(&result);
86 //--- Add a listener that print dots as test run.
87 CPPUNIT_NS::BriefTestProgressListener progress;
88 controller.addListener(&progress);
90 //--- Add the top suite to the test runner
91 CPPUNIT_NS::TestRunner runner;
93 runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
94 runner.run(controller);
99 return result.wasSuccessful() ? 0 : 1;