diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2020-05-31 18:52:30 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2020-06-19 18:58:09 +0100 |
commit | dda640890002b123c46f8c2389c97e836e5100a8 (patch) | |
tree | 33ee6dcd5c0ceca721ff70595bda2c74a47f6d74 | |
parent | Add a pkgconfig file (diff) | |
download | libjsonpp-0.12.1.tar.bz2 libjsonpp-0.12.1.tar.xz libjsonpp-0.12.1.zip |
Clang-format and cppchecklibjsonpp-0.12.1
-rw-r--r-- | libjsonpp/jsonFlexLexer.cpp | 4 | ||||
-rw-r--r-- | libjsonpp/jsonFlexLexer.h | 45 | ||||
-rw-r--r-- | libjsonpp/jsonValueFlexLexer.cpp | 3 | ||||
-rw-r--r-- | libjsonpp/jsonValueFlexLexer.h | 33 | ||||
-rw-r--r-- | libjsonpp/jsonpp.cpp | 1 | ||||
-rw-r--r-- | libjsonpp/jsonpp.h | 16 | ||||
-rw-r--r-- | libjsonpp/parse.cpp | 23 | ||||
-rw-r--r-- | libjsonpp/serialize.cpp | 225 | ||||
-rw-r--r-- | libjsonpp/testEncoding.cpp | 15 | ||||
-rw-r--r-- | libjsonpp/testParse.cpp | 69 | ||||
-rw-r--r-- | libjsonpp/testSerialise.cpp | 67 |
11 files changed, 255 insertions, 246 deletions
diff --git a/libjsonpp/jsonFlexLexer.cpp b/libjsonpp/jsonFlexLexer.cpp index df29d8f..b58c82f 100644 --- a/libjsonpp/jsonFlexLexer.cpp +++ b/libjsonpp/jsonFlexLexer.cpp @@ -3,8 +3,7 @@ namespace json { jsonFlexLexer::jsonFlexLexer(std::istream & in, std::string enc) : - yyFlexLexer(&in, nullptr), - encoding(enc != utf8 ? std::move(enc) : std::string()) + yyFlexLexer(&in, nullptr), encoding(enc != utf8 ? std::move(enc) : std::string()) { yy_push_state(0); } @@ -29,4 +28,3 @@ namespace json { { } } - diff --git a/libjsonpp/jsonFlexLexer.h b/libjsonpp/jsonFlexLexer.h index bbd6b95..eab59a6 100644 --- a/libjsonpp/jsonFlexLexer.h +++ b/libjsonpp/jsonFlexLexer.h @@ -1,47 +1,46 @@ #ifndef JSONFLEXLEXER_H #define JSONFLEXLEXER_H -#include <string> #include <stdexcept> +#include <string> #ifndef FLEX_SCANNER -#define yyFlexLexer jsonBaseFlexLexer -#include <FlexLexer.h> +# define yyFlexLexer jsonBaseFlexLexer +# include <FlexLexer.h> #endif namespace json { #pragma GCC visibility push(default) class ParseError : public std::invalid_argument { - public: - ParseError(const char *, int, int); + public: + ParseError(const char *, int, int); }; class jsonFlexLexer : public yyFlexLexer { - public: - jsonFlexLexer(std::istream &, std::string enc); + public: + jsonFlexLexer(std::istream &, std::string enc); - int yylex() override; + int yylex() override; - private: - virtual void BeginObject() = 0; - virtual void BeginArray() = 0; + private: + virtual void BeginObject() = 0; + virtual void BeginArray() = 0; - virtual void PushBoolean(bool) = 0; - virtual void PushNumber(double) = 0; - virtual void PushNull() = 0; - virtual void PushText(std::string &&) = 0; - virtual void PushKey(std::string &&) = 0; + virtual void PushBoolean(bool) = 0; + virtual void PushNumber(double) = 0; + virtual void PushNull() = 0; + virtual void PushText(std::string &&) = 0; + virtual void PushKey(std::string &&) = 0; - virtual void EndArray() = 0; - virtual void EndObject() = 0; + virtual void EndArray() = 0; + virtual void EndObject() = 0; - void LexerError(const char * msg) override; - std::string encodeBuf() const; + void LexerError(const char * msg) override; + std::string encodeBuf() const; - std::string buf; - const std::string encoding; + std::string buf; + const std::string encoding; }; #pragma GCC visibility pop } #endif - diff --git a/libjsonpp/jsonValueFlexLexer.cpp b/libjsonpp/jsonValueFlexLexer.cpp index 5225c95..68b0f5d 100644 --- a/libjsonpp/jsonValueFlexLexer.cpp +++ b/libjsonpp/jsonValueFlexLexer.cpp @@ -13,7 +13,7 @@ namespace json { jsonValueFlexLexer::BeginObject() { auto object = std::get_if<Object>(acceptValues.top()(Object())); - acceptValues.push([object,this](auto && value) { + acceptValues.push([object, this](auto && value) { return &object->emplace(std::move(name), std::forward<Value>(value)).first->second; }); } @@ -69,4 +69,3 @@ namespace json { acceptValues.pop(); } } - diff --git a/libjsonpp/jsonValueFlexLexer.h b/libjsonpp/jsonValueFlexLexer.h index 82f3d62..964674c 100644 --- a/libjsonpp/jsonValueFlexLexer.h +++ b/libjsonpp/jsonValueFlexLexer.h @@ -3,32 +3,31 @@ #include "jsonFlexLexer.h" #include "jsonpp.h" -#include <stack> #include <functional> +#include <stack> namespace json { class jsonValueFlexLexer : public jsonFlexLexer { - public: - jsonValueFlexLexer(std::istream &, std::string enc, Value & v); + public: + jsonValueFlexLexer(std::istream &, std::string enc, Value & v); - void BeginObject() override; - void BeginArray() override; + void BeginObject() override; + void BeginArray() override; - void PushBoolean(bool) override; - void PushNumber(double) override; - void PushNull() override; - void PushText(std::string &&) override; - void PushKey(std::string &&) override; + void PushBoolean(bool) override; + void PushNumber(double) override; + void PushNull() override; + void PushText(std::string &&) override; + void PushKey(std::string &&) override; - void EndArray() override; - void EndObject() override; + void EndArray() override; + void EndObject() override; - private: - using AcceptValue = std::function<Value *(Value &&)>; - std::stack<AcceptValue> acceptValues; - std::string name; + private: + using AcceptValue = std::function<Value *(Value &&)>; + std::stack<AcceptValue> acceptValues; + std::string name; }; } #endif - diff --git a/libjsonpp/jsonpp.cpp b/libjsonpp/jsonpp.cpp index db2ac63..66f7524 100644 --- a/libjsonpp/jsonpp.cpp +++ b/libjsonpp/jsonpp.cpp @@ -11,4 +11,3 @@ namespace json { static_assert(std::is_nothrow_move_assignable<Object>::value); static_assert(std::is_nothrow_move_assignable<Array>::value); } - diff --git a/libjsonpp/jsonpp.h b/libjsonpp/jsonpp.h index 8ad953d..4966827 100644 --- a/libjsonpp/jsonpp.h +++ b/libjsonpp/jsonpp.h @@ -1,11 +1,11 @@ #ifndef JSONPP_H #define JSONPP_H +#include "jsonFlexLexer.h" #include <glibmm/ustring.h> -#include <variant> #include <map> +#include <variant> #include <vector> -#include "jsonFlexLexer.h" namespace json { extern const std::string utf8; @@ -15,19 +15,20 @@ namespace json { using Number = double; using Boolean = bool; #pragma GCC visibility push(default) - class Null { }; + class Null { + }; class Object; class Array; using Value = std::variant<Null, String, Number, Object, Array, Boolean>; using M = std::map<std::string, Value, std::less<>>; class Object : public M { - public: - using M::M; + public: + using M::M; }; using A = std::vector<Value>; class Array : public A { - public: - using A::A; + public: + using A::A; }; Value parseValue(std::istream &); @@ -40,4 +41,3 @@ namespace json { } #endif - diff --git a/libjsonpp/parse.cpp b/libjsonpp/parse.cpp index ec6448b..6a4c4f1 100644 --- a/libjsonpp/parse.cpp +++ b/libjsonpp/parse.cpp @@ -1,27 +1,34 @@ -#include "jsonpp.h" #include "jsonValueFlexLexer.h" +#include "jsonpp.h" namespace json { - Value parseValue(std::istream & s) { + Value + parseValue(std::istream & s) + { return parseValue(s, std::string()); } - Value parseValue(std::istream & s, const std::string & enc) { + Value + parseValue(std::istream & s, const std::string & enc) + { Value v; jsonValueFlexLexer jfl(s, enc, v); - while (jfl.yylex()) {} + while (jfl.yylex()) { } return v; } - Value parseValue(const Glib::ustring & s) { + Value + parseValue(const Glib::ustring & s) + { std::stringstream stream(s); return parseValue(stream); } - Value parseValue(Glib::ustring::const_iterator & s) { + Value + parseValue(Glib::ustring::const_iterator & s) + { Glib::ustring::const_iterator start = s; - while (*s++) {} + while (*s++) { } return parseValue(Glib::ustring(start, --s)); } } - diff --git a/libjsonpp/serialize.cpp b/libjsonpp/serialize.cpp index cb3d203..31f066e 100644 --- a/libjsonpp/serialize.cpp +++ b/libjsonpp/serialize.cpp @@ -1,124 +1,145 @@ #include "jsonpp.h" #include <glibmm/convert.h> -#include <ios> #include <iomanip> +#include <ios> namespace json { class JsonSerialize { - public: - JsonSerialize(std::ostream & out, const std::string & encoding) : - s(out), - e(encoding) { - s << std::boolalpha // for Boolean - << std::defaultfloat // for Number - << std::setfill('0') // for String \uNNNN - ; - } - void operator()(const Value & v) const { - std::visit(*this, v); - } - void operator()(const std::string & str) const { - (*this)(str, e); + public: + JsonSerialize(std::ostream & out, const std::string & encoding) : s(out), e(encoding) + { + s << std::boolalpha // for Boolean + << std::defaultfloat // for Number + << std::setfill('0') // for String \uNNNN + ; + } + void + operator()(const Value & v) const + { + std::visit(*this, v); + } + void + operator()(const std::string & str) const + { + (*this)(str, e); + } + void + operator()(const String & str) const + { + (*this)(str, e); + } + void + operator()(const String & str, const std::string & enc) const + { + if (!enc.empty()) { + serializeString(Glib::convert(str, enc, utf8)); } - void operator()(const String & str) const { - (*this)(str, e); + else { + serializeString(str); } - void operator()(const String & str, const std::string & enc) const { - if (!enc.empty()) { - serializeString(Glib::convert(str, enc, utf8)); + } + void + serializeString(const String & str) const + { + s << '"'; + for (auto i = str.begin(); i != str.end();) { + const auto start = i; + while (i != str.end() && *i >= 32 && *i != '/' && *i != '"' && *i != '\\') { + i++; } - else { - serializeString(str); + if (start == str.begin() && i == str.end()) { + s << str.raw(); + break; } - } - void serializeString(const String & str) const { - s << '"'; - for (auto i = str.begin(); i != str.end(); ) { - const auto start = i; - while (i != str.end() && *i >= 32 && *i != '/' && *i != '"' && *i != '\\') { - i++; - } - if (start == str.begin() && i == str.end()) { - s << str.raw(); - break; - } - else if (start != i) { - s << Glib::ustring(start, i).raw(); - } - while (i != str.end() && (*i < 32 || *i == '/' || *i == '"' || *i == '\\')) { - const gunichar & c = *i; - switch (c) { - case '\f': - s << "\\f"; - break; - case '\t': - s << "\\t"; - break; - case '\n': - s << "\\n"; - break; - case '\b': - s << "\\b"; - break; - case '\r': - s << "\\r"; - break; - case '/': - s << "\\/"; - break; - case '\\': - s << "\\\\"; - break; - case '"': - s << "\\\""; - break; - default: - s << "\\u" << std::setw(4) << std::hex << c << std::setw(1); - break; - } - i++; - } + else if (start != i) { + s << Glib::ustring(start, i).raw(); } - s << '"'; - } - void operator()(const Number & n) const { - s << std::dec << n; - } - void operator()(const Array & a) const { - s << '['; - for (const Array::value_type & v : a) { - if (&v != &*a.begin()) { - s << ','; + while (i != str.end() && (*i < 32 || *i == '/' || *i == '"' || *i == '\\')) { + const gunichar & c = *i; + switch (c) { + case '\f': + s << "\\f"; + break; + case '\t': + s << "\\t"; + break; + case '\n': + s << "\\n"; + break; + case '\b': + s << "\\b"; + break; + case '\r': + s << "\\r"; + break; + case '/': + s << "\\/"; + break; + case '\\': + s << "\\\\"; + break; + case '"': + s << "\\\""; + break; + default: + s << "\\u" << std::setw(4) << std::hex << c << std::setw(1); + break; } - (*this)(v); + i++; } - s << ']'; } - void operator()(const Object & o) const { - s << '{'; - for (const Object::value_type & v : o) { - if (&v != &*o.begin()) { - s << ','; - } - (*this)(v.first); - s << ':'; - (*this)(v.second); + s << '"'; + } + void + operator()(const Number & n) const + { + s << std::dec << n; + } + void + operator()(const Array & a) const + { + s << '['; + for (const Array::value_type & v : a) { + if (&v != &*a.begin()) { + s << ','; } - s << '}'; + (*this)(v); } - void operator()(const Null &) const { - s << null; - } - void operator()(const Boolean & b) const { - s << b; + s << ']'; + } + void + operator()(const Object & o) const + { + s << '{'; + for (const Object::value_type & v : o) { + if (&v != &*o.begin()) { + s << ','; + } + (*this)(v.first); + s << ':'; + (*this)(v.second); } - private: - std::ostream & s; - const std::string & e; + s << '}'; + } + void + operator()(const Null &) const + { + s << null; + } + void + operator()(const Boolean & b) const + { + s << b; + } + + private: + std::ostream & s; + const std::string & e; }; - void serializeValue(const Value & v, std::ostream & s, const std::string & enc) { + void + serializeValue(const Value & v, std::ostream & s, const std::string & enc) + { JsonSerialize(s, enc)(v); } } - diff --git a/libjsonpp/testEncoding.cpp b/libjsonpp/testEncoding.cpp index 80efcb4..b92029a 100644 --- a/libjsonpp/testEncoding.cpp +++ b/libjsonpp/testEncoding.cpp @@ -2,15 +2,14 @@ #include <boost/test/unit_test.hpp> #include "jsonpp.h" -#include <fstream> #include <filesystem> +#include <fstream> #define XSTR(s) STR(s) #define STR(s) #s const std::filesystem::path root(XSTR(ROOT)); -static -std::string +static std::string writeString(const json::Value & v, const std::string & enc) { std::stringstream ss; @@ -18,23 +17,23 @@ writeString(const json::Value & v, const std::string & enc) return ss.str(); } -BOOST_AUTO_TEST_CASE( parse_latin1 ) +BOOST_AUTO_TEST_CASE(parse_latin1) { std::stringstream ss("\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\""); BOOST_REQUIRE_EQUAL(19, ss.str().length()); BOOST_REQUIRE_EQUAL("A ÙñîçôÐè string.", std::get<Glib::ustring>(json::parseValue(ss, "latin1"))); } -BOOST_AUTO_TEST_CASE( write_latin1 ) +BOOST_AUTO_TEST_CASE(write_latin1) { - BOOST_REQUIRE_EQUAL("\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\"", writeString(Glib::ustring("A ÙñîçôÐè string."), "latin1")); + BOOST_REQUIRE_EQUAL( + "\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\"", writeString(Glib::ustring("A ÙñîçôÐè string."), "latin1")); } -BOOST_AUTO_TEST_CASE( defaultEncoding ) +BOOST_AUTO_TEST_CASE(defaultEncoding) { json::String s("Some string value"); std::stringstream ss; json::serializeValue(s, ss, ""); BOOST_REQUIRE_EQUAL(ss.str(), R"S("Some string value")S"); } - diff --git a/libjsonpp/testParse.cpp b/libjsonpp/testParse.cpp index ab8083e..f9eaf92 100644 --- a/libjsonpp/testParse.cpp +++ b/libjsonpp/testParse.cpp @@ -2,56 +2,56 @@ #include <boost/test/unit_test.hpp> #include "jsonpp.h" -#include <fstream> #include <filesystem> +#include <fstream> #define XSTR(s) STR(s) #define STR(s) #s const std::filesystem::path root(XSTR(ROOT)); -BOOST_AUTO_TEST_CASE( parse_bool_true ) +BOOST_AUTO_TEST_CASE(parse_bool_true) { const Glib::ustring val(" true "); BOOST_REQUIRE_EQUAL(true, std::get<bool>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_bool_false ) +BOOST_AUTO_TEST_CASE(parse_bool_false) { const Glib::ustring val(" false "); BOOST_REQUIRE_EQUAL(false, std::get<bool>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_invalid_value ) +BOOST_AUTO_TEST_CASE(parse_invalid_value) { const Glib::ustring val(" meh "); - BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); + BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); } -BOOST_AUTO_TEST_CASE( parse_int_1 ) +BOOST_AUTO_TEST_CASE(parse_int_1) { const Glib::ustring val(" 1 "); BOOST_REQUIRE_EQUAL(1, std::get<json::Number>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_int_neg48 ) +BOOST_AUTO_TEST_CASE(parse_int_neg48) { const Glib::ustring val(" -48 "); BOOST_REQUIRE_EQUAL(-48, std::get<json::Number>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_int_48 ) +BOOST_AUTO_TEST_CASE(parse_int_48) { const Glib::ustring val(" 48 "); BOOST_REQUIRE_EQUAL(48, std::get<json::Number>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_float_pi ) +BOOST_AUTO_TEST_CASE(parse_float_pi) { const Glib::ustring val(" 3.14159265359 "); BOOST_REQUIRE_CLOSE(3.14159, std::get<json::Number>(json::parseValue(val)), 0.001); } -BOOST_AUTO_TEST_CASE( parse_array ) +BOOST_AUTO_TEST_CASE(parse_array) { const Glib::ustring val(" [ 1, 2, 3, [ 4, 5 ], {\"val1\": 6, \"val2\": [7, 8]}, 9, 10 ] "); auto arr = std::get<json::Array>(json::parseValue(val)); @@ -66,25 +66,25 @@ BOOST_AUTO_TEST_CASE( parse_array ) BOOST_REQUIRE_EQUAL(10, std::get<json::Number>(*itr++)); } -BOOST_AUTO_TEST_CASE( parse_array_of_strings ) +BOOST_AUTO_TEST_CASE(parse_array_of_strings) { const Glib::ustring val(" [ \"en\", \"de\", \"ro\", \"es\", \"fa\" ] "); std::get<json::Array>(json::parseValue(val)); } -BOOST_AUTO_TEST_CASE( parse_null ) +BOOST_AUTO_TEST_CASE(parse_null) { const Glib::ustring val(" null "); std::get<json::Null>(json::parseValue(val)); } -BOOST_AUTO_TEST_CASE( parse_empty_array ) +BOOST_AUTO_TEST_CASE(parse_empty_array) { const Glib::ustring val(" [ ] "); BOOST_REQUIRE(std::get<json::Array>(json::parseValue(val)).empty()); } -BOOST_AUTO_TEST_CASE( parse_empty_arrary_in_object ) +BOOST_AUTO_TEST_CASE(parse_empty_arrary_in_object) { const Glib::ustring val(" { \"v1\": [ ], \"v2\": 100 } "); auto value = json::parseValue(val); @@ -95,19 +95,19 @@ BOOST_AUTO_TEST_CASE( parse_empty_arrary_in_object ) BOOST_REQUIRE_EQUAL(100, std::get<json::Number>(obj["v2"])); } -BOOST_AUTO_TEST_CASE( parse_broken_array ) +BOOST_AUTO_TEST_CASE(parse_broken_array) { const Glib::ustring val(" [ 1, 2, ] "); - BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); + BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); } -BOOST_AUTO_TEST_CASE( parse_nonsense_in_array ) +BOOST_AUTO_TEST_CASE(parse_nonsense_in_array) { const Glib::ustring val(" [ 1, 2, nonsense ] "); - BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); + BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); } -BOOST_AUTO_TEST_CASE( parse_object ) +BOOST_AUTO_TEST_CASE(parse_object) { const Glib::ustring val(" { \"a\": 1, \"b\": 2 } "); auto value = json::parseValue(val); @@ -118,13 +118,13 @@ BOOST_AUTO_TEST_CASE( parse_object ) BOOST_REQUIRE_EQUAL(2, std::get<json::Number>(obj["b"])); } -BOOST_AUTO_TEST_CASE( parse_string_simple ) +BOOST_AUTO_TEST_CASE(parse_string_simple) { const Glib::ustring val(" \"simple string\" "); BOOST_REQUIRE_EQUAL("simple string", std::get<json::String>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_object_withStringContainingQuote ) +BOOST_AUTO_TEST_CASE(parse_object_withStringContainingQuote) { const Glib::ustring val(" { \"key1\": \"value1\", \"key2\": \"value\\\"2\\\"\", \"key3\": 3 } "); auto obj = std::get<json::Object>(json::parseValue(val)); @@ -134,67 +134,69 @@ BOOST_AUTO_TEST_CASE( parse_object_withStringContainingQuote ) BOOST_REQUIRE_EQUAL(3, std::get<json::Number>(obj["key3"])); } -BOOST_AUTO_TEST_CASE( parse_string_invalid_missingOpeningQuote ) +BOOST_AUTO_TEST_CASE(parse_string_invalid_missingOpeningQuote) { const Glib::ustring val(" simple string\" "); BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); } -BOOST_AUTO_TEST_CASE( parse_string_escapedQuote ) +BOOST_AUTO_TEST_CASE(parse_string_escapedQuote) { const Glib::ustring val(" \"A \\\"quoted\\\" string.\" "); BOOST_REQUIRE_EQUAL("A \"quoted\" string.", std::get<json::String>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_string_escapedWhitespace ) +BOOST_AUTO_TEST_CASE(parse_string_escapedWhitespace) { const Glib::ustring val(" \"A whitespace\\t\\r\\n\\b\\f string.\" "); BOOST_REQUIRE_EQUAL("A whitespace\t\r\n\b\f string.", std::get<json::String>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_string_escapedSlashes ) +BOOST_AUTO_TEST_CASE(parse_string_escapedSlashes) { const Glib::ustring val(" \"A whitespace\\\\ \\/ string.\" "); BOOST_REQUIRE_EQUAL("A whitespace\\ / string.", std::get<json::String>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_string_literalUnicode ) +BOOST_AUTO_TEST_CASE(parse_string_literalUnicode) { const Glib::ustring val(" \"A Űņĩćőđē string.\" "); BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", std::get<json::String>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_string_unknownEscape ) +BOOST_AUTO_TEST_CASE(parse_string_unknownEscape) { const Glib::ustring val(" \"A \\z string.\" "); BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); } -BOOST_AUTO_TEST_CASE( parse_string_shortUnicodeEscape ) +BOOST_AUTO_TEST_CASE(parse_string_shortUnicodeEscape) { const Glib::ustring val(" \"A \\u017 string.\" "); BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); } -BOOST_AUTO_TEST_CASE( parse_string_escapedUnicode ) +BOOST_AUTO_TEST_CASE(parse_string_escapedUnicode) { const Glib::ustring val(" \"A \\u0170\\u0146\\u0129\\u0107\\u0151\\u0111\\u0113 string.\" "); BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", std::get<json::String>(json::parseValue(val))); } -BOOST_AUTO_TEST_CASE( parse_sample_complexFile1 ) +BOOST_AUTO_TEST_CASE(parse_sample_complexFile1) { std::ifstream inFile((root / "initial" / "sample1.json").string()); json::Value obj = json::parseValue(inFile, "utf-8"); + BOOST_CHECK_EQUAL(obj.index(), 3); } -BOOST_AUTO_TEST_CASE( parse_sample_complexFile2 ) +BOOST_AUTO_TEST_CASE(parse_sample_complexFile2) { std::ifstream inFile((root / "initial" / "sample2.json").string()); json::Value obj = json::parseValue(inFile, "utf-8"); + BOOST_CHECK_EQUAL(obj.index(), 3); } -BOOST_AUTO_TEST_CASE( parse_sample_complexFile2_bulk, * boost::unit_test::disabled() ) +BOOST_AUTO_TEST_CASE(parse_sample_complexFile2_bulk, *boost::unit_test::disabled()) { for (int x = 0; x < 100; x++) { std::ifstream inFile((root / "initial" / "sample2.json").string()); @@ -202,10 +204,9 @@ BOOST_AUTO_TEST_CASE( parse_sample_complexFile2_bulk, * boost::unit_test::disabl } } -BOOST_AUTO_TEST_CASE( parse_from_itr ) +BOOST_AUTO_TEST_CASE(parse_from_itr) { const Glib::ustring val(" \"A \\u0170\\u0146\\u0129\\u0107\\u0151\\u0111\\u0113 string.\" "); auto itr = val.begin(); BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", std::get<json::String>(json::parseValue(itr))); } - diff --git a/libjsonpp/testSerialise.cpp b/libjsonpp/testSerialise.cpp index 05ddce4..1fb0381 100644 --- a/libjsonpp/testSerialise.cpp +++ b/libjsonpp/testSerialise.cpp @@ -2,8 +2,8 @@ #include <boost/test/unit_test.hpp> #include "jsonpp.h" -#include <fstream> #include <filesystem> +#include <fstream> #define XSTR(s) STR(s) #define STR(s) #s @@ -11,8 +11,7 @@ const std::filesystem::path root(XSTR(ROOT)); using namespace std::literals; -static -std::string +static std::string writeString(const json::Value & v) { std::stringstream ss; @@ -20,101 +19,89 @@ writeString(const json::Value & v) return ss.str(); } -BOOST_AUTO_TEST_CASE( serialise_true ) +BOOST_AUTO_TEST_CASE(serialise_true) { BOOST_REQUIRE_EQUAL("true", writeString(true)); } -BOOST_AUTO_TEST_CASE( serialise_false ) +BOOST_AUTO_TEST_CASE(serialise_false) { BOOST_REQUIRE_EQUAL("false", writeString(false)); } -BOOST_AUTO_TEST_CASE( serialise_0 ) +BOOST_AUTO_TEST_CASE(serialise_0) { BOOST_REQUIRE_EQUAL("0", writeString(0.0)); } -BOOST_AUTO_TEST_CASE( serialise_48 ) +BOOST_AUTO_TEST_CASE(serialise_48) { BOOST_REQUIRE_EQUAL("48", writeString(48.0)); } -BOOST_AUTO_TEST_CASE( serialise_neg48 ) +BOOST_AUTO_TEST_CASE(serialise_neg48) { BOOST_REQUIRE_EQUAL("-48", writeString(-48.0)); } -BOOST_AUTO_TEST_CASE( serialise_pi ) +BOOST_AUTO_TEST_CASE(serialise_pi) { BOOST_REQUIRE_EQUAL("3.14159", writeString(3.14159)); } -BOOST_AUTO_TEST_CASE( serialise_emptyArray ) +BOOST_AUTO_TEST_CASE(serialise_emptyArray) { BOOST_REQUIRE_EQUAL("[]", writeString(json::Array())); } -BOOST_AUTO_TEST_CASE( serialise_emptyObject ) +BOOST_AUTO_TEST_CASE(serialise_emptyObject) { BOOST_REQUIRE_EQUAL("{}", writeString(json::Object())); } -BOOST_AUTO_TEST_CASE( serialise_null ) +BOOST_AUTO_TEST_CASE(serialise_null) { BOOST_REQUIRE_EQUAL("null", writeString(json::Null())); } -BOOST_AUTO_TEST_CASE( serialise_unicode ) +BOOST_AUTO_TEST_CASE(serialise_unicode) { BOOST_REQUIRE_EQUAL("\"A Űņĩćőđē string.\"", writeString(Glib::ustring("A Űņĩćőđē string."))); } -BOOST_AUTO_TEST_CASE( serialise_whitespace ) +BOOST_AUTO_TEST_CASE(serialise_whitespace) { BOOST_REQUIRE_EQUAL("\"\\r\\n\\t\"", writeString(Glib::ustring("\r\n\t"))); } -BOOST_AUTO_TEST_CASE( serialise_control ) +BOOST_AUTO_TEST_CASE(serialise_control) { BOOST_REQUIRE_EQUAL("\"\\b\\f\"", writeString(Glib::ustring("\b\f"))); } -BOOST_AUTO_TEST_CASE( serialise_slashes ) +BOOST_AUTO_TEST_CASE(serialise_slashes) { BOOST_REQUIRE_EQUAL("\"\\\\\\/\"", writeString(Glib::ustring("\\/"))); } -BOOST_AUTO_TEST_CASE( serialise_other ) +BOOST_AUTO_TEST_CASE(serialise_other) { BOOST_REQUIRE_EQUAL("\"\\u000b\\u0007\"", writeString(Glib::ustring("\v\a"))); } -BOOST_AUTO_TEST_CASE( serialise_quotes ) +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", "string"s}, - {"b", json::Null() }, - {"c", 64.0}, - {"d", true}, - {"e", json::Object { - {"suba", "suba-val"s}, - {"subb", "subb-val"s} - } - }, - {"f", json::Array { - true, - false, - -4.9, - "item"s - }, - } - })); +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", "string"s}, {"b", json::Null()}, {"c", 64.0}, {"d", true}, + {"e", json::Object {{"suba", "suba-val"s}, {"subb", "subb-val"s}}}, + { + "f", + json::Array {true, false, -4.9, "item"s}, + }})); } - |