2 #include <msp/datafile/collection.h>
3 #include <msp/datafile/objectloader.h>
4 #include <msp/io/memory.h>
5 #include <msp/test/test.h>
12 class CollectionTests: public Test::RegisteredTest<CollectionTests>
15 TestCollection *collection;
21 static const char *get_name() { return "Collection"; }
26 void contains_future();
33 void name_collision();
40 class Loader: public DataFile::ObjectLoader<Base>
52 int get_tag() const { return tag; }
55 class Foo: public Base
58 static unsigned create_count;
63 static unsigned get_create_count() { return create_count; }
66 class Bar: public Base
73 class TestCollection: public DataFile::Collection
79 Foo *create_foo(const string &);
83 CollectionTests::CollectionTests()
85 add(&CollectionTests::load, "Load objects");
86 add(&CollectionTests::contains, "Containment test");
87 add(&CollectionTests::contains_future, "Future containment test");
88 add(&CollectionTests::names, "List object names");
89 add(&CollectionTests::names_future, "List future object names");
90 add(&CollectionTests::fetch, "Fetch objects");
91 add(&CollectionTests::nonexistent, "Nonexistent object").expect_throw<key_error>();
92 add(&CollectionTests::type_mismatch, "Type mismatch").expect_throw<Msp::type_mismatch>();
93 add(&CollectionTests::create, "Create object");
94 add(&CollectionTests::name_collision, "Name collision").expect_throw<Msp::key_error>();
96 collection = new TestCollection;
99 CollectionTests::~CollectionTests()
104 void CollectionTests::load()
107 "foo \"a\" { tag 1; };\n"
108 "foo \"b\" { tag 2; };\n"
109 "bar \"c\" { tag 3; };\n"
110 "sub \"d\" { tag 4; };\n";
112 IO::Memory mem(input, sizeof(input)-1);
113 DataFile::Parser parser(mem, "-");
114 TestCollection::Loader loader(*collection);
118 void CollectionTests::contains()
120 const TestCollection *ccoll = collection;
121 EXPECT(ccoll->contains<Foo>("a"));
122 EXPECT(ccoll->contains<Bar>("c"));
123 EXPECT(!ccoll->contains<Foo>("c"));
126 void CollectionTests::contains_future()
128 const TestCollection *ccoll = collection;
129 EXPECT(!ccoll->contains<Foo>("f"));
130 EXPECT(collection->contains<Foo>("f"));
133 void CollectionTests::names()
135 const TestCollection *ccoll = collection;
136 list<string> nm = ccoll->get_names<Foo>();
137 EXPECT_EQUAL(nm.size(), 2);
138 set<string> nm_set(nm.begin(), nm.end());
139 EXPECT_EQUAL(nm_set.count("a"), 1);
140 EXPECT_EQUAL(nm_set.count("b"), 1);
143 void CollectionTests::names_future()
145 list<string> nm = collection->get_names<Foo>();
146 EXPECT_EQUAL(nm.size(), 3);
147 set<string> nm_set(nm.begin(), nm.end());
148 EXPECT_EQUAL(nm_set.count("f"), 1);
151 void CollectionTests::fetch()
153 const TestCollection *ccoll = collection;
154 EXPECT_EQUAL(ccoll->get<Foo>("a").get_tag(), 1);
155 EXPECT_EQUAL(ccoll->get<Foo>("b").get_tag(), 2);
156 EXPECT_EQUAL(ccoll->get<Bar>("c").get_tag(), 3);
157 EXPECT_EQUAL(ccoll->get<Bar>("d").get_tag(), 4);
160 void CollectionTests::nonexistent()
162 collection->get<Bar>("z");
165 void CollectionTests::type_mismatch()
167 collection->get<Foo>("c");
170 void CollectionTests::create()
172 unsigned foo_count = Foo::get_create_count();
173 Foo &f = collection->get<Foo>("f");
174 Foo &f2 = collection->get<Foo>("f");
175 EXPECT_EQUAL(&f2, &f);
176 EXPECT(Foo::get_create_count()>foo_count);
179 void CollectionTests::name_collision()
181 RefPtr<Foo> a = new Foo;
182 collection->add("a", a.get());
192 Base::Loader::Loader(Base &b):
193 DataFile::ObjectLoader<Base>(b)
195 add("tag", &Base::tag);
199 unsigned Foo::create_count = 0;
207 TestCollection::TestCollection()
209 add_type<Foo>().keyword("foo").creator(&TestCollection::create_foo);
210 add_type<Bar>().keyword("bar");
211 add_type<Sub>().keyword("sub").store_as<Bar>();
212 add_future<Foo>("f");
215 Foo *TestCollection::create_foo(const string &)