From 8df49cbf6252576c6ee694f6e8703bfa27bf267c Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 5 Jun 2014 10:34:29 +0000 Subject: Adds unit tests covering class of builtin types and class of optional built in, seq, dict, struct, class Address support for optionals --- slicer/slicer/modelParts.h | 44 +++++++++++++++-------- slicer/slicer/slicer.h | 2 +- slicer/test/initial/builtins.xml | 11 ++++++ slicer/test/initial/optionals-areset.xml | 38 ++++++++++++++++++++ slicer/test/initial/optionals-notset.xml | 3 ++ slicer/test/run-slicer.cpp | 61 ++++++++++++++++++++++++++++++-- slicer/xml/serializer.cpp | 2 +- 7 files changed, 143 insertions(+), 18 deletions(-) create mode 100644 slicer/test/initial/builtins.xml create mode 100644 slicer/test/initial/optionals-areset.xml create mode 100644 slicer/test/initial/optionals-notset.xml diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 761c557..0bdcb25 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -61,6 +61,7 @@ namespace Slicer { virtual void Complete(); virtual void SetValue(ValueSourcePtr); virtual void GetValue(ValueTargetPtr); + virtual bool HasValue() const = 0; }; template @@ -80,6 +81,7 @@ namespace Slicer { virtual ModelPartPtr GetChild(const std::string &) override { return NULL; } virtual void SetValue(ValueSourcePtr s) override { s->set(Member); } virtual void GetValue(ValueTargetPtr s) override { s->get(Member); } + virtual bool HasValue() const override { return true; } private: T & Member; @@ -102,7 +104,7 @@ namespace Slicer { modelPart = new T(*OptionalMember); } } - virtual void OnEachChild(const ChildHandler & ch) + virtual void OnEachChild(const ChildHandler & ch) override { if (OptionalMember) { modelPart->OnEachChild(ch); @@ -144,6 +146,8 @@ namespace Slicer { modelPart->GetValue(s); } + virtual bool HasValue() const override { return OptionalMember; } + private: IceUtil::Optional< typename T::element_type > & OptionalMember; ModelPartPtr modelPart; @@ -154,23 +158,24 @@ namespace Slicer { public: class HookBase : public IceUtil::Shared { public: - virtual ModelPartPtr Get(T & t) const = 0; + virtual ModelPartPtr Get(T * t) const = 0; }; typedef IceUtil::Handle HookPtr; template class Hook : public HookBase { public: - ModelPartPtr Get(T & t) const override + ModelPartPtr Get(T * t) const override { - return new MP(t.*M); + return t ? new MP(t->*M) : NULL; } }; virtual void OnEachChild(const ChildHandler & ch) { for (auto h = hooks.begin(); h != hooks.end(); h++) { - ch(h->first, h->second->Get(GetModel())); + auto modelPart = h->second->Get(GetModel()); + ch(h->first, modelPart && modelPart->HasValue() ? modelPart : ModelPartPtr()); } } @@ -183,7 +188,7 @@ namespace Slicer { return NULL; } - virtual T & GetModel() = 0; + virtual T * GetModel() = 0; typedef std::map Hooks; @@ -211,11 +216,13 @@ namespace Slicer { ModelObject = new typename T::element_type(); } - typename T::element_type & GetModel() override + typename T::element_type * GetModel() override { - return *ModelObject; + return ModelObject.get(); } + virtual bool HasValue() const override { return ModelObject; } + private: T & ModelObject; }; @@ -235,11 +242,13 @@ namespace Slicer { { } - T & GetModel() override + T * GetModel() override { - return ModelObject; + return &ModelObject; } + virtual bool HasValue() const override { return true; } + private: T & ModelObject; }; @@ -272,11 +281,13 @@ namespace Slicer { ch(rootName, new ModelPartForClass(ModelObject)); } - typename T::element_type & GetModel() override + typename T::element_type * GetModel() override { - return *ModelObject; + return ModelObject.get(); } + virtual bool HasValue() const override { return ModelObject; } + private: T ModelObject; static std::string rootName; @@ -306,6 +317,8 @@ namespace Slicer { ModelPartPtr GetChild(const std::string &) override; + virtual bool HasValue() const override { return true; } + private: ModelPartPtr elementModelPart(typename T::value_type &) const; @@ -322,11 +335,13 @@ namespace Slicer { { } - ModelPartForDictionaryElement & GetModel() override + ModelPartForDictionaryElement * GetModel() override { - return *this; + return this; } + virtual bool HasValue() const override { return true; } + typename T::key_type * key; typename T::mapped_type * value; }; @@ -378,6 +393,7 @@ namespace Slicer { return new ModelPartForDictionaryElementInserter(dictionary); } + virtual bool HasValue() const override { return true; } private: T & dictionary; diff --git a/slicer/slicer/slicer.h b/slicer/slicer/slicer.h index 55ccf00..0900bf9 100644 --- a/slicer/slicer/slicer.h +++ b/slicer/slicer/slicer.h @@ -14,7 +14,7 @@ namespace Slicer { IceUtil::Handle>> root = new ModelPartForClassRoot>(); SerializerPtr serializer = new Serializer(); serializer->Deserialize(path, root); - return &root->GetModel(); + return root->GetModel(); } template diff --git a/slicer/test/initial/builtins.xml b/slicer/test/initial/builtins.xml new file mode 100644 index 0000000..650d187 --- /dev/null +++ b/slicer/test/initial/builtins.xml @@ -0,0 +1,11 @@ + + + true + 4 + 3.14159265359 + 3.14 + 80 + 800 + 40 + Sample text + diff --git a/slicer/test/initial/optionals-areset.xml b/slicer/test/initial/optionals-areset.xml new file mode 100644 index 0000000..e4cdf42 --- /dev/null +++ b/slicer/test/initial/optionals-areset.xml @@ -0,0 +1,38 @@ + + + 4 + + 1 + 2 + + + 1 + 2 + + + + 3 + 4 + + + 5 + 6 + + + + + 10 + + 11 + 12 + + + + 13 + + 14 + 15 + + + + diff --git a/slicer/test/initial/optionals-notset.xml b/slicer/test/initial/optionals-notset.xml new file mode 100644 index 0000000..8fbb701 --- /dev/null +++ b/slicer/test/initial/optionals-notset.xml @@ -0,0 +1,3 @@ + + + diff --git a/slicer/test/run-slicer.cpp b/slicer/test/run-slicer.cpp index 6a73736..897d9ee 100644 --- a/slicer/test/run-slicer.cpp +++ b/slicer/test/run-slicer.cpp @@ -5,10 +5,66 @@ #include #include #include +#include #include namespace fs = boost::filesystem; +template +void +verify(const fs::path & root, const fs::path & tmp, const fs::path & infile, const boost::function & check = NULL) +{ + const fs::path input = root / "initial" / infile; + const fs::path output = tmp / infile; + + fprintf(stderr, "%s : Deserialize\n", input.string().c_str()); + IceInternal::Handle p = Slicer::Deserialize(input); + fprintf(stderr, "%s : Check1\n", input.string().c_str()); + if (check) check(*p); + fprintf(stderr, "%s : Serialize -> %s\n", input.string().c_str(), output.string().c_str()); + Slicer::Serialize(p, output); + fprintf(stderr, "%s : Check2\n", input.string().c_str()); + if (check) check(*p); + fprintf(stderr, "%s : OK\n", input.string().c_str()); +} + +void +checkOptionals_notset(const TestModule::Optionals & opts) +{ + BOOST_ASSERT(!opts.optSimple); + BOOST_ASSERT(!opts.optStruct); + BOOST_ASSERT(!opts.optClass); + BOOST_ASSERT(!opts.optSeq); + BOOST_ASSERT(!opts.optDict); +} + +void +checkOptionals_areset(const TestModule::Optionals & opts) +{ + BOOST_ASSERT(opts.optSimple); + BOOST_ASSERT(opts.optSimple == 4); + BOOST_ASSERT(opts.optStruct); + BOOST_ASSERT(opts.optStruct->a == 1); + BOOST_ASSERT(opts.optStruct->b == 2); + BOOST_ASSERT(opts.optClass); + BOOST_ASSERT((*opts.optClass)->a == 1); + BOOST_ASSERT((*opts.optClass)->b == 2); + BOOST_ASSERT(opts.optSeq->size() == 2); + BOOST_ASSERT((*opts.optSeq)[0]->a == 3); + BOOST_ASSERT((*opts.optSeq)[0]->b == 4); + BOOST_ASSERT((*opts.optSeq)[1]->a == 5); + BOOST_ASSERT((*opts.optSeq)[1]->b == 6); + BOOST_ASSERT(opts.optDict); + BOOST_ASSERT(opts.optDict->size() == 2); + BOOST_ASSERT(opts.optDict->find(1) == opts.optDict->end()); + BOOST_ASSERT(opts.optDict->find(10) != opts.optDict->end()); + BOOST_ASSERT(opts.optDict->find(10)->second->a == 11); + BOOST_ASSERT(opts.optDict->find(10)->second->b == 12); + BOOST_ASSERT(opts.optDict->find(13) != opts.optDict->end()); + BOOST_ASSERT(opts.optDict->find(13)->second->a == 14); + BOOST_ASSERT(opts.optDict->find(13)->second->b == 15); +} + int main(int, char ** argv) { @@ -26,8 +82,9 @@ main(int, char ** argv) fs::create_directory(tmp); // Execute - TestModule::BuiltInsPtr p(new TestModule::BuiltIns()); - Slicer::Serialize(p, tmp / "out.xml"); + verify(root, tmp, "builtins.xml"); + verify(root, tmp, "optionals-notset.xml", checkOptionals_notset); + verify(root, tmp, "optionals-areset.xml", checkOptionals_areset); return 0; } diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index 0a065d8..1ff6058 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -206,7 +206,7 @@ namespace Slicer { if (name[0] == '@') { mp->GetValue(new XmlAttributeValueTarget(n, name.substr(1))); } - else { + else if (mp) { auto element = n->add_child(name); mp->GetValue(new XmlContentValueTarget(element)); mp->OnEachChild(boost::bind(&Xml::ModelTreeIterate, element, _1, _2)); -- cgit v1.2.3