aboutsummaryrefslogtreecommitdiff
path: root/paludis/virtual_constructor_TEST.cc
blob: 86c116b4f4da16fa748381d7f5f13e6b85aacb48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

#include "virtual_constructor.hh"
#include "counted_ptr.hh"
#include <test/test_runner.hh>
#include <test/test_framework.hh>

using namespace test;
using namespace paludis;

#ifndef DOXYGEN
enum CookieSize
{
    cs_small,
    cs_large
};

class Cookie :
    public InternalCounted<Cookie>
{
    private:
        CookieSize _size;

    protected:
        Cookie(CookieSize size) :
            _size(size)
        {
        };

    public:
        virtual std::string flavour() const = 0;

        virtual ~Cookie()
        {
        }

        CookieSize size() const
        {
            return _size;
        }
};

struct NoCookie
{
    NoCookie(const std::string &)
    {
    }
};

typedef VirtualConstructor<std::string,
        CountedPtr<Cookie> (*) (CookieSize),
        virtual_constructor_not_found::ThrowException<NoCookie> > CookieMaker;

class ChocolateChipCookie : public Cookie
{
    public:
        ChocolateChipCookie(CookieSize size) :
            Cookie(size)
        {
        }

        std::string flavour() const
        {
            return "Chocolate Chip";
        }

        static CountedPtr<Cookie> make(CookieSize size)
        {
            return CountedPtr<Cookie>(new ChocolateChipCookie(size));
        }
};

CookieMaker::RegisterMaker register_chocolate_chip("chocolate chip", &ChocolateChipCookie::make);

class GingerCookie : public Cookie
{
    private:
        bool _with_crunchy_bits;

    public:
        GingerCookie(CookieSize size, bool with_crunchy_bits) :
            Cookie(size),
            _with_crunchy_bits(with_crunchy_bits)
        {
        }

        std::string flavour() const
        {
            return _with_crunchy_bits ? "Crunchy Ginger" : "Ginger";
        }

        bool with_crunchy_bits() const
        {
            return _with_crunchy_bits;
        }

        static CountedPtr<Cookie> make(CookieSize size)
        {
            return CountedPtr<Cookie>(new GingerCookie(size, false));
        }

        static CountedPtr<Cookie> make_crunchy(CookieSize size)
        {
            return CountedPtr<Cookie>(new GingerCookie(size, true));
        }
};

CookieMaker::RegisterMaker register_ginger("ginger", &GingerCookie::make);
CookieMaker::RegisterMaker register_crunchy_ginger("crunchy ginger", &GingerCookie::make_crunchy);

#endif

namespace test_cases
{
    /**
     * \test Test VirtualConstructor.
     *
     * \ingroup Test
     */
    struct VirtualConstructorTest : TestCase
    {
        VirtualConstructorTest() : TestCase("virtual constructor") { }

        void run()
        {
            TEST_CHECK_EQUAL(CookieMaker::get_instance()->find_maker(
                        "chocolate chip")(cs_large)->flavour(), "Chocolate Chip");
            TEST_CHECK_EQUAL(CookieMaker::get_instance()->find_maker(
                        "chocolate chip")(cs_large)->size(), cs_large);
            TEST_CHECK_EQUAL(CookieMaker::get_instance()->find_maker(
                        "chocolate chip")(cs_small)->size(), cs_small);

            TEST_CHECK_EQUAL((*CookieMaker::get_instance())["ginger"](cs_small)->flavour(),
                    "Ginger");
            TEST_CHECK_EQUAL((*CookieMaker::get_instance())["crunchy ginger"](cs_small)->flavour(),
                    "Crunchy Ginger");

            TEST_CHECK_THROWS(CookieMaker::get_instance()->find_maker(
                        "gerbil")(cs_large)->flavour(), NoCookie);
        }
    } test_virtual_constructor;
}