From: Martin Mares Date: Wed, 8 Jul 2015 17:55:35 +0000 (+0200) Subject: JSON: More tests and bug fixes X-Git-Tag: v6.5~22 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=0f71e145c12d6ea556a393a49a7fb8aef296cf6e;p=libucw.git JSON: More tests and bug fixes --- diff --git a/ucw-json/format.c b/ucw-json/format.c index 58526c84..30f8757f 100644 --- a/ucw-json/format.c +++ b/ucw-json/format.c @@ -134,21 +134,24 @@ void json_write_value(struct json_context *js, struct json_node *n) { if (!GARY_SIZE(n->pairs)) bputs(fb, "{}"); - bputc(fb, '{'); - js->out_indent++; - for (size_t i=0; i < GARY_SIZE(n->pairs); i++) + else { - if (i) - bputc(fb, ','); + bputc(fb, '{'); + js->out_indent++; + for (size_t i=0; i < GARY_SIZE(n->pairs); i++) + { + if (i) + bputc(fb, ','); + write_space(js); + struct json_pair *p = &n->pairs[i]; + write_string(js, p->key); + bputs(fb, ": "); + json_write_value(js, p->value); + } + js->out_indent--; write_space(js); - struct json_pair *p = &n->pairs[i]; - write_string(js, p->key); - bputs(fb, ": "); - json_write_value(js, p->value); + bputc(fb, '}'); } - js->out_indent--; - write_space(js); - bputc(fb, '}'); break; } default: diff --git a/ucw-json/json-test.t b/ucw-json/json-test.t index 20aef1a9..cd6f5c72 100644 --- a/ucw-json/json-test.t +++ b/ucw-json/json-test.t @@ -1,14 +1,10 @@ # Tests for the JSON library # (c) 2015 Martin Mares -Name: Empty input -Run: ../obj/ucw-json/json-test -rw -Exit: 1 -Err: ERROR: Empty input at line 1:0 - ### Literals ### Name: Null +Run: ../obj/ucw-json/json-test -rw In: null Out: null @@ -275,3 +271,109 @@ In: [ ,false ] Out: [ "a", null, false, false ] + +Name: Unterminated array 1 +In: [1,2 +Exit: 1 +Err: ERROR: Comma or right bracket expected at line 2:0 + +Name: Unterminated array 2 +In: [1,2, +Exit: 1 +Err: ERROR: Unterminated array at line 2:0 + +Name: Extra comma not allowed +In: [1,2,] +Exit: 1 +Err: ERROR: Misplaced end of array at line 1:6 + +Name: Solitary comma not allowed +In: , +Exit: 1 +Err: ERROR: Misplaced comma at line 1:1 + +Name: Deeply nested array +In: [[[[[[[[[[]]]]]]]]]] +Out: [ [ [ [ [ [ [ [ [ [] ] ] ] ] ] ] ] ] ] + +Name: Deeply unclosed array +In: [[[[[[[[[[] +Exit: 1 +Err: ERROR: Comma or right bracket expected at line 2:0 + +Name: Missing comma +In: [1 2] +Exit: 1 +Err: ERROR: Comma or right bracket expected at line 1:5 + +### Objects ### + +Name: Empty object +In: {} +Out: {} + +Name: One-entry object +In: {"a":"b"} +Out: { "a": "b" } + +Name: Two-entry object +In: {"a":1,"b":2} +Out: { "a": 1, "b": 2 } + +Name: Nested objects +In: { + "a": [1,2], + "b": { "x": true, "y": false } + } +Out: { "a": [ 1, 2 ], "b": { "x": true, "y": false } } + +Name: Unterminated object 1 +In: { +Exit: 1 +Err: ERROR: Unterminated object at line 2:0 + +Name: Unterminated object 2 +In: { "a" +Exit: 1 +Err: ERROR: Colon expected at line 2:0 + +Name: Unterminated object 3 +In: { "a": +Exit: 1 +Err: ERROR: Unterminated object at line 2:0 + +Name: Unterminated object 4 +In: { "a":1, +Exit: 1 +Err: ERROR: Unterminated object at line 2:0 + +Name: Extra comma not allowed in objects +In: { "a":1, } +Exit: 1 +Err: ERROR: Misplaced end of object at line 1:10 + +Name: Non-string key +In: {1:2} +Exit: 1 +Err: ERROR: Object key must be a string at line 1:3 + +Name: Repeated key +In: {"a":1, "a":2} +Exit: 1 +Err: ERROR: Key already set at line 1:14 + +Name: Missing object comma +In: {"a":1 "b":2} +Exit: 1 +Err: ERROR: Comma expected at line 1:10 + +### Top-level problems ### + +Name: Empty input +Exit: 1 +Err: ERROR: Empty input at line 1:0 + +Name: Multiple values +In: 1 2 +Exit: 1 +Err: ERROR: Only one top-level value allowed at line 1:4 diff --git a/ucw-json/parse.c b/ucw-json/parse.c index 765352b0..96c916f3 100644 --- a/ucw-json/parse.c +++ b/ucw-json/parse.c @@ -386,7 +386,7 @@ struct json_node *json_next_value(struct json_context *js) if (t->type == JSON_END_ARRAY) break; if (t->type != JSON_VALUE_SEP) - json_parse_error(js, "Comma expected"); + json_parse_error(js, "Comma or right bracket expected"); } return a; }