Browse Source

Rewrite test_basic_parse() to factor out a single_basic_parse() to avoid lots of duplicate boiler plate code.

Also, emit the input string in the output so it's easier to see what's going on.
tags/json-c-0.13-20171207
Eric Haszlakiewicz 7 years ago
parent
commit
b8738dd623
2 changed files with 90 additions and 188 deletions
  1. +51
    -151
      tests/test_parse.c
  2. +39
    -37
      tests/test_parse.expected

+ 51
- 151
tests/test_parse.c View File

@@ -28,163 +28,63 @@ int main(void)
static json_c_visit_userfunc clear_serializer; static json_c_visit_userfunc clear_serializer;
static void do_clear_serializer(json_object *jso); static void do_clear_serializer(json_object *jso);


static void test_basic_parse()
static void single_basic_parse(const char *test_string, int clear_serializer)
{ {
json_object *new_obj; json_object *new_obj;


new_obj = json_tokener_parse("\"\003\"");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("/* hello */\"foo\"");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("// hello\n\"foo\"");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
new_obj = json_tokener_parse(test_string);
if (clear_serializer)
do_clear_serializer(new_obj);
printf("new_obj.to_string(%s)=%s\n", test_string, json_object_to_json_string(new_obj));
json_object_put(new_obj); json_object_put(new_obj);

new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

}
static void test_basic_parse()
{
single_basic_parse("\"\003\"", 0);
single_basic_parse("/* hello */\"foo\"", 0);
single_basic_parse("// hello\n\"foo\"", 0);
single_basic_parse("\"\\u0041\\u0042\\u0043\"", 0);
// Test with a "short" high surrogate // Test with a "short" high surrogate
new_obj = json_tokener_parse("[9,'\\uDAD");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("null");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("NaN");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("-NaN"); /* non-sensical, returns null */
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("Inf"); /* must use full string, returns null */
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("inf"); /* must use full string, returns null */
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("Infinity");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("infinity");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("-Infinity");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("-infinity");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("True");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("12");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("12.3");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("12.3.4"); /* non-sensical, returns null */
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

single_basic_parse("[9,'\\uDAD", 0);
single_basic_parse("null", 0);
single_basic_parse("NaN", 0);
single_basic_parse("-NaN", 0); /* non-sensical, returns null */

single_basic_parse("Inf", 0); /* must use full string, returns null */
single_basic_parse("inf", 0); /* must use full string, returns null */
single_basic_parse("Infinity", 0);
single_basic_parse("infinity", 0);
single_basic_parse("-Infinity", 0);
single_basic_parse("-infinity", 0);

single_basic_parse("True", 0);

single_basic_parse("12", 0);
single_basic_parse("12.3", 0);
single_basic_parse("12.3.4", 0); /* non-sensical, returns null */
/* was returning (int)2015 before patch, should return null */ /* was returning (int)2015 before patch, should return null */
new_obj = json_tokener_parse("2015-01-15");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{\"FoO\" : -12.3E512}");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{\"FoO\" : -12.3E51.2}"); /* non-sensical, returns null */
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[\"\\n\"]");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[null]");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[]");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[false]");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{}");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"abc\": \"blue\nred\\ngreen\" }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[0e]");
do_clear_serializer(new_obj);
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[0e+]");
do_clear_serializer(new_obj);
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[0e+-1]");
do_clear_serializer(new_obj);
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

new_obj = json_tokener_parse("[18446744073709551616]");
do_clear_serializer(new_obj);
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);
single_basic_parse("2015-01-15", 0);

single_basic_parse("{\"FoO\" : -12.3E512}", 0);
single_basic_parse("{\"FoO\" : -12.3E51.2}", 0); /* non-sensical, returns null */
single_basic_parse("[\"\\n\"]", 0);
single_basic_parse("[\"\\nabc\\n\"]", 0);
single_basic_parse("[null]", 0);
single_basic_parse("[]", 0);
single_basic_parse("[false]", 0);
single_basic_parse("[\"abc\",null,\"def\",12]", 0);
single_basic_parse("{}", 0);
single_basic_parse("{ \"foo\": \"bar\" }", 0);
single_basic_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }", 0);
single_basic_parse("{ \"foo\": [null, \"foo\"] }", 0);
single_basic_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }", 0);
single_basic_parse("{ \"abc\": \"blue\nred\\ngreen\" }", 0);

