Skip to content

Commit

Permalink
Add json tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lefticus committed Sep 30, 2015
1 parent ba30d4f commit b434d26
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 21 deletions.
36 changes: 15 additions & 21 deletions include/chaiscript/utility/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ); }
Expand Down Expand Up @@ -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 );
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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");
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -568,23 +564,24 @@ 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;
}
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;

Expand All @@ -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();
Expand All @@ -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 + "'");
}
}

Expand Down
3 changes: 3 additions & 0 deletions unittests/json_1.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


assert_true(from_json("null").is_var_null())
1 change: 1 addition & 0 deletions unittests/json_10.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("\"This is a\\n\\nMultiline string\""), "This is a\n\nMultiline string")
14 changes: 14 additions & 0 deletions unittests/json_11.chai
Original file line number Diff line number Diff line change
@@ -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" ]);



1 change: 1 addition & 0 deletions unittests/json_12.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("\"\""), "")
1 change: 1 addition & 0 deletions unittests/json_13.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("1.20E+2"), 1.20e2)
3 changes: 3 additions & 0 deletions unittests/json_2.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

assert_true(from_json("true"))

1 change: 1 addition & 0 deletions unittests/json_3.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("100"), 100)
1 change: 1 addition & 0 deletions unittests/json_4.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("1.234"), 1.234)
1 change: 1 addition & 0 deletions unittests/json_5.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("\"StringTest\""), "StringTest")
1 change: 1 addition & 0 deletions unittests/json_6.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("{}"), Map())
4 changes: 4 additions & 0 deletions unittests/json_7.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
assert_equal(from_json("\n" +
"{\n" +
" \"Key\" : \"Value\"\n" +
"}\n"), ["Key":"Value"])
1 change: 1 addition & 0 deletions unittests/json_8.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_equal(from_json("[]"), [])
2 changes: 2 additions & 0 deletions unittests/json_9.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
assert_equal(from_json("[1,2,3]"), [1,2,3])

1 comment on commit b434d26

@lefticus
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.