aboutsummaryrefslogtreecommitdiff
path: root/0.8.0/paludis/util/virtual_constructor_TEST.cc
blob: 673f0c13d00b14d0b340e68b910d2fd0f6feca69 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/util/counted_ptr.hh>
#include <paludis/util/virtual_constructor.hh>
#include <set>
#include <test/test_framework.hh>
#include <test/test_runner.hh>

/** \file
 * Test cases for VirtualConstructor.
 *
 * \ingroup grptestcases
 */

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 grptestcases
     */
    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;

    /**
     * \test Test VirtualConstructor keys
     *
     * \ingroup grptestcases
     */
    struct VirtualConstructorKeysTest : TestCase
    {
        VirtualConstructorKeysTest() : TestCase("virtual constructor keys") { }

        void run()
        {
            std::set<std::string> keys;

            TEST_CHECK(keys.empty());
            CookieMaker::get_instance()->copy_keys(std::inserter(keys, keys.begin()));
            TEST_CHECK(! keys.empty());
            TEST_CHECK(keys.end() != keys.find("crunchy ginger"));
            TEST_CHECK(keys.end() != keys.find("ginger"));
            TEST_CHECK(keys.end() != keys.find("chocolate chip"));
            TEST_CHECK(keys.end() == keys.find("gerbil"));
        }
    } test_virtual_constructor_keys;
}