From b434d26a5d2c9a9e1b8d12e410e94832c384e230 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 30 Sep 2015 14:24:56 -0600 Subject: [PATCH] Add json tests --- include/chaiscript/utility/json.hpp | 36 ++++++++++++----------------- unittests/json_1.chai | 3 +++ unittests/json_10.chai | 1 + unittests/json_11.chai | 14 +++++++++++ unittests/json_12.chai | 1 + unittests/json_13.chai | 1 + unittests/json_2.chai | 3 +++ unittests/json_3.chai | 1 + unittests/json_4.chai | 1 + unittests/json_5.chai | 1 + unittests/json_6.chai | 1 + unittests/json_7.chai | 4 ++++ unittests/json_8.chai | 1 + unittests/json_9.chai | 2 ++ 14 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 unittests/json_1.chai create mode 100644 unittests/json_10.chai create mode 100644 unittests/json_11.chai create mode 100644 unittests/json_12.chai create mode 100644 unittests/json_13.chai create mode 100644 unittests/json_2.chai create mode 100644 unittests/json_3.chai create mode 100644 unittests/json_4.chai create mode 100644 unittests/json_5.chai create mode 100644 unittests/json_6.chai create mode 100644 unittests/json_7.chai create mode 100644 unittests/json_8.chai create mode 100644 unittests/json_9.chai diff --git a/include/chaiscript/utility/json.hpp b/include/chaiscript/utility/json.hpp index 82a1dac..e8cae19 100644 --- a/include/chaiscript/utility/json.hpp +++ b/include/chaiscript/utility/json.hpp @@ -301,7 +301,7 @@ class JSON string ToString() const { bool b; return ToString( b ); } string ToString( bool &ok ) const { ok = (Type == Class::String); - return ok ? std::move( json_escape( *Internal.String ) ): string(""); + return ok ? *Internal.String : string(""); } double ToFloat() const { bool b; return ToFloat( b ); } @@ -461,8 +461,7 @@ namespace { JSON Key = parse_next( str, offset ); consume_ws( str, offset ); if( str[offset] != ':' ) { - std::cerr << "Error: Object: Expected colon, found '" << str[offset] << "'\n"; - break; + throw std::runtime_error(std::string("JSON ERROR: Object: Expected colon, found '") + str[offset] + "'\n"); } consume_ws( str, ++offset ); JSON Value = parse_next( str, offset ); @@ -476,8 +475,7 @@ namespace { ++offset; break; } else { - std::cerr << "ERROR: Object: Expected comma, found '" << str[offset] << "'\n"; - break; + throw std::runtime_error(std::string("JSON ERROR: Object: Expected comma, found '") + str[offset] + "'\n"); } } @@ -505,8 +503,7 @@ namespace { ++offset; break; } else { - std::cerr << "ERROR: Array: Expected ',' or ']', found '" << str[offset] << "'\n"; - return JSON::Make( JSON::Class::Array ); + throw std::runtime_error(std::string("JSON ERROR: Array: Expected ',' or ']', found '") + str[offset] + "'\n"); } } @@ -533,8 +530,7 @@ namespace { if( (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ) val += c; else { - std::cerr << "ERROR: String: Expected hex character in unicode escape, found '" << c << "'\n"; - return JSON::Make( JSON::Class::String ); + throw std::runtime_error(std::string("JSON ERROR: String: Expected hex character in unicode escape, found '") + c + "'"); } } offset += 4; @@ -568,14 +564,16 @@ namespace { } if( offset < str.size() && (c == 'E' || c == 'e' )) { c = str[ offset++ ]; - if( c == '-' ){ ++offset; exp_str += '-';} - for (;;) { + if( c == '-' ) { exp_str += '-';} + else if( c == '+' ) { } + else --offset; + + for (; offset < str.size() ;) { c = str[ offset++ ]; if( c >= '0' && c <= '9' ) exp_str += c; else if( !isspace( c ) && c != ',' && c != ']' && c != '}' ) { - std::cerr << "ERROR: Number: Expected a number for exponent, found '" << c << "'\n"; - return JSON::Make( JSON::Class::Null ); + throw std::runtime_error(std::string("JSON ERROR: Number: Expected a number for exponent, found '") + c + "'"); } else break; @@ -583,8 +581,7 @@ namespace { exp = std::stol( exp_str ); } else if( offset < str.size() && (!isspace( c ) && c != ',' && c != ']' && c != '}' )) { - std::cerr << "ERROR: Number: unexpected character '" << c << "'\n"; - return JSON::Make( JSON::Class::Null ); + throw std::runtime_error(std::string("JSON ERROR: Number: unexpected character '") + c + "'"); } --offset; @@ -608,16 +605,14 @@ namespace { offset += 5; Bool = false; } else { - std::cerr << "ERROR: Bool: Expected 'true' or 'false', found '" << str.substr( offset, 5 ) << "'\n"; - return JSON::Make( JSON::Class::Null ); + throw std::runtime_error(std::string("JSON ERROR: Bool: Expected 'true' or 'false', found '") + str.substr( offset, 5 ) + "'"); } return Bool; } JSON parse_null( const string &str, size_t &offset ) { if( str.substr( offset, 4 ) != "null" ) { - std::cerr << "ERROR: Null: Expected 'null', found '" << str.substr( offset, 4 ) << "'\n"; - return JSON::Make( JSON::Class::Null ); + throw std::runtime_error(std::string("JSON ERROR: Null: Expected 'null', found '") + str.substr( offset, 4 ) + "'"); } offset += 4; return JSON(); @@ -637,8 +632,7 @@ namespace { default : if( ( value <= '9' && value >= '0' ) || value == '-' ) return parse_number( str, offset ); } - std::cerr << "ERROR: Parse: Unknown starting character '" << value << "'\n"; - return JSON(); + throw std::runtime_error(std::string("JSON ERROR: Parse: Unexpected starting character '") + value + "'"); } } diff --git a/unittests/json_1.chai b/unittests/json_1.chai new file mode 100644 index 0000000..63277b3 --- /dev/null +++ b/unittests/json_1.chai @@ -0,0 +1,3 @@ + + +assert_true(from_json("null").is_var_null()) diff --git a/unittests/json_10.chai b/unittests/json_10.chai new file mode 100644 index 0000000..f19c7cf --- /dev/null +++ b/unittests/json_10.chai @@ -0,0 +1 @@ +assert_equal(from_json("\"This is a\\n\\nMultiline string\""), "This is a\n\nMultiline string") diff --git a/unittests/json_11.chai b/unittests/json_11.chai new file mode 100644 index 0000000..f11db38 --- /dev/null +++ b/unittests/json_11.chai @@ -0,0 +1,14 @@ +assert_equal(from_json( +"{\n" + +" \"T1\" : \"Value With a Quote : \\\"\",\n" + +" \"T2\" : \"Value With a Rev Solidus : \\/\",\n" + +" \"T3\" : \"Value with a Solidus : \\\\\",\n" + +" \"T4\" : \"Value with a Backspace : \\b\",\n" + +" \"T5\" : \"Value with a Formfeed : \\f\",\n" + +" \"T6\" : \"Value with a Newline : \\n\",\n" + +" \"T7\" : \"Value with a Carriage Return : \\r\",\n" + +" \"T8\" : \"Value with a Horizontal Tab : \\t\"\n" + +"}"), [ "T1" : "Value With a Quote : \"", "T2" : "Value With a Rev Solidus : /", "T3" : "Value with a Solidus : \\", "T4" : "Value with a Backspace : \b", "T5" : "Value with a Formfeed : \f", "T6" : "Value with a Newline : \n", "T7" : "Value with a Carriage Return : \r", "T8" : "Value with a Horizontal Tab : \t" ]); + + + diff --git a/unittests/json_12.chai b/unittests/json_12.chai new file mode 100644 index 0000000..a93211d --- /dev/null +++ b/unittests/json_12.chai @@ -0,0 +1 @@ +assert_equal(from_json("\"\""), "") diff --git a/unittests/json_13.chai b/unittests/json_13.chai new file mode 100644 index 0000000..3f9fa00 --- /dev/null +++ b/unittests/json_13.chai @@ -0,0 +1 @@ +assert_equal(from_json("1.20E+2"), 1.20e2) diff --git a/unittests/json_2.chai b/unittests/json_2.chai new file mode 100644 index 0000000..0f779bd --- /dev/null +++ b/unittests/json_2.chai @@ -0,0 +1,3 @@ + +assert_true(from_json("true")) + diff --git a/unittests/json_3.chai b/unittests/json_3.chai new file mode 100644 index 0000000..d3f222e --- /dev/null +++ b/unittests/json_3.chai @@ -0,0 +1 @@ +assert_equal(from_json("100"), 100) diff --git a/unittests/json_4.chai b/unittests/json_4.chai new file mode 100644 index 0000000..22d388c --- /dev/null +++ b/unittests/json_4.chai @@ -0,0 +1 @@ +assert_equal(from_json("1.234"), 1.234) diff --git a/unittests/json_5.chai b/unittests/json_5.chai new file mode 100644 index 0000000..9ad2ca2 --- /dev/null +++ b/unittests/json_5.chai @@ -0,0 +1 @@ +assert_equal(from_json("\"StringTest\""), "StringTest") diff --git a/unittests/json_6.chai b/unittests/json_6.chai new file mode 100644 index 0000000..8c33bff --- /dev/null +++ b/unittests/json_6.chai @@ -0,0 +1 @@ +assert_equal(from_json("{}"), Map()) diff --git a/unittests/json_7.chai b/unittests/json_7.chai new file mode 100644 index 0000000..b2c6efa --- /dev/null +++ b/unittests/json_7.chai @@ -0,0 +1,4 @@ +assert_equal(from_json("\n" + +"{\n" + +" \"Key\" : \"Value\"\n" + +"}\n"), ["Key":"Value"]) diff --git a/unittests/json_8.chai b/unittests/json_8.chai new file mode 100644 index 0000000..104c8bc --- /dev/null +++ b/unittests/json_8.chai @@ -0,0 +1 @@ +assert_equal(from_json("[]"), []) diff --git a/unittests/json_9.chai b/unittests/json_9.chai new file mode 100644 index 0000000..1512738 --- /dev/null +++ b/unittests/json_9.chai @@ -0,0 +1,2 @@ +assert_equal(from_json("[1,2,3]"), [1,2,3]) +