From 92694b907d7575462c029723ccad14dff7d2c146 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 28 Nov 2014 10:50:20 +0000 Subject: Add a unit test over config variable Adds p2common unit tests dir Adds a very basic test script node that accepts a simple map for values --- project2/common/Jamfile.jam | 4 +- project2/common/unittests/Jamfile.jam | 9 ++++ project2/common/unittests/testConfig.cpp | 18 +++++++ project2/common/variables/config.cpp | 1 + project2/ut/testScriptNode.cpp | 83 ++++++++++++++++++++++++++++++++ project2/ut/testScriptNode.h | 28 +++++++++++ 6 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 project2/common/unittests/Jamfile.jam create mode 100644 project2/common/unittests/testConfig.cpp create mode 100644 project2/ut/testScriptNode.cpp create mode 100644 project2/ut/testScriptNode.h diff --git a/project2/common/Jamfile.jam b/project2/common/Jamfile.jam index 93c9754..6514b1e 100644 --- a/project2/common/Jamfile.jam +++ b/project2/common/Jamfile.jam @@ -7,6 +7,8 @@ lib boost_system : : boost_system ; lib boost_filesystem : : boost_filesystem ; lib boost_date_time : : boost_date_time ; +build-project unittests ; + cpp-pch pch : pch.hpp : ../../libmisc ../lib//p2lib @@ -15,7 +17,7 @@ cpp-pch pch : pch.hpp : lib p2common : pch - [ glob-tree *.cpp ] + [ glob-tree *.cpp : unittests ] : . glibmm diff --git a/project2/common/unittests/Jamfile.jam b/project2/common/unittests/Jamfile.jam new file mode 100644 index 0000000..540c1e0 --- /dev/null +++ b/project2/common/unittests/Jamfile.jam @@ -0,0 +1,9 @@ +import testing ; + +unit-test testConfig : + testConfig.cpp + : + ../../ut//p2ut + ..//p2common + ; + diff --git a/project2/common/unittests/testConfig.cpp b/project2/common/unittests/testConfig.cpp new file mode 100644 index 0000000..8c25394 --- /dev/null +++ b/project2/common/unittests/testConfig.cpp @@ -0,0 +1,18 @@ +#define BOOST_TEST_MODULE Config +#include +#include +#include +#include + +BOOST_AUTO_TEST_CASE( config_application_value ) +{ + TestOptionsSource::LoadTestOptions({ + { "application.data", "testvalue" } + }); + Variable::VariableImplPtr c = VariableLoader::createNew("config", new TestScriptNode({ + { "name", VariableType("data") } + })); + BOOST_REQUIRE(c); + BOOST_REQUIRE_EQUAL("testvalue", c->value(NULL).as()); +} + diff --git a/project2/common/variables/config.cpp b/project2/common/variables/config.cpp index 9b4cccb..5b36f6e 100644 --- a/project2/common/variables/config.cpp +++ b/project2/common/variables/config.cpp @@ -72,6 +72,7 @@ class VariableConfigLoader : public VariableLoader::For { opts("Variables - ModConfig options") { opts(new AppSettings()); + OptionsSets::Add(typeid(AppSettings).name(), &opts); } const Options * options() const { diff --git a/project2/ut/testScriptNode.cpp b/project2/ut/testScriptNode.cpp new file mode 100644 index 0000000..fb54c12 --- /dev/null +++ b/project2/ut/testScriptNode.cpp @@ -0,0 +1,83 @@ +#include "testScriptNode.h" +#include "variables/literal.h" +#include + +TestScriptNode::TestScriptNode(const Vars & v) : + ScriptNode(NULL), + vars(v) +{ +} + +bool TestScriptNode::componentNamespace() const +{ + return false; +} + +std::string TestScriptNode::get_name() const +{ + Logger()->messagebf(LOG_DEBUG, "%s", __PRETTY_FUNCTION__); + return std::string(); +} + +ScriptNodePtr TestScriptNode::child(const Glib::ustring & name, bool required) const +{ + Logger()->messagebf(LOG_DEBUG, "%s", __PRETTY_FUNCTION__); + if (required) { + throw ValueNotFound(name); + } + return NULL; +} + +ScriptNode::ScriptNodes TestScriptNode::children() const +{ + Logger()->messagebf(LOG_DEBUG, "%s", __PRETTY_FUNCTION__); + return ScriptNode::ScriptNodes(); +} + +ScriptNode::ScriptNodes TestScriptNode::childrenIn(const Glib::ustring & sub) const +{ + Logger()->messagebf(LOG_DEBUG, "%s", __PRETTY_FUNCTION__); + (void)sub; + return ScriptNode::ScriptNodes(); +} + +bool TestScriptNode::valueExists(const Glib::ustring & name) const +{ + Logger()->messagebf(LOG_DEBUG, "%s", __PRETTY_FUNCTION__); + return vars.find(name.collate_key()) != vars.end(); +} + +bool TestScriptNode::applyValue(const Glib::ustring & name, VariableType & target, ExecContext *) const +{ + Logger()->messagebf(LOG_DEBUG, "%s: %s", __PRETTY_FUNCTION__, name); + auto i = vars.find(name.collate_key()); + if (i != vars.end()) { + target = i->second; + Logger()->messagebf(LOG_DEBUG, "%s -> %s", __PRETTY_FUNCTION__, target); + return true; + } + return false; +} + +VariableImpl * TestScriptNode::variable(const boost::optional & defaultSource) const +{ + Logger()->messagebf(LOG_DEBUG, "%s: defaultSource = %s", __PRETTY_FUNCTION__, + defaultSource ? *defaultSource : "NULL"); + (void)defaultSource; + return NULL; +} + +VariableImpl * TestScriptNode::variable(const Glib::ustring & name) const +{ + Logger()->messagebf(LOG_DEBUG, "%s: %s", __PRETTY_FUNCTION__, name); + auto i = vars.find(name.collate_key()); + if (i != vars.end()) { + return new VariableLiteral(i->second); + } + throw ValueNotFound(name); +} + +void TestScriptNode::composeWithCallbacks(const LiteralCallback &, const NodeCallback &) const +{ +} + diff --git a/project2/ut/testScriptNode.h b/project2/ut/testScriptNode.h new file mode 100644 index 0000000..d85f6db --- /dev/null +++ b/project2/ut/testScriptNode.h @@ -0,0 +1,28 @@ +#ifndef TESTSCRIPTNODE_H +#define TESTSCRIPTNODE_H + +#include + +class TestScriptNode : public ScriptNode { + public: + typedef std::map Vars; + + TestScriptNode(const Vars &); + + bool componentNamespace() const; + std::string get_name() const; + ScriptNodePtr child(const Glib::ustring & name, bool required = true) const; + ScriptNodes children() const; + ScriptNodes childrenIn(const Glib::ustring & sub) const; + bool valueExists(const Glib::ustring & name) const; + bool applyValue(const Glib::ustring & name, VariableType & target, ExecContext *) const; + VariableImpl * variable(const boost::optional & defaultSource = boost::optional()) const; + VariableImpl * variable(const Glib::ustring & name) const; + void composeWithCallbacks(const LiteralCallback &, const NodeCallback &) const; + + private: + Vars vars; +}; + +#endif + -- cgit v1.2.3