From aea8cd1c7b38d5c54c83bdf59a1643255d3063a1 Mon Sep 17 00:00:00 2001 From: Thomas Moulard <thomas.moulard@gmail.com> Date: Wed, 29 Dec 2010 01:04:49 +0100 Subject: [PATCH] Add tests for interpreter. --- tests/CMakeLists.txt | 5 +- tests/data/empty.sot | 0 tests/interpreter.cpp | 219 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 tests/data/empty.sot create mode 100644 tests/interpreter.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c2c36ad9..db6f1daa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,6 +21,8 @@ INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) # Make Boost.Test generates the main function in test cases. ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK -DBOOST_TEST_MAIN) +ADD_DEFINITIONS(-DTESTS_DATADIR="${CMAKE_CURRENT_SOURCE_DIR}/data") + # DYNAMIC_GRAPH_TEST(NAME) # ------------------------ # @@ -58,4 +60,5 @@ DYNAMIC_GRAPH_TEST(entity) DYNAMIC_GRAPH_TEST(custom-entity) DYNAMIC_GRAPH_TEST(factory) DYNAMIC_GRAPH_TEST(pool) -DYNAMIC_GRAPH_TEST(signal-time-dependent) \ No newline at end of file +DYNAMIC_GRAPH_TEST(signal-time-dependent) +DYNAMIC_GRAPH_TEST(interpreter) \ No newline at end of file diff --git a/tests/data/empty.sot b/tests/data/empty.sot new file mode 100644 index 00000000..e69de29b diff --git a/tests/interpreter.cpp b/tests/interpreter.cpp new file mode 100644 index 00000000..eb45a26f --- /dev/null +++ b/tests/interpreter.cpp @@ -0,0 +1,219 @@ +// 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/exception-factory.h> +#include <dynamic-graph/interpreter.h> + +#define BOOST_TEST_MODULE interpreter + +#include <boost/test/unit_test.hpp> +#include <boost/test/output_test_stream.hpp> + +using boost::test_tools::output_test_stream; + +// This unit test checks the dynamic-graph interpreter +// and its related mechanisms. +// +// It may be split in several tests in the future if this +// becomes too big. + + +// Simple test to check the shell default constructor. +BOOST_AUTO_TEST_CASE (constructor) +{ + dynamicgraph::Interpreter shell; +} + + +// Check that an empty command is valid. +BOOST_AUTO_TEST_CASE (cmd_empty) +{ + output_test_stream output; + std::istringstream args; + dynamicgraph::Interpreter shell; + shell.cmd ("", args, output); + BOOST_CHECK (output.is_empty ()); +} + + +// Check that a non existing command returns +// the appropriate error. +BOOST_AUTO_TEST_CASE (cmd_notexist) +{ + output_test_stream output; + std::istringstream args; + dynamicgraph::Interpreter shell; + try + { + shell.cmd ("I DO NOT EXIST", args, output); + } + catch (const dynamicgraph::ExceptionFactory& exception) + { + BOOST_CHECK_EQUAL (exception.getCode (), + dynamicgraph::ExceptionFactory::UNREFERED_FUNCTION); + } +} + + +// Check that the "help" command works. +BOOST_AUTO_TEST_CASE (cmd_help) +{ + output_test_stream output; + std::istringstream args; + dynamicgraph::Interpreter shell; + shell.cmd ("help", args, output); + BOOST_CHECK + (output.is_equal + ( + "Help\n" + " - compute <obj.sig> <time> Recompute <sig> at time <time> \n" + " - destroy <object> Destroy an object.\n" + " - get <obj.signal> <value> Get the signal to the constant value.\n" + " - import <script.txt> Import the script.\n" + " Behaves like run but searches for files in default script directories.\n" + " - loadPlugin <file.so> <directory> Load the plugin from the specified directory.\n" + " - new <class> <object> Create a new entity.\n" + " - plug <obj1.sig1> <obj2.sig2> Plug sig1 (producer) on sig2 (consumer).\n" + " - popImportPaths <path> Drop path from default directories.\n" + " - pushImportPaths <path> Add path to default directories.\n" + " - run <script.txt> Run the script.\n" + " - set <obj.signal> <value> Set the signal to the constant value.\n" + " - unloadPlugin <path/file.so> Unload the plugin.\n" + )); +} + + +// Check that an empty file can be parsed successfully. +BOOST_AUTO_TEST_CASE (cmd_run_emptyfile) +{ + output_test_stream output; + dynamicgraph::Interpreter shell; + + std::stringstream ss; + + std::string cmd = "run"; + ss << TESTS_DATADIR "/empty.sot"; + + std::istringstream args (ss.str ()); + shell.cmd (cmd, args, output); + BOOST_CHECK (output.is_empty ()); +} + +// Check that trying to run a non existing file returns an appropriate +// error. +BOOST_AUTO_TEST_CASE (cmd_run_notexist) +{ + output_test_stream output; + dynamicgraph::Interpreter shell; + + std::stringstream ss; + + std::string cmd = "run"; + ss << "idonotexist"; + + std::istringstream args (ss.str ()); + try + { + shell.cmd (cmd, args, output); + BOOST_ERROR ("Should never happen"); + } + catch (const dynamicgraph::ExceptionFactory& exception) + { + BOOST_CHECK_EQUAL (exception.getCode (), + dynamicgraph::ExceptionFactory::READ_FILE); + } +} + +// Check that import a non existing file returns an appropriate error. +BOOST_AUTO_TEST_CASE (cmd_import_notexist) +{ + output_test_stream output; + dynamicgraph::Interpreter shell; + + std::stringstream ss; + + std::string cmd = "import"; + ss << "idonotexist"; + + std::istringstream args (ss.str ()); + try + { + shell.cmd (cmd, args, output); + BOOST_ERROR ("Should never happen"); + } + catch (const dynamicgraph::ExceptionFactory& exception) + { + BOOST_CHECK_EQUAL (exception.getCode (), + dynamicgraph::ExceptionFactory::READ_FILE); + } +} + +// Check that import/pushImportPaths/popImportPaths works. +BOOST_AUTO_TEST_CASE (cmd_import_push) +{ + dynamicgraph::Interpreter shell; + + // Push path. + { + output_test_stream output; + std::stringstream ss; + + std::string cmd = "pushImportPaths"; + ss << TESTS_DATADIR; + std::istringstream args (ss.str ()); + shell.cmd (cmd, args, output); + BOOST_CHECK (output.is_empty ()); + } + + // Import empty file. + { + output_test_stream output; + std::stringstream ss; + + std::string cmd = "import"; + ss << "empty.sot"; + std::istringstream args (ss.str ()); + shell.cmd (cmd, args, output); + BOOST_CHECK (output.is_empty ()); + } + + // Pop path. + { + output_test_stream output; + std::stringstream ss; + + std::string cmd = "popImportPaths"; + std::istringstream args (ss.str ()); + shell.cmd (cmd, args, output); + BOOST_CHECK (output.is_empty ()); + } + + // Import empty file again: this will fail + // as empty.sot is *NOT* in a standard location. + { + output_test_stream output; + std::stringstream ss; + + std::string cmd = "import"; + ss << "empty.sot"; + std::istringstream args (ss.str ()); + + // Make sure this trigger an error. + BOOST_CHECK_THROW (shell.cmd (cmd, args, output), + dynamicgraph::ExceptionFactory); + } + +} -- GitLab