1 |
tim |
150 |
#include "utils/GenericFactoryTestCase.hpp" |
2 |
|
|
#include <iostream> |
3 |
|
|
#include <algorithm> |
4 |
|
|
// Registers the fixture into the 'registry' |
5 |
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( GenericFactoryTestCase ); |
6 |
|
|
void GenericFactoryTestCase::testGenericFactory() { |
7 |
|
|
//test getIdents |
8 |
|
|
std::vector<std::string> idents; |
9 |
|
|
idents = ShapeFactory::getInstance()->getIdents(); |
10 |
|
|
CPPUNIT_ASSERT(std::find(idents.begin(), idents.end(), "MyLine") != idents.end()); |
11 |
|
|
CPPUNIT_ASSERT(std::find(idents.begin(), idents.end(), "MyCircle") != idents.end()); |
12 |
|
|
|
13 |
|
|
//test createObject |
14 |
|
|
Shape* line = ShapeFactory::getInstance()->createObject("MyLine"); |
15 |
|
|
CPPUNIT_ASSERT(line != NULL && line->getType() == "Line"); |
16 |
|
|
delete line; |
17 |
|
|
|
18 |
|
|
Shape* circle = ShapeFactory::getInstance()->createObject("MyCircle"); |
19 |
|
|
CPPUNIT_ASSERT(circle != NULL && circle->getType() == "Circle"); |
20 |
|
|
delete circle; |
21 |
|
|
|
22 |
|
|
//test registerCreator |
23 |
|
|
bool registeredCreateAnotherCircle = |
24 |
|
|
ShapeFactory::getInstance()->registerCreator("MyCircle", createAnotherCircle); |
25 |
|
|
CPPUNIT_ASSERT(!registeredCreateAnotherCircle); |
26 |
|
|
|
27 |
|
|
//test unregisterCreator |
28 |
|
|
ShapeFactory::getInstance()->unregisterCreator("MyCircle"); |
29 |
|
|
idents = ShapeFactory::getInstance()->getIdents(); |
30 |
|
|
CPPUNIT_ASSERT(idents.size() == 1 && std::find(idents.begin(), idents.end(), "MyLine") != idents.end()); |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
//test registerCreator |
34 |
|
|
registeredCreateAnotherCircle = |
35 |
|
|
ShapeFactory::getInstance()->registerCreator("MyCircle", createAnotherCircle); |
36 |
|
|
CPPUNIT_ASSERT(registeredCreateAnotherCircle); |
37 |
|
|
idents = ShapeFactory::getInstance()->getIdents(); |
38 |
|
|
CPPUNIT_ASSERT(std::find(idents.begin(), idents.end(), "MyLine") != idents.end()); |
39 |
|
|
CPPUNIT_ASSERT(std::find(idents.begin(), idents.end(), "MyCircle") != idents.end()); |
40 |
|
|
|
41 |
|
|
//expect createAnotherCircle will replace createCircle |
42 |
|
|
Shape* anotherCircle = ShapeFactory::getInstance()->createObject("MyCircle"); |
43 |
|
|
CPPUNIT_ASSERT(anotherCircle != NULL && anotherCircle->getType() == "AnotherCircle"); |
44 |
|
|
delete anotherCircle; |
45 |
|
|
|
46 |
|
|
//test macro REGISTER_CREATOR |
47 |
|
|
REGISTER_CREATOR(ShapeFactory, "MyCubic", Cubic); |
48 |
|
|
Shape* cubic = ShapeFactory::getInstance()->createObject("MyCubic"); |
49 |
|
|
CPPUNIT_ASSERT(circle != NULL && circle->getType() == "Cubic"); |
50 |
|
|
} |