From 8c862072587c2ceed7d48ad335a031556f5b37cb Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sun, 26 Feb 2017 16:22:45 +0000 Subject: [PATCH] Add a few more cases to test_parse to provide some examples of how parsing works; should help address the questions raised in Issue #302. --- tests/test_parse.c | 35 ++++++++++++++++++++++++++++++++++- tests/test_parse.expected | 18 +++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/tests/test_parse.c b/tests/test_parse.c index 8f66441..73487b6 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -262,7 +262,38 @@ struct incremental_step { /* To stop parsing a number we need to reach a non-digit, e.g. a \0 */ { "1", 1, 1, json_tokener_continue, 0 }, + /* This should parse as the number 12, since it continues the "1" */ { "2", 2, 1, json_tokener_success, 0 }, + { "12{", 3, 2, json_tokener_success, 1 }, + + /* Similar tests for other kinds of objects: */ + /* These could all return success immediately, since regardless of + what follows the false/true/null token we *will* return a json object, + but it currently doesn't work that way. hmm... */ + { "false", 5, 5, json_tokener_continue, 1 }, + { "false", 6, 5, json_tokener_success, 1 }, + { "true", 4, 4, json_tokener_continue, 1 }, + { "true", 5, 4, json_tokener_success, 1 }, + { "null", 4, 4, json_tokener_continue, 1 }, + { "null", 5, 4, json_tokener_success, 1 }, + + /* offset=1 because "n" is the start of "null". hmm... */ + { "noodle", 7, 1, json_tokener_error_parse_null, 1 }, + /* offset=2 because "na" is the start of "nan". hmm... */ + { "naodle", 7, 2, json_tokener_error_parse_null, 1 }, + /* offset=2 because "tr" is the start of "true". hmm... */ + { "track", 6, 2, json_tokener_error_parse_boolean, 1 }, + + /* Although they may initially look like they should fail, + the next few tests check that parsing multiple sequential + json objects in the input works as expected */ + { "null123", 9, 4, json_tokener_success, 0 }, + { "null123" + 4, 4, 3, json_tokener_success, 1 }, + { "nullx", 5, 4, json_tokener_success, 0 }, + { "nullx" + 4, 2, 0, json_tokener_error_parse_unexpected, 1 }, + { "{\"a\":1}{\"b\":2}",15, 7, json_tokener_success, 0 }, + { "{\"a\":1}{\"b\":2}" + 7, + 8, 7, json_tokener_success, 1 }, /* Some bad formatting. Check we get the correct error status */ { "2015-01-15", 10, 4, json_tokener_error_parse_number, 1 }, @@ -362,7 +393,9 @@ static void test_incremental_parse() } else { - if (new_obj == NULL) + if (new_obj == NULL && + !(step->length >= 4 && + strncmp(step->string_to_parse, "null", 4) == 0)) printf("ERROR: expected valid object, instead: %s\n", json_tokener_error_desc(jerr)); else if (tok->char_offset != expected_char_offset) diff --git a/tests/test_parse.expected b/tests/test_parse.expected index 713d318..183b38b 100644 --- a/tests/test_parse.expected +++ b/tests/test_parse.expected @@ -58,6 +58,22 @@ json_tokener_parse_ex(tok, {"x": 123 }"X", 14) ... OK: got object of type [obje json_tokener_parse_ex(tok, "Y" , 3) ... OK: got object of type [string]: "Y" json_tokener_parse_ex(tok, 1 , 1) ... OK: got correct error: continue json_tokener_parse_ex(tok, 2 , 2) ... OK: got object of type [int]: 12 +json_tokener_parse_ex(tok, 12{ , 3) ... OK: got object of type [int]: 12 +json_tokener_parse_ex(tok, false , 5) ... OK: got correct error: continue +json_tokener_parse_ex(tok, false , 6) ... OK: got object of type [boolean]: false +json_tokener_parse_ex(tok, true , 4) ... OK: got correct error: continue +json_tokener_parse_ex(tok, true , 5) ... OK: got object of type [boolean]: true +json_tokener_parse_ex(tok, null , 4) ... OK: got correct error: continue +json_tokener_parse_ex(tok, null , 5) ... OK: got object of type [null]: null +json_tokener_parse_ex(tok, noodle , 7) ... OK: got correct error: null expected +json_tokener_parse_ex(tok, naodle , 7) ... OK: got correct error: null expected +json_tokener_parse_ex(tok, track , 6) ... OK: got correct error: boolean expected +json_tokener_parse_ex(tok, null123 , 9) ... OK: got object of type [null]: null +json_tokener_parse_ex(tok, 123 , 4) ... OK: got object of type [int]: 123 +json_tokener_parse_ex(tok, nullx , 5) ... OK: got object of type [null]: null +json_tokener_parse_ex(tok, x , 2) ... OK: got correct error: unexpected character +json_tokener_parse_ex(tok, {"a":1}{"b":2}, 15) ... OK: got object of type [object]: { "a": 1 } +json_tokener_parse_ex(tok, {"b":2} , 8) ... OK: got object of type [object]: { "b": 2 } json_tokener_parse_ex(tok, 2015-01-15 , 10) ... OK: got correct error: number expected json_tokener_parse_ex(tok, "blue" , 6) ... OK: got object of type [string]: "blue" json_tokener_parse_ex(tok, "\"" , 4) ... OK: got object of type [string]: "\"" @@ -74,5 +90,5 @@ json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got object of type [array] json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, {"a":1,} , 8) ... OK: got correct error: unexpected character -End Incremental Tests OK=32 ERROR=0 +End Incremental Tests OK=48 ERROR=0 ==================================