Skip to content

Commit

Permalink
common/test/run-json: check tok->size is as expected.
Browse files Browse the repository at this point in the history
The external/jsmn/README.md only says:
		int size;        // Number of child (nested) tokens

But it only counts *direct* children, or *direct* members for an object.

This test verifies this (the bug proved to be elsewhere: see next patch!).

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and dflate committed Jan 18, 2019
1 parent 0fb48c8 commit a5a1550
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions common/test/run-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ static int test_json_tok_bitcoin_amount(void)
return 0;
}

static void test_json_tok_size(void)
{
const jsmntok_t *toks;
char *buf;
bool ok;

buf = "[\"e1\", [\"e2\", \"e3\"]]";
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
assert(ok);
/* size only counts *direct* children */
assert(toks[0].size == 2);
assert(toks[2].size == 2);

buf = "[[\"e1\", \"e2\"], \"e3\"]";
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
assert(ok);
/* size only counts *direct* children */
assert(toks[0].size == 2);
assert(toks[1].size == 2);

buf = "{\"e1\" : {\"e2\", \"e3\"}}";
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
assert(ok);
/* size only counts *direct* children */
assert(toks[0].size == 1);
assert(toks[2].size == 2);

buf = "{\"e1\" : {\"e2\", \"e3\"}, \"e4\" : {\"e5\", \"e6\"}}";
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
assert(ok);
/* size only counts *direct* children */
assert(toks[0].size == 2);
assert(toks[2].size == 2);
assert(toks[6].size == 2);
}

static void test_json_delve(void)
{
const jsmntok_t *toks, *t;
Expand All @@ -49,6 +85,7 @@ static void test_json_delve(void)
buf = "{\"1\":\"one\", \"2\":\"two\", \"3\":[\"three\", {\"deeper\": 17}]}";
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
assert(ok);
assert(toks->size == 3);

t = json_delve(buf, toks, ".1");
assert(t);
Expand All @@ -66,6 +103,7 @@ static void test_json_delve(void)
assert(t);
assert(t->type == JSMN_ARRAY);
assert(t == toks+6);
assert(t->size == 2);

t = json_delve(buf, toks, ".3[0]");
assert(t);
Expand All @@ -77,6 +115,7 @@ static void test_json_delve(void)
assert(t);
assert(t->type == JSMN_OBJECT);
assert(t == toks+8);
assert(t->size == 1);

t = json_delve(buf, toks, ".3[1].deeper");
assert(t);
Expand Down Expand Up @@ -118,11 +157,15 @@ static void test_json_delve(void)
"\n";
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
assert(ok);
assert(toks->size == 4);

t = json_delve(buf, toks, ".rpcfile");
assert(!t);
t = json_delve(buf, toks, ".configuration.rpc-file");
assert(!t);
t = json_delve(buf, toks, ".params.configuration");
assert(t);
assert(t->size == 2);
t = json_delve(buf, toks, ".params.configuration.rpc-file");
assert(t);
assert(t->type == JSMN_STRING);
Expand All @@ -134,6 +177,7 @@ int main(void)
setup_locale();
setup_tmpctx();

test_json_tok_size();
test_json_tok_bitcoin_amount();
test_json_delve();
assert(!taken_any());
Expand Down

0 comments on commit a5a1550

Please sign in to comment.