// Clear serializer for these tests so we see the actual parsed value.
single_basic_parse("[0e]", 1);
single_basic_parse("[0e+]", 1);
single_basic_parse("[0e+-1]", 1);
single_basic_parse("[18446744073709551616]", 1);
} }


// Clear the re-serialization information that the tokener // Clear the re-serialization information that the tokener


+ 39
- 37
tests/test_parse.expected View File

@@ -1,40 +1,42 @@
new_obj.to_string()="\u0003"
new_obj.to_string()="foo"
new_obj.to_string()="foo"
new_obj.to_string()="ABC"
new_obj.to_string()=null
new_obj.to_string()=null
new_obj.to_string()=NaN
new_obj.to_string()=null
new_obj.to_string()=null
new_obj.to_string()=null
new_obj.to_string()=Infinity
new_obj.to_string()=Infinity
new_obj.to_string()=-Infinity
new_obj.to_string()=-Infinity
new_obj.to_string()=true
new_obj.to_string()=12
new_obj.to_string()=12.3
new_obj.to_string()=null
new_obj.to_string()=null
new_obj.to_string()={ "FoO": -12.3E512 }
new_obj.to_string()=null
new_obj.to_string()=[ "\n" ]
new_obj.to_string()=[ "\nabc\n" ]
new_obj.to_string()=[ null ]
new_obj.to_string()=[ ]
new_obj.to_string()=[ false ]
new_obj.to_string()=[ "abc", null, "def", 12 ]
new_obj.to_string()={ }
new_obj.to_string()={ "foo": "bar" }
new_obj.to_string()={ "foo": "bar", "baz": null, "bool0": true }
new_obj.to_string()={ "foo": [ null, "foo" ] }
new_obj.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] }
new_obj.to_string()={ "abc": "blue\nred\ngreen" }
new_obj.to_string()=[ 0.0 ]
new_obj.to_string()=[ 0.0 ]
new_obj.to_string()=null
new_obj.to_string()=[ 9223372036854775807 ]
new_obj.to_string("")="\u0003"
new_obj.to_string(/* hello */"foo")="foo"
new_obj.to_string(// hello
"foo")="foo"
new_obj.to_string("\u0041\u0042\u0043")="ABC"
new_obj.to_string([9,'\uDAD)=null
new_obj.to_string(null)=null
new_obj.to_string(NaN)=NaN
new_obj.to_string(-NaN)=null
new_obj.to_string(Inf)=null
new_obj.to_string(inf)=null
new_obj.to_string(Infinity)=Infinity
new_obj.to_string(infinity)=Infinity
new_obj.to_string(-Infinity)=-Infinity
new_obj.to_string(-infinity)=-Infinity
new_obj.to_string(True)=true
new_obj.to_string(12)=12
new_obj.to_string(12.3)=12.3
new_obj.to_string(12.3.4)=null
new_obj.to_string(2015-01-15)=null
new_obj.to_string({"FoO" : -12.3E512})={ "FoO": -12.3E512 }
new_obj.to_string({"FoO" : -12.3E51.2})=null
new_obj.to_string(["\n"])=[ "\n" ]
new_obj.to_string(["\nabc\n"])=[ "\nabc\n" ]
new_obj.to_string([null])=[ null ]
new_obj.to_string([])=[ ]
new_obj.to_string([false])=[ false ]
new_obj.to_string(["abc",null,"def",12])=[ "abc", null, "def", 12 ]
new_obj.to_string({})={ }
new_obj.to_string({ "foo": "bar" })={ "foo": "bar" }
new_obj.to_string({ "foo": "bar", "baz": null, "bool0": true })={ "foo": "bar", "baz": null, "bool0": true }
new_obj.to_string({ "foo": [null, "foo"] })={ "foo": [ null, "foo" ] }
new_obj.to_string({ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] })={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] }
new_obj.to_string({ "abc": "blue
red\ngreen" })={ "abc": "blue\nred\ngreen" }
new_obj.to_string([0e])=[ 0.0 ]
new_obj.to_string([0e+])=[ 0.0 ]
new_obj.to_string([0e+-1])=null
new_obj.to_string([18446744073709551616])=[ 9223372036854775807 ]
================================== ==================================
json_tokener_parse_versbose() OK json_tokener_parse_versbose() OK
================================== ==================================


Loading…
Cancel
Save