diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9ab6e94d4b3f007925b569e26745345cc235bacd..fd34f690a44f6404e2be1de12c7125a90395d573 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -55,3 +55,6 @@ FOREACH(lib ${signalcast_libs}) ENDFOREACH() DYNAMIC_GRAPH_TEST(test_signalcast) TARGET_LINK_LIBRARIES(${EXECUTABLE_NAME} ${signalcast_libs}) + +# Unit testing. +DYNAMIC_GRAPH_TEST(entity) \ No newline at end of file diff --git a/tests/entity.cpp b/tests/entity.cpp new file mode 100644 index 0000000000000000000000000000000000000000..467503f7fc9c12003f58a2d44ab0a782f107df6f --- /dev/null +++ b/tests/entity.cpp @@ -0,0 +1,190 @@ +// Copyright 2010 Thomas Moulard. +// +// This file is part of dynamic-graph. +// dynamic-graph is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// dynamic-graph is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// You should have received a copy of the GNU Lesser General Public License +// along with dynamic-graph. If not, see <http://www.gnu.org/licenses/>. + +#include <sstream> +#include <dynamic-graph/entity.h> +#include <dynamic-graph/exception-factory.h> + +#define BOOST_TEST_MODULE entity + +#include <boost/test/unit_test.hpp> +#include <boost/test/output_test_stream.hpp> + +using boost::test_tools::output_test_stream; + +BOOST_AUTO_TEST_CASE (constructor) +{ + BOOST_CHECK_EQUAL (dynamicgraph::Entity::CLASS_NAME, "Entity"); + + dynamicgraph::Entity entity ("my-entity"); + BOOST_CHECK_EQUAL (entity.getName (), "my-entity"); + BOOST_CHECK_EQUAL (entity.getClassName (), dynamicgraph::Entity::CLASS_NAME); + + dynamicgraph::Entity entity2 (""); +} + +BOOST_AUTO_TEST_CASE (signal) +{ + dynamicgraph::Entity entity (""); + + // Non const getter. + try + { + entity.getSignal ("I do not exist"); + } + catch (dynamicgraph::ExceptionFactory& exception) + { + //FIXME: getCode should be const. + BOOST_CHECK_EQUAL (exception.getCode (), + dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL); + } + + // Const getter. + try + { + const dynamicgraph::Entity& entityConst = entity; + entityConst.getSignal ("I do not exist"); + } + catch (dynamicgraph::ExceptionFactory& exception) + { + //FIXME: getCode should be const. + BOOST_CHECK_EQUAL (exception.getCode (), + dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL); + } +} + +BOOST_AUTO_TEST_CASE (displaySignalList) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + + entity.displaySignalList(output); + BOOST_CHECK (output.is_equal ("--- <my-entity> signal list: \n")); +} + +BOOST_AUTO_TEST_CASE (display) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + + entity.display(output); + BOOST_CHECK (output.is_equal ("Entity: my-entity")); +} + +BOOST_AUTO_TEST_CASE (getCommandList) +{ + dynamicgraph::Entity entity ("my-entity"); + + //FIXME: this is wrong, another available command is help. + BOOST_CHECK_EQUAL (entity.getCommandList (), "print\nsignals\nsignalDep"); +} + +BOOST_AUTO_TEST_CASE (commandLine_help) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + + std::istringstream args; + + entity.commandLine("help", args, output); + BOOST_CHECK + (output.is_equal + ( + "Entity : \n" + " - print\t\t\tWhat d'you think?\n" + " - signals\t\t\tDisplay the signals list.\n" + " - signalDep <signame> \tDisplay the dependency graph for signal signame.\n" + )); +} + +BOOST_AUTO_TEST_CASE (commandLine_print) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + + std::istringstream args; + + entity.commandLine("print", args, output); + BOOST_CHECK (output.is_equal ("Entity: my-entity\n")); +} + +BOOST_AUTO_TEST_CASE (commandLine_signals) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + + std::istringstream args; + + entity.commandLine("signals", args, output); + BOOST_CHECK (output.is_equal ("--- <my-entity> signal list: \n")); +} + +// FIXME: is it what we should expect? +BOOST_AUTO_TEST_CASE (commandLine_signalDep) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + + std::istringstream args; + + try + { + entity.commandLine("signalDep", args, output); + } + catch (dynamicgraph::ExceptionFactory& exception) + { + //FIXME: getCode should be const. + BOOST_CHECK_EQUAL (exception.getCode (), + dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL); + } + + BOOST_CHECK (output.is_empty ()); +} + +BOOST_AUTO_TEST_CASE (writeGraph) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + entity.writeGraph (output); + + BOOST_CHECK (output.is_equal ("")); +} + +BOOST_AUTO_TEST_CASE (writeCompletionList) +{ + dynamicgraph::Entity entity ("my-entity"); + + output_test_stream output; + entity.writeGraph (output); + + BOOST_CHECK (output.is_equal ("")); +} + +// WTF? +BOOST_AUTO_TEST_CASE (wtf) +{ + dynamicgraph::Entity entity ("my-entity"); + BOOST_CHECK_EQUAL (entity.test (), + static_cast<dynamicgraph::SignalBase<int>*> (0)); + + entity.test2 (static_cast<dynamicgraph::SignalBase<int>*> (0)); +}