From e4d54b8dd2fcf73eb0a7f9968cd11fb75dcaadaf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Jul 2015 03:54:29 +0100 Subject: Add test cases over serialisation, fix output of boolean, fix random fixed with numbers --- libjsonpp/Jamfile.jam | 14 ++++++ libjsonpp/serialize.cpp | 4 +- libjsonpp/testSerialise.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 libjsonpp/testSerialise.cpp diff --git a/libjsonpp/Jamfile.jam b/libjsonpp/Jamfile.jam index cfbb48e..d97edea 100644 --- a/libjsonpp/Jamfile.jam +++ b/libjsonpp/Jamfile.jam @@ -41,4 +41,18 @@ run testParse ; +run + testSerialise.cpp + pch + : : : + ROOT=\"$(me)\" + BOOST_TEST_DYN_LINK + jsonpp + boost_utf + boost_system + boost_filesystem + : + testSerialise + ; + package.install install : . : : jsonpp : [ glob *.h ] ; diff --git a/libjsonpp/serialize.cpp b/libjsonpp/serialize.cpp index 7fde08e..b9456a7 100644 --- a/libjsonpp/serialize.cpp +++ b/libjsonpp/serialize.cpp @@ -1,6 +1,7 @@ #include #include "jsonpp.h" #include +#include namespace json { class JsonSerialize : public boost::static_visitor<> { @@ -128,11 +129,12 @@ namespace json { } void serializeNumber(const Number & n, std::ostream & s, const std::string & ) { + s.unsetf(std::ios::fixed); s << n; } void serializeBoolean(const Boolean & b, std::ostream & s, const std::string & ) { - s << b; + s << (b ? "true" : "false"); } void serializeNull(const Null &, std::ostream & s, const std::string & ) { diff --git a/libjsonpp/testSerialise.cpp b/libjsonpp/testSerialise.cpp new file mode 100644 index 0000000..98e9b41 --- /dev/null +++ b/libjsonpp/testSerialise.cpp @@ -0,0 +1,114 @@ +#include + +#define BOOST_TEST_MODULE serialise +#include + +#include "jsonpp.h" +#include +#include + +#define XSTR(s) STR(s) +#define STR(s) #s +const boost::filesystem::path root(XSTR(ROOT)); + +template +static +json::ValuePtr +jvp(const T & t) +{ + return json::ValuePtr(new json::Value(t)); +} + +static +std::string +writeString(const json::Value & v) +{ + std::stringstream ss; + json::serializeValue(v, ss, "utf-8"); + return ss.str(); +} + +BOOST_AUTO_TEST_CASE( serialise_true ) +{ + BOOST_REQUIRE_EQUAL("true", writeString(true)); +} + +BOOST_AUTO_TEST_CASE( serialise_false ) +{ + BOOST_REQUIRE_EQUAL("false", writeString(false)); +} + +BOOST_AUTO_TEST_CASE( serialise_0 ) +{ + BOOST_REQUIRE_EQUAL("0", writeString(0.0)); +} + +BOOST_AUTO_TEST_CASE( serialise_48 ) +{ + BOOST_REQUIRE_EQUAL("48", writeString(48.0)); +} + +BOOST_AUTO_TEST_CASE( serialise_neg48 ) +{ + BOOST_REQUIRE_EQUAL("-48", writeString(-48.0)); +} + +BOOST_AUTO_TEST_CASE( serialise_pi ) +{ + BOOST_REQUIRE_EQUAL("3.14159", writeString(3.14159)); +} + +BOOST_AUTO_TEST_CASE( serialise_emptyArray ) +{ + BOOST_REQUIRE_EQUAL("[]", writeString(json::Array())); +} + +BOOST_AUTO_TEST_CASE( serialise_emptyObject ) +{ + BOOST_REQUIRE_EQUAL("{}", writeString(json::Object())); +} + +BOOST_AUTO_TEST_CASE( serialise_null ) +{ + BOOST_REQUIRE_EQUAL("null", writeString(json::Null())); +} + +BOOST_AUTO_TEST_CASE( serialise_unicode ) +{ + BOOST_REQUIRE_EQUAL("\"A Űņĩćőđē string.\"", writeString(Glib::ustring("A Űņĩćőđē string."))); +} + +BOOST_AUTO_TEST_CASE( serialise_whitespace ) +{ + BOOST_REQUIRE_EQUAL("\"\\r\\n\\t\"", writeString(Glib::ustring("\r\n\t"))); +} + +BOOST_AUTO_TEST_CASE( serialise_quotes ) +{ + BOOST_REQUIRE_EQUAL("\"string with \\\" in\"", writeString(Glib::ustring("string with \" in"))); +} + +BOOST_AUTO_TEST_CASE( serialise_complexObject ) +{ + BOOST_REQUIRE_EQUAL("{\"a\":\"string\",\"b\":null,\"c\":64,\"d\":true,\"e\":{\"suba\":\"suba-val\",\"subb\":\"subb-val\"},\"f\":[true,false,-4.9,\"item\"]}", writeString(json::Object({ + {"a", jvp(Glib::ustring("string"))}, + {"b", jvp(json::Null())}, + {"c", jvp(64.0)}, + {"d", jvp(true)}, + {"e", jvp( + json::Object({ + {"suba", jvp(Glib::ustring("suba-val"))}, + {"subb", jvp(Glib::ustring("subb-val"))} + }) + )}, + {"f", jvp( + json::Array({ + jvp(true), + jvp(false), + jvp(-4.9), + jvp(Glib::ustring("item")) + }) + )}, + }))); +} + -- cgit v1.2.3