diff options
author | Josh Blum | 2013-07-07 11:50:03 -0700 |
---|---|---|
committer | Josh Blum | 2013-07-07 11:50:03 -0700 |
commit | c6373f53f4690d667e553ed7258e13c3b45aa323 (patch) | |
tree | 202bf0d31140c5567cad54dea697ad1dcbba6432 /tests | |
parent | e2a237e121edfb53378ae57d8faa71491d0f29f1 (diff) | |
download | sandhi-c6373f53f4690d667e553ed7258e13c3b45aa323.tar.gz sandhi-c6373f53f4690d667e553ed7258e13c3b45aa323.tar.bz2 sandhi-c6373f53f4690d667e553ed7258e13c3b45aa323.zip |
gras: created element factory
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/factory_test.cpp | 77 |
2 files changed, 78 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bb3f3c8..62cf7d8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,7 @@ set(test_sources callable_test.cpp chrono_time_test.cpp block_props_test.cpp + factory_test.cpp serialize_tags_test.cpp ) diff --git a/tests/factory_test.cpp b/tests/factory_test.cpp new file mode 100644 index 0000000..00239f9 --- /dev/null +++ b/tests/factory_test.cpp @@ -0,0 +1,77 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#include <boost/test/unit_test.hpp> +#include <iostream> + +#include <gras/block.hpp> + +struct MyBlock : gras::Block +{ + MyBlock(const long &a0 = 0, const std::string &a1 = "", const bool &a2 = false): + gras::Block("MyBlock"), a0(a0), a1(a1), a2(a2) + { + } + + const long a0; + const std::string a1; + const bool a2; + + //dummy work + void work(const InputItems &, const OutputItems &){} +}; + +// a few test factory functions + +gras::Block *make_my_block_args0(void) +{ + return new MyBlock(); +} + +gras::Block *make_my_block_args1(const long &a0) +{ + return new MyBlock(a0); +} + +gras::Block *make_my_block_args2(const long &a0, const std::string &a1) +{ + return new MyBlock(a0, a1); +} + +gras::Block *make_my_block_args3(const long &a0, const std::string &a1, const bool &a2) +{ + return new MyBlock(a0, a1, a2); +} + +#include <gras/factory.hpp> + +GRAS_REGISTER_FACTORY(make_my_block_args0) +GRAS_REGISTER_FACTORY(make_my_block_args1) +GRAS_REGISTER_FACTORY(make_my_block_args2) +GRAS_REGISTER_FACTORY(make_my_block_args3) + +BOOST_AUTO_TEST_CASE(test_register_and_make) +{ + gras::Element *my_block0 = gras::Factory::make("make_my_block_args0"); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block0) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a0, 0); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a1, ""); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a2, false); + + gras::Element *my_block1 = gras::Factory::make("make_my_block_args1", 42); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block1) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a0, 42); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a1, ""); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a2, false); + + gras::Element *my_block2 = gras::Factory::make("make_my_block_args2", 1, "foo"); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block2) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a0, 1); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a1, "foo"); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a2, false); + + gras::Element *my_block3 = gras::Factory::make("make_my_block_args3", -2, "bar", true); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block3) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a0, -2); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a1, "bar"); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a2, true); +} |