]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Add a few more cases to test_parse to provide some examples of how parsing works...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 26 Feb 2017 16:22:45 +0000 (16:22 +0000)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 26 Feb 2017 16:22:45 +0000 (16:22 +0000)
tests/test_parse.c
tests/test_parse.expected

index 8f664419787b7988d290c38fd19ea20063e7be81..73487b6f37651a5b676acd638e6a89cec8afec77 100644 (file)
@@ -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)
index 713d3186859c20f4a65cff1da6e813d7b679c118..183b38baab2631b79de258838ad96cd4d8901236 100644 (file)
@@ -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
 ==================================