From aa2a9f4da591509858b011f5aacc7cfc742821ff Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 16 Oct 2015 20:51:06 +0100 Subject: Improve test coverage. Fix empty xml element handling to still call into children with empty content --- slicer/test/initial/builtins3.json | 2 ++ slicer/test/serializers.cpp | 18 ++++++++++++++ slicer/xml/Jamfile.jam | 20 +++++++++++++++- slicer/xml/serializer.cpp | 23 +++++++++++------- slicer/xml/serializer.h | 5 ++++ slicer/xml/testSpecifics.cpp | 48 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 slicer/test/initial/builtins3.json create mode 100644 slicer/xml/testSpecifics.cpp diff --git a/slicer/test/initial/builtins3.json b/slicer/test/initial/builtins3.json new file mode 100644 index 0000000..753395b --- /dev/null +++ b/slicer/test/initial/builtins3.json @@ -0,0 +1,2 @@ +{"mbool":false,"mbyte":255,"mdouble":-3.0625,"mfloat":-3.125,"mint":-80,"mlong":-800,"mshort":-40,"mstring":"-Sample text-"} + diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index e381c6d..0d309dc 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -114,6 +114,19 @@ checkBuiltIns_valuesCorrect(const TestModule::BuiltInsPtr & bt) BOOST_REQUIRE_EQUAL(bt->mstring, "Sample text"); } +void +checkBuiltIns3_valuesCorrect(const TestModule::BuiltInsPtr & bt) +{ + BOOST_REQUIRE_EQUAL(bt->mbool, false); + BOOST_REQUIRE_EQUAL(bt->mbyte, 255); + BOOST_REQUIRE_EQUAL(bt->mshort, -40); + BOOST_REQUIRE_EQUAL(bt->mint, -80); + BOOST_REQUIRE_EQUAL(bt->mlong, -800); + BOOST_REQUIRE_EQUAL(bt->mfloat, -3.125); + BOOST_REQUIRE_EQUAL(bt->mdouble, -3.0625); + BOOST_REQUIRE_EQUAL(bt->mstring, "-Sample text-"); +} + void checkInherits_types(const TestModule::InheritanceContPtr & i) { @@ -359,6 +372,11 @@ BOOST_AUTO_TEST_CASE( builtins2_json ) verifyByFile("builtins2.json", checkBuiltIns_valuesCorrect); } +BOOST_AUTO_TEST_CASE( builtins3_json ) +{ + verifyByFile("builtins3.json", checkBuiltIns3_valuesCorrect); +} + BOOST_AUTO_TEST_CASE( optionals_areset2_json ) { verifyByFile("optionals-areset2.json", checkOptionals_areset); diff --git a/slicer/xml/Jamfile.jam b/slicer/xml/Jamfile.jam index ed2fbf1..7b07545 100644 --- a/slicer/xml/Jamfile.jam +++ b/slicer/xml/Jamfile.jam @@ -1,13 +1,16 @@ +import testing ; + alias libxmlpp : : : : "`pkg-config --cflags libxml++-2.6`" "`pkg-config --libs libxml++-2.6`" ; lib boost_system ; lib boost_filesystem ; +lib boost_utf : : boost_unit_test_framework ; lib IceUtil ; lib adhocutil : : : : /usr/include/adhocutil ; lib slicer-xml : - [ glob *.cpp ] + [ glob *.cpp : test*.cpp ] : .. boost_system @@ -21,3 +24,18 @@ lib slicer-xml : : : libxmlpp ; + +run testSpecifics.cpp + : : : + BOOST_TEST_DYN_LINK + slicer-xml + boost_utf + ../test//slicer-test + ../test//common + ../slicer//slicer + .. + ../test//compilation + : + testSpecifics + ; + diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index 225a1b3..6f5df3a 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -14,13 +14,10 @@ namespace Slicer { const std::string md_bare = "xml:bare"; const auto defaultElementCreator = boost::bind(&xmlpp::Element::add_child, _1, _2, Glib::ustring()); - class BadBooleanValue : public std::invalid_argument { - public: - BadBooleanValue(const Glib::ustring &) : - std::invalid_argument("Bad boolean value") - { - } - }; + BadBooleanValue::BadBooleanValue(const Glib::ustring &) : + std::invalid_argument("Bad boolean value") + { + } static const Glib::ustring TrueText("true"); static const Glib::ustring FalseText("false"); @@ -80,6 +77,10 @@ namespace Slicer { class XmlContentValueSource : public XmlValueSource { public: + XmlContentValueSource() : + XmlValueSource(Glib::ustring()) + { + } XmlContentValueSource(const xmlpp::ContentNode * c) : XmlValueSource(c->get_content()) { @@ -190,7 +191,13 @@ namespace Slicer { if (!attrs.empty()) { DocumentTreeIterate(attrs.front(), smp); } - DocumentTreeIterate(element->get_first_child(), smp); + auto firstChild = element->get_first_child(); + if (firstChild) { + DocumentTreeIterate(firstChild, smp); + } + else { + smp->SetValue(new XmlContentValueSource()); + } smp->Complete(); } } diff --git a/slicer/xml/serializer.h b/slicer/xml/serializer.h index f50435e..a57e846 100644 --- a/slicer/xml/serializer.h +++ b/slicer/xml/serializer.h @@ -6,6 +6,11 @@ #include namespace Slicer { + class BadBooleanValue : public std::invalid_argument { + public: + BadBooleanValue(const Glib::ustring &); + }; + class XmlSerializer : public Serializer { protected: typedef boost::function ElementCreator; diff --git a/slicer/xml/testSpecifics.cpp b/slicer/xml/testSpecifics.cpp new file mode 100644 index 0000000..20f65fe --- /dev/null +++ b/slicer/xml/testSpecifics.cpp @@ -0,0 +1,48 @@ +#define BOOST_TEST_MODULE db_insert +#include +#include +#include "serializer.h" +#include +#include + +template +T +BoostThrowWrapperHelper(P && ... p) +{ + return Slicer::DeserializeAny(p...); +} + +BOOST_AUTO_TEST_CASE( boolean_values ) +{ + xmlpp::DomParser doc; + doc.parse_memory("true"); + BOOST_REQUIRE_EQUAL(true, BoostThrowWrapperHelper(doc.get_document())); + doc.parse_memory("false"); + BOOST_REQUIRE_EQUAL(false, BoostThrowWrapperHelper(doc.get_document())); + doc.parse_memory("nonsense"); + BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), Slicer::BadBooleanValue); + doc.parse_memory(" "); + BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), Slicer::BadBooleanValue); + doc.parse_memory(""); + BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), Slicer::BadBooleanValue); +} + +BOOST_AUTO_TEST_CASE( int_values ) +{ + xmlpp::DomParser doc; + doc.parse_memory("13"); + BOOST_REQUIRE_EQUAL(13, BoostThrowWrapperHelper(doc.get_document())); + doc.parse_memory("84"); + BOOST_REQUIRE_EQUAL(84, BoostThrowWrapperHelper(doc.get_document())); + doc.parse_memory("0"); + BOOST_REQUIRE_EQUAL(0, BoostThrowWrapperHelper(doc.get_document())); + doc.parse_memory("-4"); + BOOST_REQUIRE_EQUAL(-4, BoostThrowWrapperHelper(doc.get_document())); + doc.parse_memory(" "); + BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), std::bad_cast); + doc.parse_memory("notanint"); + BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), std::bad_cast); + doc.parse_memory(""); + BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(doc.get_document()), std::bad_cast); +} + -- cgit v1.2.3