From: Mikko Rasa Date: Wed, 26 Sep 2012 20:29:57 +0000 (+0300) Subject: More unit tests for Collection, including contains and future objects X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=92bcf7a5e69a7c8e6055a77a39f356c293b89445;p=libs%2Fdatafile.git More unit tests for Collection, including contains and future objects --- diff --git a/tests/collection.cpp b/tests/collection.cpp index bb72c03..bbcc6c9 100644 --- a/tests/collection.cpp +++ b/tests/collection.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -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(); add(&CollectionTests::type_mismatch, "Type mismatch").expect_throw(); @@ -95,6 +115,39 @@ void CollectionTests::load() loader.load(parser); } +void CollectionTests::contains() +{ + const TestCollection *ccoll = collection; + EXPECT(ccoll->contains("a")); + EXPECT(ccoll->contains("c")); + EXPECT(!ccoll->contains("c")); +} + +void CollectionTests::contains_future() +{ + const TestCollection *ccoll = collection; + EXPECT(!ccoll->contains("f")); + EXPECT(collection->contains("f")); +} + +void CollectionTests::names() +{ + const TestCollection *ccoll = collection; + list nm = ccoll->get_names(); + EXPECT_EQUAL(nm.size(), 2); + set 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 nm = collection->get_names(); + EXPECT_EQUAL(nm.size(), 3); + set 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("f"); Foo &f2 = collection->get("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(b) { add("tag", &Base::tag); } + +unsigned Foo::create_count = 0; + +Foo::Foo() +{ + ++create_count; +} + + TestCollection::TestCollection() { add_type().keyword("foo").creator(&TestCollection::create_foo); add_type().keyword("bar"); add_type().keyword("sub").store_as(); + add_future("f"); } Foo *TestCollection::create_foo(const string &)