]> git.tdb.fi Git - libs/datafile.git/commitdiff
More unit tests for Collection, including contains and future objects
authorMikko Rasa <tdb@tdb.fi>
Wed, 26 Sep 2012 20:29:57 +0000 (23:29 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 26 Sep 2012 20:29:57 +0000 (23:29 +0300)
tests/collection.cpp

index bb72c03fbba536f3cce42cf6cfdea6e52c9f45ba..bbcc6c9fce28bab325341149a1123be8acf09f03 100644 (file)
@@ -1,3 +1,4 @@
+#include <set>
 #include <msp/datafile/collection.h>
 #include <msp/datafile/objectloader.h>
 #include <msp/io/memory.h>
@@ -21,6 +22,10 @@ public:
 
 private:
        void load();
+       void contains();
+       void contains_future();
+       void names();
+       void names_future();
        void fetch();
        void nonexistent();
        void type_mismatch();
@@ -28,6 +33,7 @@ private:
        void name_collision();
 };
 
+
 class Base
 {
 public:
@@ -47,7 +53,15 @@ public:
 };
 
 class Foo: public Base
-{ };
+{
+private:
+       static unsigned create_count;
+
+public:
+       Foo();
+
+       static unsigned get_create_count() { return create_count; }
+};
 
 class Bar: public Base
 { };
@@ -55,6 +69,7 @@ class Bar: public Base
 class Sub: public Bar
 { };
 
+
 class TestCollection: public DataFile::Collection
 {
 public:
@@ -64,9 +79,14 @@ private:
        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>();
@@ -95,6 +115,39 @@ void CollectionTests::load()
        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;
@@ -116,9 +169,11 @@ void CollectionTests::type_mismatch()
 
 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()
@@ -128,21 +183,33 @@ 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 &)