summaryrefslogtreecommitdiff
path: root/libadhocutil/unittests/testFactory.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-09-14 20:31:30 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2015-09-14 20:31:30 +0100
commit13ef5b22effca60ac26bce71d4c67011d4c533aa (patch)
tree8820f1c1b2a02cb757bac5e854d914a5c9f2772c /libadhocutil/unittests/testFactory.cpp
parentCreate a unique namespace for plugin registry functions (diff)
downloadlibadhocutil-13ef5b22effca60ac26bce71d4c67011d4c533aa.tar.bz2
libadhocutil-13ef5b22effca60ac26bce71d4c67011d4c533aa.tar.xz
libadhocutil-13ef5b22effca60ac26bce71d4c67011d4c533aa.zip
Add basic factory support
Diffstat (limited to 'libadhocutil/unittests/testFactory.cpp')
-rw-r--r--libadhocutil/unittests/testFactory.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/libadhocutil/unittests/testFactory.cpp b/libadhocutil/unittests/testFactory.cpp
new file mode 100644
index 0000000..9b463f1
--- /dev/null
+++ b/libadhocutil/unittests/testFactory.cpp
@@ -0,0 +1,65 @@
+#define BOOST_TEST_MODULE Factory
+#include <boost/test/unit_test.hpp>
+
+#include "factory.h"
+#include "factory.impl.h"
+#include "plugins.h"
+#include "plugins.impl.h"
+
+using namespace AdHoc;
+
+class BaseThing {
+ public:
+ BaseThing(int, const std::string &){}
+
+ virtual void execute() const = 0;
+};
+
+class ImplOfThing : public BaseThing {
+ public:
+ ImplOfThing(int i, const std::string & s) : BaseThing(i, s) {}
+ void execute() const { }
+};
+class OtherImplOfThing : public BaseThing {
+ public:
+ OtherImplOfThing(int i, const std::string & s) : BaseThing(i, s) {}
+ void execute() const { }
+};
+
+typedef AdHoc::Factory<BaseThing, int, std::string> BaseThingFactory;
+
+NAMEDFACTORY("a", ImplOfThing, BaseThingFactory);
+FACTORY(OtherImplOfThing, BaseThingFactory);
+
+BOOST_AUTO_TEST_CASE( ready )
+{
+ BOOST_REQUIRE_EQUAL(2, PluginManager::getDefault()->count());
+}
+
+BOOST_AUTO_TEST_CASE( get )
+{
+ auto all = PluginManager::getDefault()->getAll();
+ auto factory1 = PluginManager::getDefault()->get<BaseThingFactory>("a")->implementation();
+ auto factory2 = BaseThingFactory::get("a");
+ auto factory3 = BaseThingFactory::get("OtherImplOfThing");
+ BOOST_REQUIRE(factory1);
+ BOOST_REQUIRE_EQUAL(factory1, factory2);
+ BOOST_REQUIRE(factory3);
+ BOOST_REQUIRE(factory1 != factory3);
+}
+
+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");
+ BOOST_REQUIRE(i1);
+ BOOST_REQUIRE(i2);
+ BOOST_REQUIRE(i3);
+ BOOST_REQUIRE(i1 != i2);
+ BOOST_REQUIRE(i1 != i3);
+ BOOST_REQUIRE(i2 != i3);
+}
+