summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJosh Blum2013-07-07 22:42:53 -0700
committerJosh Blum2013-07-07 22:42:53 -0700
commitdd3872720fcd35dc6e9e95eb597901902bea1ec5 (patch)
tree5cc01745d0f69d45c2777a465861a36139e1f111 /tests
parent746b27ece7ae9cd60cab5945a195751ec9f9199f (diff)
downloadsandhi-dd3872720fcd35dc6e9e95eb597901902bea1ec5.tar.gz
sandhi-dd3872720fcd35dc6e9e95eb597901902bea1ec5.tar.bz2
sandhi-dd3872720fcd35dc6e9e95eb597901902bea1ec5.zip
gras: a unit test for the factory
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt14
-rw-r--r--tests/example_module.cpp28
-rw-r--r--tests/factory_test.cpp16
-rw-r--r--tests/module_loader_test.py13
4 files changed, 63 insertions, 8 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 62cf7d8..5024954 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -50,3 +50,17 @@ GR_ADD_TEST(sbuffer_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/sbuffe
GR_ADD_TEST(query_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/query_test.py)
GR_ADD_TEST(block_props_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/block_props_test.py)
GR_ADD_TEST(time_tags_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/time_tags_test.py)
+
+########################################################################
+# Build an example loadable module
+########################################################################
+add_library(example_module MODULE example_module.cpp)
+target_link_libraries(example_module ${GRAS_LIBRARIES})
+
+set(CURRENT_LIBRARY_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
+if(WIN32)
+set(CURRENT_LIBRARY_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
+endif()
+file(TO_NATIVE_PATH "${CURRENT_LIBRARY_BUILD_DIR}" CURRENT_LIBRARY_BUILD_DIR)
+list(APPEND GR_TEST_ENVIRONS "GRAS_MODULE_PATH=${CURRENT_LIBRARY_BUILD_DIR}")
+GR_ADD_TEST(module_loader_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/module_loader_test.py)
diff --git a/tests/example_module.cpp b/tests/example_module.cpp
new file mode 100644
index 0000000..2186973
--- /dev/null
+++ b/tests/example_module.cpp
@@ -0,0 +1,28 @@
+// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+#include <gras/block.hpp>
+#include <gras/factory.hpp>
+
+struct MyBlock : gras::Block
+{
+ MyBlock(void):
+ gras::Block("MyBlock")
+ {
+ this->register_call("get_num", &MyBlock::get_num);
+ }
+
+ int get_num(void)
+ {
+ return 42;
+ }
+
+ //dummy work
+ void work(const InputItems &, const OutputItems &){}
+};
+
+gras::Block *make_my_block(void)
+{
+ return new MyBlock();
+}
+
+GRAS_REGISTER_FACTORY("/tests/my_block", make_my_block)
diff --git a/tests/factory_test.cpp b/tests/factory_test.cpp
index 00239f9..999e86e 100644
--- a/tests/factory_test.cpp
+++ b/tests/factory_test.cpp
@@ -44,32 +44,32 @@ gras::Block *make_my_block_args3(const long &a0, const std::string &a1, const bo
#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)
+GRAS_REGISTER_FACTORY("/tests/my_block_args0", make_my_block_args0)
+GRAS_REGISTER_FACTORY("/tests/my_block_args1", make_my_block_args1)
+GRAS_REGISTER_FACTORY("/tests/my_block_args2", make_my_block_args2)
+GRAS_REGISTER_FACTORY("/tests/my_block_args3", make_my_block_args3)
BOOST_AUTO_TEST_CASE(test_register_and_make)
{
- gras::Element *my_block0 = gras::Factory::make("make_my_block_args0");
+ gras::Element *my_block0 = gras::Factory::make("/tests/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);
+ gras::Element *my_block1 = gras::Factory::make("/tests/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");
+ gras::Element *my_block2 = gras::Factory::make("/tests/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);
+ gras::Element *my_block3 = gras::Factory::make("/tests/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");
diff --git a/tests/module_loader_test.py b/tests/module_loader_test.py
new file mode 100644
index 0000000..8b7a0c9
--- /dev/null
+++ b/tests/module_loader_test.py
@@ -0,0 +1,13 @@
+# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+import unittest
+import gras
+
+class ModuleLoaderTest(unittest.TestCase):
+
+ def test_load_module(self):
+ my_block = gras.Factory.make("/tests/my_block")
+ self.assertEqual(my_block.get_num(), 42)
+
+if __name__ == '__main__':
+ unittest.main()