+#include <set>
#include <msp/datafile/collection.h>
#include <msp/datafile/objectloader.h>
#include <msp/io/memory.h>
private:
void load();
+ void contains();
+ void contains_future();
+ void names();
+ void names_future();
void fetch();
void nonexistent();
void type_mismatch();
void name_collision();
};
+
class Base
{
public:
};
class Foo: public Base
-{ };
+{
+private:
+ static unsigned create_count;
+
+public:
+ Foo();
+
+ static unsigned get_create_count() { return create_count; }
+};
class Bar: public Base
{ };
class Sub: public Bar
{ };
+
class TestCollection: public DataFile::Collection
{
public:
Foo *create_foo(const string &);
};
+
CollectionTests::CollectionTests()
{
add(&CollectionTests::load, "Load objects");
+ add(&CollectionTests::contains, "Containment test");
+ add(&CollectionTests::contains_future, "Future containment test");
+ add(&CollectionTests::names, "List object names");
+ add(&CollectionTests::names_future, "List future object names");
add(&CollectionTests::fetch, "Fetch objects");
add(&CollectionTests::nonexistent, "Nonexistent object").expect_throw<key_error>();
add(&CollectionTests::type_mismatch, "Type mismatch").expect_throw<Msp::type_mismatch>();
loader.load(parser);
}
+void CollectionTests::contains()
+{
+ const TestCollection *ccoll = collection;
+ EXPECT(ccoll->contains<Foo>("a"));
+ EXPECT(ccoll->contains<Bar>("c"));
+ EXPECT(!ccoll->contains<Foo>("c"));
+}
+
+void CollectionTests::contains_future()
+{
+ const TestCollection *ccoll = collection;
+ EXPECT(!ccoll->contains<Foo>("f"));
+ EXPECT(collection->contains<Foo>("f"));
+}
+
+void CollectionTests::names()
+{
+ const TestCollection *ccoll = collection;
+ list<string> nm = ccoll->get_names<Foo>();
+ EXPECT_EQUAL(nm.size(), 2);
+ set<string> nm_set(nm.begin(), nm.end());
+ EXPECT_EQUAL(nm_set.count("a"), 1);
+ EXPECT_EQUAL(nm_set.count("b"), 1);
+}
+
+void CollectionTests::names_future()
+{
+ list<string> nm = collection->get_names<Foo>();
+ EXPECT_EQUAL(nm.size(), 3);
+ set<string> nm_set(nm.begin(), nm.end());
+ EXPECT_EQUAL(nm_set.count("f"), 1);
+}
+
void CollectionTests::fetch()
{
const TestCollection *ccoll = collection;
void CollectionTests::create()
{
+ unsigned foo_count = Foo::get_create_count();
Foo &f = collection->get<Foo>("f");
Foo &f2 = collection->get<Foo>("f");
EXPECT_EQUAL(&f2, &f);
+ EXPECT(Foo::get_create_count()>foo_count);
}
void CollectionTests::name_collision()
a.release();
}
+
Base::Base():
tag(0)
{ }
+
Base::Loader::Loader(Base &b):
DataFile::ObjectLoader<Base>(b)
{
add("tag", &Base::tag);
}
+
+unsigned Foo::create_count = 0;
+
+Foo::Foo()
+{
+ ++create_count;
+}
+
+
TestCollection::TestCollection()
{
add_type<Foo>().keyword("foo").creator(&TestCollection::create_foo);
add_type<Bar>().keyword("bar");
add_type<Sub>().keyword("sub").store_as<Bar>();
+ add_future<Foo>("f");
}
Foo *TestCollection::create_foo(const string &)