X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=tests%2Fcollection.cpp;fp=tests%2Fcollection.cpp;h=69aaf428c21a2c11d3fa4fce971eefacd95d7331;hb=3461239f03fd281092e81d0e264a0d5dab165415;hp=0000000000000000000000000000000000000000;hpb=9f2a99f61887a71a31afb4c56558fcc76be532d1;p=libs%2Fdatafile.git diff --git a/tests/collection.cpp b/tests/collection.cpp new file mode 100644 index 0000000..69aaf42 --- /dev/null +++ b/tests/collection.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class TestCollection; + +class CollectionTests: public Test::RegisteredTest +{ +private: + TestCollection *collection; + +public: + CollectionTests(); + ~CollectionTests(); + + static const char *get_name() { return "Collection"; } + +private: + void load(); + void fetch(); + void nonexistent(); + void type_mismatch(); + void create(); + void name_collision(); +}; + +class Base +{ +public: + class Loader: public DataFile::ObjectLoader + { + public: + Loader(Base &); + }; + +private: + int tag; + +public: + Base(); + + int get_tag() const { return tag; } +}; + +class Foo: public Base +{ }; + +class Bar: public Base +{ }; + +class Sub: public Bar +{ }; + +class TestCollection: public DataFile::Collection +{ +public: + TestCollection(); + +private: + Foo *create_foo(const string &); +}; + +CollectionTests::CollectionTests() +{ + add(&CollectionTests::load, "Load objects"); + add(&CollectionTests::fetch, "Fetch objects"); + add(&CollectionTests::nonexistent, "Nonexistent object").expect_throw(); + add(&CollectionTests::type_mismatch, "Type mismatch").expect_throw(); + add(&CollectionTests::create, "Create object"); + add(&CollectionTests::name_collision, "Name collision").expect_throw(); + + collection = new TestCollection; +} + +CollectionTests::~CollectionTests() +{ + delete collection; +} + +void CollectionTests::load() +{ + const char input[] = + "foo \"a\" { tag 1; };\n" + "foo \"b\" { tag 2; };\n" + "bar \"c\" { tag 3; };\n" + "sub \"d\" { tag 4; };\n"; + + IO::Memory mem(input, sizeof(input)-1); + DataFile::Parser parser(mem, "-"); + TestCollection::Loader loader(*collection); + loader.load(parser); +} + +void CollectionTests::fetch() +{ + const TestCollection *ccoll = collection; + EXPECT_EQUAL(ccoll->get("a").get_tag(), 1); + EXPECT_EQUAL(ccoll->get("b").get_tag(), 2); + EXPECT_EQUAL(ccoll->get("c").get_tag(), 3); + EXPECT_EQUAL(ccoll->get("d").get_tag(), 4); +} + +void CollectionTests::nonexistent() +{ + collection->get("z"); +} + +void CollectionTests::type_mismatch() +{ + collection->get("c"); +} + +void CollectionTests::create() +{ + Foo &f = collection->get("f"); + Foo &f2 = collection->get("f"); + EXPECT_EQUAL(&f2, &f); +} + +void CollectionTests::name_collision() +{ + RefPtr a = new Foo; + collection->add("a", a.get()); + a.release(); +} + +Base::Base(): + tag(0) +{ } + +Base::Loader::Loader(Base &b): + DataFile::ObjectLoader(b) +{ + add("tag", &Base::tag); +} + +TestCollection::TestCollection() +{ + add_type().keyword("foo").create(&TestCollection::create_foo); + add_type().keyword("bar"); + add_type().keyword("sub").store_as(); +} + +Foo *TestCollection::create_foo(const string &) +{ + return new Foo; +}