]> git.tdb.fi Git - libs/datafile.git/blob - tests/collection.cpp
Rename CollectionItemType::create method to creator
[libs/datafile.git] / tests / collection.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/datafile/objectloader.h>
3 #include <msp/io/memory.h>
4 #include <msp/test/test.h>
5
6 using namespace std;
7 using namespace Msp;
8
9 class TestCollection;
10
11 class CollectionTests: public Test::RegisteredTest<CollectionTests>
12 {
13 private:
14         TestCollection *collection;
15
16 public:
17         CollectionTests();
18         ~CollectionTests();
19
20         static const char *get_name() { return "Collection"; }
21
22 private:
23         void load();
24         void fetch();
25         void nonexistent();
26         void type_mismatch();
27         void create();
28         void name_collision();
29 };
30
31 class Base
32 {
33 public:
34         class Loader: public DataFile::ObjectLoader<Base>
35         {
36         public:
37                 Loader(Base &);
38         };
39
40 private:
41         int tag;
42
43 public:
44         Base();
45
46         int get_tag() const { return tag; }
47 };
48
49 class Foo: public Base
50 { };
51
52 class Bar: public Base
53 { };
54
55 class Sub: public Bar
56 { };
57
58 class TestCollection: public DataFile::Collection
59 {
60 public:
61         TestCollection();
62
63 private:
64         Foo *create_foo(const string &);
65 };
66
67 CollectionTests::CollectionTests()
68 {
69         add(&CollectionTests::load, "Load objects");
70         add(&CollectionTests::fetch, "Fetch objects");
71         add(&CollectionTests::nonexistent, "Nonexistent object").expect_throw<key_error>();
72         add(&CollectionTests::type_mismatch, "Type mismatch").expect_throw<Msp::type_mismatch>();
73         add(&CollectionTests::create, "Create object");
74         add(&CollectionTests::name_collision, "Name collision").expect_throw<Msp::key_error>();
75
76         collection = new TestCollection;
77 }
78
79 CollectionTests::~CollectionTests()
80 {
81         delete collection;
82 }
83
84 void CollectionTests::load()
85 {
86         const char input[] =
87                 "foo \"a\" { tag 1; };\n"
88                 "foo \"b\" { tag 2; };\n"
89                 "bar \"c\" { tag 3; };\n"
90                 "sub \"d\" { tag 4; };\n";
91
92         IO::Memory mem(input, sizeof(input)-1);
93         DataFile::Parser parser(mem, "-");
94         TestCollection::Loader loader(*collection);
95         loader.load(parser);
96 }
97
98 void CollectionTests::fetch()
99 {
100         const TestCollection *ccoll = collection;
101         EXPECT_EQUAL(ccoll->get<Foo>("a").get_tag(), 1);
102         EXPECT_EQUAL(ccoll->get<Foo>("b").get_tag(), 2);
103         EXPECT_EQUAL(ccoll->get<Bar>("c").get_tag(), 3);
104         EXPECT_EQUAL(ccoll->get<Bar>("d").get_tag(), 4);
105 }
106
107 void CollectionTests::nonexistent()
108 {
109         collection->get<Bar>("z");
110 }
111
112 void CollectionTests::type_mismatch()
113 {
114         collection->get<Foo>("c");
115 }
116
117 void CollectionTests::create()
118 {
119         Foo &f = collection->get<Foo>("f");
120         Foo &f2 = collection->get<Foo>("f");
121         EXPECT_EQUAL(&f2, &f);
122 }
123
124 void CollectionTests::name_collision()
125 {
126         RefPtr<Foo> a = new Foo;
127         collection->add("a", a.get());
128         a.release();
129 }
130
131 Base::Base():
132         tag(0)
133 { }
134
135 Base::Loader::Loader(Base &b):
136         DataFile::ObjectLoader<Base>(b)
137 {
138         add("tag", &Base::tag);
139 }
140
141 TestCollection::TestCollection()
142 {
143         add_type<Foo>().keyword("foo").creator(&TestCollection::create_foo);
144         add_type<Bar>().keyword("bar");
145         add_type<Sub>().keyword("sub").store_as<Bar>();
146 }
147
148 Foo *TestCollection::create_foo(const string &)
149 {
150         return new Foo;
151 }