From 4efe7293b698f3eb413651a59c89dd5e31e9d0a3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 19 May 2015 21:19:50 +0100 Subject: Split test presenter into its own class, not rolled into the script host --- project2/ut/testPresenter.cpp | 109 +++++++++++++++++++++++++++++++++++++++++ project2/ut/testPresenter.h | 38 ++++++++++++++ project2/ut/testScriptHost.cpp | 106 ++++----------------------------------- project2/ut/testScriptHost.h | 24 ++------- 4 files changed, 159 insertions(+), 118 deletions(-) create mode 100644 project2/ut/testPresenter.cpp create mode 100644 project2/ut/testPresenter.h diff --git a/project2/ut/testPresenter.cpp b/project2/ut/testPresenter.cpp new file mode 100644 index 0000000..e93d7aa --- /dev/null +++ b/project2/ut/testPresenter.cpp @@ -0,0 +1,109 @@ +#include "testPresenter.h" +#include "misc.h" +#include +#include +#include +#include + +TestPresenter::TestPresenter() : + MultiRowSetPresenter(Default) +{ +} + +void +TestPresenter::addNamedValue(const Glib::ustring & name, const VariableType & value) const +{ + presenterData.push_back(stringbf("%s: %s=%s", __FUNCTION__, name, value)); +} + +void +TestPresenter::addNewRow(const Glib::ustring & name) const +{ + presenterData.push_back(stringbf("%s: %s", __FUNCTION__, name)); +} + +void +TestPresenter::finishRow() const +{ + presenterData.push_back(stringbf("%s", __FUNCTION__)); +} + +void +TestPresenter::addNewRowSet(const Glib::ustring & name) const +{ + presenterData.push_back(stringbf("%s: %s", __FUNCTION__, name)); +} + +void +TestPresenter::addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const +{ + presenterData.push_back(stringbf("%s: %s:%s", __FUNCTION__, ns, name)); +} + +void +TestPresenter::finishRowSet() const +{ + presenterData.push_back(stringbf("%s", __FUNCTION__)); +} + +void +TestPresenter::addNewArray(const Glib::ustring & name, bool objects) const +{ + presenterData.push_back(stringbf("%s: %s(%s)", __FUNCTION__, name, objects)); +} + +void +TestPresenter::finishArray(bool objects) const +{ + presenterData.push_back(stringbf("%s: (%s)", __FUNCTION__, objects)); +} + +void +TestPresenter::init(ExecContext *) +{ + presenterData.push_back(stringbf("%s", __FUNCTION__)); +} + +const PresenterData & +TestPresenter::GetPresenterData() const +{ + return presenterData; +} + +namespace std { + std::istream & + operator>>(std::istream & s, PresenterData & v) + { + while (true) { + char buf[BUFSIZ]; + s.getline(buf, sizeof(buf)); + if (s.good()) { + v.push_back(buf); + } + else { + break; + } + } + return s; + } + + std::ostream & + operator<<(std::ostream & s, const PresenterData & v) + { + for (const auto & e : v) { + s << "[" << e << "]" << std::endl; + } + return s; + } + + bool + operator==(const PresenterData & left, const boost::filesystem::path & rightPath) + { + PresenterData right; + BOOST_REQUIRE(boost::filesystem::exists(rightPath)); + fstream strm(rightPath.string()); + strm >> right; + return (left == right); + } +} + diff --git a/project2/ut/testPresenter.h b/project2/ut/testPresenter.h new file mode 100644 index 0000000..a6772ea --- /dev/null +++ b/project2/ut/testPresenter.h @@ -0,0 +1,38 @@ +#ifndef TESTPRESENTER_H +#define TESTPRESENTER_H + +#include +#include +#include +#include + +typedef std::vector PresenterData; + +class TestPresenter : public MultiRowSetPresenter { + public: + TestPresenter(); + + void addNamedValue(const Glib::ustring & name, const VariableType & value) const override; + void addNewRow(const Glib::ustring & name) const override; + void finishRow() const override; + void addNewRowSet(const Glib::ustring & name) const override; + void addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const override; + void finishRowSet() const override; + void addNewArray(const Glib::ustring & name, bool objects) const override; + void finishArray(bool objects) const override; + void init(ExecContext *) override; + + const PresenterData & GetPresenterData() const; + + private: + mutable PresenterData presenterData; +}; + +namespace std { + bool operator==(const PresenterData &, const boost::filesystem::path &); + std::ostream & operator<<(std::ostream & s, const PresenterData & v); + std::istream & operator>>(std::istream & s, PresenterData & v); +} + +#endif + diff --git a/project2/ut/testScriptHost.cpp b/project2/ut/testScriptHost.cpp index 7d456b9..69ee39b 100644 --- a/project2/ut/testScriptHost.cpp +++ b/project2/ut/testScriptHost.cpp @@ -1,18 +1,14 @@ #include "testScriptHost.h" -#include -#include #include #include #include -#include TestScriptHost::TestScriptHost(ScriptReaderPtr script) : SourceObject(script->root()), CommonObjects(script->root()), CheckHost(script->root()), TaskHost(script->root()), - ViewHost(script->root()), - MultiRowSetPresenter(Default) + ViewHost(script->root()) { Logger()->message(LOG_DEBUG, __PRETTY_FUNCTION__); } @@ -24,7 +20,10 @@ TestScriptHost::~TestScriptHost() MultiRowSetPresenterPtr TestScriptHost::getPresenter(ExecContext *) const { - return const_cast(this); + if (!presenter) { + presenter = new TestPresenter(); + } + return presenter; } void TestScriptHost::process(ExecContext * ec) @@ -34,100 +33,13 @@ void TestScriptHost::process(ExecContext * ec) executeViews(ec); } -void -TestScriptHost::addNamedValue(const Glib::ustring & name, const VariableType & value) const -{ - presenterData.push_back(stringbf("%s: %s=%s", __FUNCTION__, name, value)); -} - -void -TestScriptHost::addNewRow(const Glib::ustring & name) const -{ - presenterData.push_back(stringbf("%s: %s", __FUNCTION__, name)); -} - -void -TestScriptHost::finishRow() const -{ - presenterData.push_back(stringbf("%s", __FUNCTION__)); -} - -void -TestScriptHost::addNewRowSet(const Glib::ustring & name) const -{ - presenterData.push_back(stringbf("%s: %s", __FUNCTION__, name)); -} - -void -TestScriptHost::addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const -{ - presenterData.push_back(stringbf("%s: %s:%s", __FUNCTION__, ns, name)); -} - -void -TestScriptHost::finishRowSet() const -{ - presenterData.push_back(stringbf("%s", __FUNCTION__)); -} - -void -TestScriptHost::addNewArray(const Glib::ustring & name, bool objects) const -{ - presenterData.push_back(stringbf("%s: %s(%s)", __FUNCTION__, name, objects)); -} - -void -TestScriptHost::finishArray(bool objects) const -{ - presenterData.push_back(stringbf("%s: (%s)", __FUNCTION__, objects)); -} - -void -TestScriptHost::init(ExecContext *) -{ - presenterData.push_back(stringbf("%s", __FUNCTION__)); -} - const PresenterData & TestScriptHost::GetPresenterData() const { - return presenterData; -} - -namespace std { - std::istream & - operator>>(std::istream & s, PresenterData & v) - { - while (true) { - char buf[BUFSIZ]; - s.getline(buf, sizeof(buf)); - if (s.good()) { - v.push_back(buf); - } - else { - break; - } - } - return s; - } - - std::ostream & - operator<<(std::ostream & s, const PresenterData & v) - { - for (const auto & e : v) { - s << "[" << e << "]" << std::endl; - } - return s; - } - - bool - operator==(const PresenterData & left, const boost::filesystem::path & rightPath) - { - PresenterData right; - BOOST_REQUIRE(boost::filesystem::exists(rightPath)); - fstream strm(rightPath.string()); - strm >> right; - return (left == right); + auto tp = boost::dynamic_pointer_cast(presenter); + if (!tp) { + throw std::runtime_error("Not using the TestPresenter"); } + return tp->GetPresenterData(); } diff --git a/project2/ut/testScriptHost.h b/project2/ut/testScriptHost.h index a843a4a..c838f9c 100644 --- a/project2/ut/testScriptHost.h +++ b/project2/ut/testScriptHost.h @@ -3,12 +3,9 @@ #include #include -#include -#include +#include "testPresenter.h" -typedef std::vector PresenterData; - -class TestScriptHost : public TaskHost, public ViewHost, public MultiRowSetPresenter { +class TestScriptHost : public TaskHost, public ViewHost { public: TestScriptHost(ScriptReaderPtr script); ~TestScriptHost(); @@ -16,27 +13,12 @@ class TestScriptHost : public TaskHost, public ViewHost, public MultiRowSetPrese MultiRowSetPresenterPtr getPresenter(ExecContext *) const override; void process(ExecContext * ec); - void addNamedValue(const Glib::ustring & name, const VariableType & value) const override; - void addNewRow(const Glib::ustring & name) const override; - void finishRow() const override; - void addNewRowSet(const Glib::ustring & name) const override; - void addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const override; - void finishRowSet() const override; - void addNewArray(const Glib::ustring & name, bool objects) const override; - void finishArray(bool objects) const override; - void init(ExecContext *) override; const PresenterData & GetPresenterData() const; private: - mutable PresenterData presenterData; + mutable MultiRowSetPresenterPtr presenter; }; -namespace std { - bool operator==(const PresenterData &, const boost::filesystem::path &); - std::ostream & operator<<(std::ostream & s, const PresenterData & v); - std::istream & operator>>(std::istream & s, PresenterData & v); -} - #endif -- cgit v1.2.3