factory_tests.cpp 1.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "cellulut_tests.hpp"

#include "factory.hpp"

class Transition
{
public:
    virtual std::string say_hi() = 0;
};

class LifeLikeTransition : public Transition
{
public:

    virtual std::string say_hi()
    { return "Life hi!\n"; }
};
18
REGISTER_FACTORY_ENTRY(Transition, LifeLikeTransition, "life-like");
19
20
21
22
23
24
25
26

class GenerationsTransition : public Transition
{
public:

    virtual std::string say_hi()
    { return "Generations hi!\n"; }
};
27
REGISTER_FACTORY_ENTRY(Transition, GenerationsTransition, "generations");
28
29
30
31


void CellulutTests::test_factory()
{
32
33
34
35
    QCOMPARE(Factory<Transition>::list_choices().size(), 2ull);
    const auto choices = Factory<Transition>::list_choices();
    QCOMPARE(std::count(choices.begin(), choices.end(), "life-like"), 1);
    QCOMPARE(std::count(choices.begin(), choices.end(), "generations"), 1);
36
37
38
39


    std::unique_ptr<Transition> life_trans = Factory<Transition>::make("life-like");
    QVERIFY(life_trans != nullptr);
40
    QCOMPARE(life_trans->say_hi(), std::string("Life hi!\n"));
41
42
43

    std::unique_ptr<Transition> gens_trans = Factory<Transition>::make("generations");
    QVERIFY(gens_trans != nullptr);
44
    QCOMPARE(gens_trans->say_hi(), std::string("Generations hi!\n"));
45
46
47
48


    QVERIFY(nullptr == Factory<Transition>::make("<invalid>"));
}