summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2014-06-05 10:34:29 +0000
committerrandomdan <randomdan@localhost>2014-06-05 10:34:29 +0000
commit8df49cbf6252576c6ee694f6e8703bfa27bf267c (patch)
tree3adac5f6098244fa4e49a104b57abd2f28a379b8
parentMore friendly linkflags and some ycm configs (diff)
downloadslicer-8df49cbf6252576c6ee694f6e8703bfa27bf267c.tar.bz2
slicer-8df49cbf6252576c6ee694f6e8703bfa27bf267c.tar.xz
slicer-8df49cbf6252576c6ee694f6e8703bfa27bf267c.zip
Adds unit tests covering class of builtin types and class of optional built in, seq, dict, struct, class
Address support for optionals
-rw-r--r--slicer/slicer/modelParts.h44
-rw-r--r--slicer/slicer/slicer.h2
-rw-r--r--slicer/test/initial/builtins.xml11
-rw-r--r--slicer/test/initial/optionals-areset.xml38
-rw-r--r--slicer/test/initial/optionals-notset.xml3
-rw-r--r--slicer/test/run-slicer.cpp61
-rw-r--r--slicer/xml/serializer.cpp2
7 files changed, 143 insertions, 18 deletions
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<typename T>
@@ -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<HookBase> HookPtr;
template <typename MT, MT T::*M, typename MP>
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<std::string, HookPtr> 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<T>(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<T> & GetModel() override
+ ModelPartForDictionaryElement<T> * 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<T>(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<ModelPartForClassRoot<IceInternal::Handle<Object>>> root = new ModelPartForClassRoot<IceInternal::Handle<Object>>();
SerializerPtr serializer = new Serializer();
serializer->Deserialize(path, root);
- return &root->GetModel();
+ return root->GetModel();
}
template <typename Serializer, typename Object>
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 @@
+<?xml version="1.0"?>
+<BuiltIns>
+ <mbool>true</mbool>
+ <mbyte>4</mbyte>
+ <mdouble>3.14159265359</mdouble>
+ <mfloat>3.14</mfloat>
+ <mint>80</mint>
+ <mlong>800</mlong>
+ <mshort>40</mshort>
+ <mstring>Sample text</mstring>
+</BuiltIns>
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 @@
+<?xml version="1.0"?>
+<Optionals>
+ <optSimple>4</optSimple>
+ <optClass>
+ <a>1</a>
+ <b>2</b>
+ </optClass>
+ <optStruct>
+ <a>1</a>
+ <b>2</b>
+ </optStruct>
+ <optSeq>
+ <element>
+ <a>3</a>
+ <b>4</b>
+ </element>
+ <element>
+ <a>5</a>
+ <b>6</b>
+ </element>
+ </optSeq>
+ <optDict>
+ <element>
+ <key>10</key>
+ <value>
+ <a>11</a>
+ <b>12</b>
+ </value>
+ </element>
+ <element>
+ <key>13</key>
+ <value>
+ <a>14</a>
+ <b>15</b>
+ </value>
+ </element>
+ </optDict>
+</Optionals>
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 @@
+<?xml version="1.0"?>
+<Optionals>
+</Optionals>
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 <boost/filesystem/operations.hpp>
#include <boost/format.hpp>
#include <boost/function.hpp>
+#include <boost/assert.hpp>
#include <types.h>
namespace fs = boost::filesystem;
+template<typename T, typename Serializer>
+void
+verify(const fs::path & root, const fs::path & tmp, const fs::path & infile, const boost::function<void(const T &)> & 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<T> p = Slicer::Deserialize<Serializer, T>(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<Serializer>(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<Slicer::Xml>(p, tmp / "out.xml");
+ verify<TestModule::BuiltIns, Slicer::Xml>(root, tmp, "builtins.xml");
+ verify<TestModule::Optionals, Slicer::Xml>(root, tmp, "optionals-notset.xml", checkOptionals_notset);
+ verify<TestModule::Optionals, Slicer::Xml>(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));