From ea94518a6453ec610ebfff83f27c0b31196fb5e5 Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 28 Dec 2014 17:09:38 +0100 Subject: [PATCH] + more test cases --- src/JSON.cc | 4 +-- src/JSON.h | 4 +-- test/JSON_unit.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/JSON.cc b/src/JSON.cc index b3d7c9d2..6a98e823 100644 --- a/src/JSON.cc +++ b/src/JSON.cc @@ -285,7 +285,7 @@ JSON::~JSON() noexcept @param s a string representation of a JSON object @return a JSON object */ -JSON JSON::parse(std::string& s) +JSON JSON::parse(const std::string& s) { JSON j; Parser(s).parse(j); @@ -1892,7 +1892,7 @@ JSON::Parser::Parser(const char* s) /*! @copydoc JSON::Parser::Parser(const char* s) */ -JSON::Parser::Parser(std::string& s) +JSON::Parser::Parser(const std::string& s) : _length(s.length()), _buffer(new char[_length + 1]) { std::strcpy(_buffer, s.c_str()); diff --git a/src/JSON.h b/src/JSON.h index e6693cc9..5353baa7 100644 --- a/src/JSON.h +++ b/src/JSON.h @@ -139,7 +139,7 @@ class JSON ~JSON() noexcept; /// create from string representation - static JSON parse(std::string&); + static JSON parse(const std::string&); /// create from string representation static JSON parse(const char*); @@ -387,7 +387,7 @@ class JSON /// a parser reading from a C string Parser(const char*); /// a parser reading from a C++ string - Parser(std::string&); + Parser(const std::string&); /// a parser reading from an input stream Parser(std::istream&); /// destructor of the parser diff --git a/test/JSON_unit.cc b/test/JSON_unit.cc index d4edf8aa..124b58da 100644 --- a/test/JSON_unit.cc +++ b/test/JSON_unit.cc @@ -17,6 +17,10 @@ TEST_CASE("array") // string representation of default value CHECK(j.toString() == "[]"); + // iterators + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + // check payload //CHECK(*(j.data().array) == JSON::array_t()); //CHECK(*(j_const.data().array) == JSON::array_t()); @@ -298,6 +302,10 @@ TEST_CASE("object") // string representation of default value CHECK(j.toString() == "{}"); + // iterators + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + // check payload //CHECK(*(j.data().object) == JSON::object_t()); //CHECK(*(j_const.data().object) == JSON::object_t()); @@ -678,6 +686,10 @@ TEST_CASE("null") // string representation of default value CHECK(j.toString() == "null"); + // iterators + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + // container members CHECK(j.size() == 0); CHECK(j.empty() == true); @@ -737,6 +749,10 @@ TEST_CASE("string") // const object const JSON j_const = j; + // iterators + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + // string representation of default value CHECK(j.toString() == "\"\""); @@ -819,6 +835,10 @@ TEST_CASE("boolean") // const object const JSON j_const = j; + // iterators + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + // string representation of default value CHECK(j.toString() == "false"); @@ -898,6 +918,10 @@ TEST_CASE("number (int)") // const object const JSON j_const = j; + // iterators + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + // string representation of default value CHECK(j.toString() == "0"); @@ -984,6 +1008,10 @@ TEST_CASE("number (float)") // const object const JSON j_const = j; + // iterators + CHECK(j.begin() != j.end()); + CHECK(j.cbegin() != j.cend()); + // string representation of default value CHECK(j.toString() == "0.000000"); @@ -1060,6 +1088,7 @@ TEST_CASE("Comparisons") JSON j4 = nullptr; JSON j5 = 42; JSON j6 = 23.42; + JSON j7 = "hello"; CHECK((j1 == j1) == true); CHECK((j1 == j2) == false); @@ -1067,6 +1096,7 @@ TEST_CASE("Comparisons") CHECK((j1 == j4) == false); CHECK((j1 == j5) == false); CHECK((j1 == j6) == false); + CHECK((j1 == j7) == false); CHECK((j2 == j1) == false); CHECK((j2 == j2) == true); @@ -1074,6 +1104,7 @@ TEST_CASE("Comparisons") CHECK((j2 == j4) == false); CHECK((j2 == j5) == false); CHECK((j2 == j6) == false); + CHECK((j2 == j7) == false); CHECK((j3 == j1) == false); CHECK((j3 == j2) == false); @@ -1081,6 +1112,7 @@ TEST_CASE("Comparisons") CHECK((j3 == j4) == false); CHECK((j3 == j5) == false); CHECK((j3 == j6) == false); + CHECK((j3 == j7) == false); CHECK((j4 == j1) == false); CHECK((j4 == j2) == false); @@ -1088,6 +1120,7 @@ TEST_CASE("Comparisons") CHECK((j4 == j4) == true); CHECK((j4 == j5) == false); CHECK((j4 == j6) == false); + CHECK((j4 == j7) == false); CHECK((j5 == j1) == false); CHECK((j5 == j2) == false); @@ -1095,6 +1128,7 @@ TEST_CASE("Comparisons") CHECK((j5 == j4) == false); CHECK((j5 == j5) == true); CHECK((j5 == j6) == false); + CHECK((j5 == j7) == false); CHECK((j6 == j1) == false); CHECK((j6 == j2) == false); @@ -1102,6 +1136,15 @@ TEST_CASE("Comparisons") CHECK((j6 == j4) == false); CHECK((j6 == j5) == false); CHECK((j6 == j6) == true); + CHECK((j6 == j7) == false); + + CHECK((j7 == j1) == false); + CHECK((j7 == j2) == false); + CHECK((j7 == j3) == false); + CHECK((j7 == j4) == false); + CHECK((j7 == j5) == false); + CHECK((j7 == j6) == false); + CHECK((j7 == j7) == true); CHECK((j1 != j1) == false); CHECK((j1 != j2) == true); @@ -1109,6 +1152,7 @@ TEST_CASE("Comparisons") CHECK((j1 != j4) == true); CHECK((j1 != j5) == true); CHECK((j1 != j6) == true); + CHECK((j1 != j7) == true); CHECK((j2 != j1) == true); CHECK((j2 != j2) == false); @@ -1116,6 +1160,7 @@ TEST_CASE("Comparisons") CHECK((j2 != j4) == true); CHECK((j2 != j5) == true); CHECK((j2 != j6) == true); + CHECK((j2 != j7) == true); CHECK((j3 != j1) == true); CHECK((j3 != j2) == true); @@ -1123,6 +1168,7 @@ TEST_CASE("Comparisons") CHECK((j3 != j4) == true); CHECK((j3 != j5) == true); CHECK((j3 != j6) == true); + CHECK((j3 != j7) == true); CHECK((j4 != j1) == true); CHECK((j4 != j2) == true); @@ -1130,6 +1176,7 @@ TEST_CASE("Comparisons") CHECK((j4 != j4) == false); CHECK((j4 != j5) == true); CHECK((j4 != j6) == true); + CHECK((j4 != j7) == true); CHECK((j5 != j1) == true); CHECK((j5 != j2) == true); @@ -1137,6 +1184,7 @@ TEST_CASE("Comparisons") CHECK((j5 != j4) == true); CHECK((j5 != j5) == false); CHECK((j5 != j6) == true); + CHECK((j5 != j7) == true); CHECK((j6 != j1) == true); CHECK((j6 != j2) == true); @@ -1144,6 +1192,15 @@ TEST_CASE("Comparisons") CHECK((j6 != j4) == true); CHECK((j6 != j5) == true); CHECK((j6 != j6) == false); + CHECK((j6 != j7) == true); + + CHECK((j7 != j1) == true); + CHECK((j7 != j2) == true); + CHECK((j7 != j3) == true); + CHECK((j7 != j4) == true); + CHECK((j7 != j5) == true); + CHECK((j7 != j6) == true); + CHECK((j7 != j7) == false); } TEST_CASE("Parser") @@ -1173,6 +1230,10 @@ TEST_CASE("Parser") CHECK(JSON::parse("\"\"") == JSON("")); CHECK(JSON::parse("\"foo\"") == JSON("foo")); + // escape characters + CHECK_THROWS_AS(JSON::parse("\"\\\""), std::invalid_argument); + CHECK_NOTHROW(JSON::parse("\"\\\"\"")); + // quotes must be closed CHECK_THROWS_AS(JSON::parse("\""), std::invalid_argument); } @@ -1299,4 +1360,10 @@ TEST_CASE("Parser") auto j3 = "{\"key\": \"value\"}"_json; CHECK(j3["key"] == "value"); } + + SECTION("Errors") + { + CHECK_THROWS_AS(JSON::parse(""), std::invalid_argument); + CHECK_THROWS_AS(JSON::parse(std::string("")), std::invalid_argument); + } }