From b2f0c0b59dd7603a4ac29a1690237be51e1afb80 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 16 Oct 2015 21:31:30 +0100 Subject: Improve coverage and factory correctness testing --- libadhocutil/unittests/testFactory.cpp | 43 +++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/libadhocutil/unittests/testFactory.cpp b/libadhocutil/unittests/testFactory.cpp index da8ab6f..dbca28b 100644 --- a/libadhocutil/unittests/testFactory.cpp +++ b/libadhocutil/unittests/testFactory.cpp @@ -10,29 +10,42 @@ using namespace AdHoc; class BaseThing { public: - BaseThing(int, const std::string &){} + BaseThing(int, std::string * n) : + name(n) + { + } + virtual ~BaseThing() = default; virtual void execute() const = 0; + + protected: + std::string * name; }; class ImplOfThing : public BaseThing { public: - ImplOfThing(int i, const std::string & s) : BaseThing(i, s) {} - void execute() const { } + ImplOfThing(int i, std::string * s) : BaseThing(i, s) {} + void execute() const + { + *name = typeid(this).name(); + } }; class OtherImplOfThing : public BaseThing { public: - OtherImplOfThing(int i, const std::string & s) : BaseThing(i, s) {} - void execute() const { } + OtherImplOfThing(int i, std::string * s) : BaseThing(i, s) {} + void execute() const + { + *name = typeid(this).name(); + } }; -typedef AdHoc::Factory BaseThingFactory; +typedef AdHoc::Factory BaseThingFactory; NAMEDFACTORY("a", ImplOfThing, BaseThingFactory); FACTORY(OtherImplOfThing, BaseThingFactory); -INSTANTIATEFACTORY(BaseThing, int, std::string); +INSTANTIATEFACTORY(BaseThing, int, std::string *); // Multiple factories in one compilation unit INSTANTIATEFACTORY(BaseThing, std::string, std::string); // Factories of things with commas @@ -59,12 +72,19 @@ BOOST_AUTO_TEST_CASE( create ) { auto factory1 = BaseThingFactory::get("a"); auto factory2 = BaseThingFactory::get("OtherImplOfThing"); - auto i1 = factory1->create(1, "std"); - auto i2 = factory1->create(1, "std"); - auto i3 = factory2->create(1, "std"); + std::string n; + auto i1 = factory1->create(1, &n); + auto i2 = factory1->create(1, &n); + auto i3 = factory2->create(1, &n); BOOST_REQUIRE(i1); + i1->execute(); + BOOST_REQUIRE_EQUAL(n, "PK11ImplOfThing"); BOOST_REQUIRE(i2); + i2->execute(); + BOOST_REQUIRE_EQUAL(n, "PK11ImplOfThing"); BOOST_REQUIRE(i3); + i3->execute(); + BOOST_REQUIRE_EQUAL(n, "PK16OtherImplOfThing"); BOOST_REQUIRE(i1 != i2); BOOST_REQUIRE(i1 != i3); BOOST_REQUIRE(i2 != i3); @@ -72,7 +92,8 @@ BOOST_AUTO_TEST_CASE( create ) BOOST_AUTO_TEST_CASE( createNew ) { - auto i = BaseThingFactory::createNew("a", 1, "std"); + std::string n; + auto i = BaseThingFactory::createNew("a", 1, &n); BOOST_REQUIRE(i); delete i; } -- cgit v1.2.3