From 72446eb7ff43136533f4eaa4be844313bcc0c9ee Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Jul 2015 02:57:40 +0100 Subject: Rename test cases, parsing specific --- libjsonpp/Jamfile.jam | 4 +- libjsonpp/test1.cpp | 205 ------------------------------------------------ libjsonpp/testParse.cpp | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+), 207 deletions(-) delete mode 100644 libjsonpp/test1.cpp create mode 100644 libjsonpp/testParse.cpp diff --git a/libjsonpp/Jamfile.jam b/libjsonpp/Jamfile.jam index 1f01b09..cfbb48e 100644 --- a/libjsonpp/Jamfile.jam +++ b/libjsonpp/Jamfile.jam @@ -28,7 +28,7 @@ lib jsonpp : ; run - test1.cpp + testParse.cpp pch : : : ROOT=\"$(me)\" @@ -38,7 +38,7 @@ run boost_system boost_filesystem : - test1 + testParse ; package.install install : . : : jsonpp : [ glob *.h ] ; diff --git a/libjsonpp/test1.cpp b/libjsonpp/test1.cpp deleted file mode 100644 index fd98c44..0000000 --- a/libjsonpp/test1.cpp +++ /dev/null @@ -1,205 +0,0 @@ -#include - -#define BOOST_TEST_MODULE parsing -#include - -#include "jsonpp.h" -#include -#include - -#define XSTR(s) STR(s) -#define STR(s) #s -const boost::filesystem::path root(XSTR(ROOT)); - -BOOST_AUTO_TEST_CASE( parse_bool_true ) -{ - const Glib::ustring val(" true "); - BOOST_REQUIRE_EQUAL(true, boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_bool_false ) -{ - const Glib::ustring val(" false "); - BOOST_REQUIRE_EQUAL(false, boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_invalid_value ) -{ - const Glib::ustring val(" meh "); - BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); -} - -BOOST_AUTO_TEST_CASE( parse_int_1 ) -{ - const Glib::ustring val(" 1 "); - BOOST_REQUIRE_EQUAL(1, boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_int_neg48 ) -{ - const Glib::ustring val(" -48 "); - BOOST_REQUIRE_EQUAL(-48, boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_int_48 ) -{ - const Glib::ustring val(" 48 "); - BOOST_REQUIRE_EQUAL(48, boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_float_pi ) -{ - const Glib::ustring val(" 3.14159265359 "); - BOOST_REQUIRE_CLOSE(3.14159, boost::get(json::parseValue(val)), 0.001); -} - -BOOST_AUTO_TEST_CASE( parse_array ) -{ - const Glib::ustring val(" [ 1, 2, 3, [ 4, 5 ], {\"val1\": 6, \"val2\": [7, 8]}, 9, 10 ] "); - auto arr = boost::get(json::parseValue(val)); - BOOST_REQUIRE_EQUAL(7, arr.size()); - auto itr = arr.begin(); - BOOST_REQUIRE_EQUAL(1, boost::get(**itr++)); - BOOST_REQUIRE_EQUAL(2, boost::get(**itr++)); - BOOST_REQUIRE_EQUAL(3, boost::get(**itr++)); - boost::get(**itr++); - boost::get(**itr++); - BOOST_REQUIRE_EQUAL(9, boost::get(**itr++)); - BOOST_REQUIRE_EQUAL(10, boost::get(**itr++)); -} - -BOOST_AUTO_TEST_CASE( parse_array_of_strings ) -{ - const Glib::ustring val(" [ \"en\", \"de\", \"ro\", \"es\", \"fa\" ] "); - boost::get(json::parseValue(val)); -} - -BOOST_AUTO_TEST_CASE( parse_null ) -{ - const Glib::ustring val(" null "); - boost::get(json::parseValue(val)); -} - -BOOST_AUTO_TEST_CASE( parse_empty_array ) -{ - const Glib::ustring val(" [ ] "); - BOOST_REQUIRE(boost::get(json::parseValue(val)).empty()); -} - -BOOST_AUTO_TEST_CASE( parse_empty_arrary_in_object ) -{ - const Glib::ustring val(" { \"v1\": [ ], \"v2\": 100 } "); - auto value = json::parseValue(val); - BOOST_REQUIRE_EQUAL(3, value.which()); - auto obj = boost::get(value); - BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE(boost::get(*obj["v1"]).empty()); - BOOST_REQUIRE_EQUAL(100, boost::get(*obj["v2"])); -} - -BOOST_AUTO_TEST_CASE( parse_broken_array ) -{ - const Glib::ustring val(" [ 1, 2, ] "); - BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); -} - -BOOST_AUTO_TEST_CASE( parse_nonsense_in_array ) -{ - const Glib::ustring val(" [ 1, 2, nonsense ] "); - BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); -} - -BOOST_AUTO_TEST_CASE( parse_object ) -{ - const Glib::ustring val(" { \"a\": 1, \"b\": 2 } "); - auto value = json::parseValue(val); - BOOST_REQUIRE_EQUAL(3, value.which()); - auto obj = boost::get(value); - BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE_EQUAL(1, boost::get(*obj["a"])); - BOOST_REQUIRE_EQUAL(2, boost::get(*obj["b"])); -} - -BOOST_AUTO_TEST_CASE( parse_string_simple ) -{ - const Glib::ustring val(" \"simple string\" "); - BOOST_REQUIRE_EQUAL("simple string", boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_object_withStringContainingQuote ) -{ - const Glib::ustring val(" { \"key1\": \"value1\", \"key2\": \"value\\\"2\\\"\", \"key3\": 3 } "); - auto obj = json::parseObject(val); - BOOST_REQUIRE_EQUAL(3, obj.size()); - BOOST_REQUIRE_EQUAL("value1", boost::get(*obj["key1"])); - BOOST_REQUIRE_EQUAL("value\"2\"", boost::get(*obj["key2"])); - BOOST_REQUIRE_EQUAL(3, boost::get(*obj["key3"])); -} - -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 ) -{ - const Glib::ustring val(" \"A \\\"quoted\\\" string.\" "); - BOOST_REQUIRE_EQUAL("A \"quoted\" string.", boost::get(json::parseValue(val))); -} - -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.", boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_string_escapedSlashes ) -{ - const Glib::ustring val(" \"A whitespace\\\\ \\/ string.\" "); - BOOST_REQUIRE_EQUAL("A whitespace\\ / string.", boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_string_literalUnicode ) -{ - const Glib::ustring val(" \"A Űņĩćőđē string.\" "); - BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", boost::get(json::parseValue(val))); -} - -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 ) -{ - const Glib::ustring val(" \"A \\u017 string.\" "); - BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); -} - -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.", boost::get(json::parseValue(val))); -} - -BOOST_AUTO_TEST_CASE( parse_sample_complexFile1 ) -{ - std::ifstream inFile((root / "initial" / "sample1.json").string()); - json::Value obj = json::parseValue(inFile, "utf-8"); -} - -BOOST_AUTO_TEST_CASE( parse_sample_complexFile2 ) -{ - std::ifstream inFile((root / "initial" / "sample2.json").string()); - json::Value obj = json::parseValue(inFile, "utf-8"); -} - -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.", boost::get(json::parseValue(itr))); -} - diff --git a/libjsonpp/testParse.cpp b/libjsonpp/testParse.cpp new file mode 100644 index 0000000..fd98c44 --- /dev/null +++ b/libjsonpp/testParse.cpp @@ -0,0 +1,205 @@ +#include + +#define BOOST_TEST_MODULE parsing +#include + +#include "jsonpp.h" +#include +#include + +#define XSTR(s) STR(s) +#define STR(s) #s +const boost::filesystem::path root(XSTR(ROOT)); + +BOOST_AUTO_TEST_CASE( parse_bool_true ) +{ + const Glib::ustring val(" true "); + BOOST_REQUIRE_EQUAL(true, boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_bool_false ) +{ + const Glib::ustring val(" false "); + BOOST_REQUIRE_EQUAL(false, boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_invalid_value ) +{ + const Glib::ustring val(" meh "); + BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); +} + +BOOST_AUTO_TEST_CASE( parse_int_1 ) +{ + const Glib::ustring val(" 1 "); + BOOST_REQUIRE_EQUAL(1, boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_int_neg48 ) +{ + const Glib::ustring val(" -48 "); + BOOST_REQUIRE_EQUAL(-48, boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_int_48 ) +{ + const Glib::ustring val(" 48 "); + BOOST_REQUIRE_EQUAL(48, boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_float_pi ) +{ + const Glib::ustring val(" 3.14159265359 "); + BOOST_REQUIRE_CLOSE(3.14159, boost::get(json::parseValue(val)), 0.001); +} + +BOOST_AUTO_TEST_CASE( parse_array ) +{ + const Glib::ustring val(" [ 1, 2, 3, [ 4, 5 ], {\"val1\": 6, \"val2\": [7, 8]}, 9, 10 ] "); + auto arr = boost::get(json::parseValue(val)); + BOOST_REQUIRE_EQUAL(7, arr.size()); + auto itr = arr.begin(); + BOOST_REQUIRE_EQUAL(1, boost::get(**itr++)); + BOOST_REQUIRE_EQUAL(2, boost::get(**itr++)); + BOOST_REQUIRE_EQUAL(3, boost::get(**itr++)); + boost::get(**itr++); + boost::get(**itr++); + BOOST_REQUIRE_EQUAL(9, boost::get(**itr++)); + BOOST_REQUIRE_EQUAL(10, boost::get(**itr++)); +} + +BOOST_AUTO_TEST_CASE( parse_array_of_strings ) +{ + const Glib::ustring val(" [ \"en\", \"de\", \"ro\", \"es\", \"fa\" ] "); + boost::get(json::parseValue(val)); +} + +BOOST_AUTO_TEST_CASE( parse_null ) +{ + const Glib::ustring val(" null "); + boost::get(json::parseValue(val)); +} + +BOOST_AUTO_TEST_CASE( parse_empty_array ) +{ + const Glib::ustring val(" [ ] "); + BOOST_REQUIRE(boost::get(json::parseValue(val)).empty()); +} + +BOOST_AUTO_TEST_CASE( parse_empty_arrary_in_object ) +{ + const Glib::ustring val(" { \"v1\": [ ], \"v2\": 100 } "); + auto value = json::parseValue(val); + BOOST_REQUIRE_EQUAL(3, value.which()); + auto obj = boost::get(value); + BOOST_REQUIRE_EQUAL(2, obj.size()); + BOOST_REQUIRE(boost::get(*obj["v1"]).empty()); + BOOST_REQUIRE_EQUAL(100, boost::get(*obj["v2"])); +} + +BOOST_AUTO_TEST_CASE( parse_broken_array ) +{ + const Glib::ustring val(" [ 1, 2, ] "); + BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); +} + +BOOST_AUTO_TEST_CASE( parse_nonsense_in_array ) +{ + const Glib::ustring val(" [ 1, 2, nonsense ] "); + BOOST_CHECK_THROW(json::parseValue(val), json::ParseError ); +} + +BOOST_AUTO_TEST_CASE( parse_object ) +{ + const Glib::ustring val(" { \"a\": 1, \"b\": 2 } "); + auto value = json::parseValue(val); + BOOST_REQUIRE_EQUAL(3, value.which()); + auto obj = boost::get(value); + BOOST_REQUIRE_EQUAL(2, obj.size()); + BOOST_REQUIRE_EQUAL(1, boost::get(*obj["a"])); + BOOST_REQUIRE_EQUAL(2, boost::get(*obj["b"])); +} + +BOOST_AUTO_TEST_CASE( parse_string_simple ) +{ + const Glib::ustring val(" \"simple string\" "); + BOOST_REQUIRE_EQUAL("simple string", boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_object_withStringContainingQuote ) +{ + const Glib::ustring val(" { \"key1\": \"value1\", \"key2\": \"value\\\"2\\\"\", \"key3\": 3 } "); + auto obj = json::parseObject(val); + BOOST_REQUIRE_EQUAL(3, obj.size()); + BOOST_REQUIRE_EQUAL("value1", boost::get(*obj["key1"])); + BOOST_REQUIRE_EQUAL("value\"2\"", boost::get(*obj["key2"])); + BOOST_REQUIRE_EQUAL(3, boost::get(*obj["key3"])); +} + +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 ) +{ + const Glib::ustring val(" \"A \\\"quoted\\\" string.\" "); + BOOST_REQUIRE_EQUAL("A \"quoted\" string.", boost::get(json::parseValue(val))); +} + +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.", boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_string_escapedSlashes ) +{ + const Glib::ustring val(" \"A whitespace\\\\ \\/ string.\" "); + BOOST_REQUIRE_EQUAL("A whitespace\\ / string.", boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_string_literalUnicode ) +{ + const Glib::ustring val(" \"A Űņĩćőđē string.\" "); + BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", boost::get(json::parseValue(val))); +} + +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 ) +{ + const Glib::ustring val(" \"A \\u017 string.\" "); + BOOST_CHECK_THROW(json::parseValue(val), json::ParseError); +} + +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.", boost::get(json::parseValue(val))); +} + +BOOST_AUTO_TEST_CASE( parse_sample_complexFile1 ) +{ + std::ifstream inFile((root / "initial" / "sample1.json").string()); + json::Value obj = json::parseValue(inFile, "utf-8"); +} + +BOOST_AUTO_TEST_CASE( parse_sample_complexFile2 ) +{ + std::ifstream inFile((root / "initial" / "sample2.json").string()); + json::Value obj = json::parseValue(inFile, "utf-8"); +} + +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.", boost::get(json::parseValue(itr))); +} + -- cgit v1.2.3