test/run-param: don't reach into json_result to get the string.

It's about to change, so refactor this first.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-10-19 11:47:48 +10:30
parent 39f0dfb664
commit 12adcda370

View File

@ -323,13 +323,17 @@ static void bad_programmer(void)
#endif #endif
static void add_members(struct param **params, static void add_members(struct param **params,
struct json_result *obj, char **obj,
struct json_result *arr, unsigned int **ints) char **arr, unsigned int **ints)
{ {
for (int i = 0; i < tal_count(ints); ++i) { for (int i = 0; i < tal_count(ints); ++i) {
char *name = tal_fmt(tmpctx, "%i", i); const char *name = tal_fmt(*params, "%i", i);
json_add_num(obj, name, i); if (i != 0) {
json_add_num(arr, NULL, i); tal_append_fmt(obj, ", ");
tal_append_fmt(arr, ", ");
}
tal_append_fmt(obj, "\"%i\" : %i", i, i);
tal_append_fmt(arr, "%i", i);
param_add(params, name, true, param_add(params, name, true,
typesafe_cb_preargs(bool, void **, typesafe_cb_preargs(bool, void **,
json_tok_number, json_tok_number,
@ -351,16 +355,14 @@ static void five_hundred_params(void)
struct param *params = tal_arr(NULL, struct param, 0); struct param *params = tal_arr(NULL, struct param, 0);
unsigned int **ints = tal_arr(params, unsigned int*, 500); unsigned int **ints = tal_arr(params, unsigned int*, 500);
struct json_result *obj = new_json_result(params); char *obj = tal_fmt(params, "{ ");
struct json_result *arr = new_json_result(params); char *arr = tal_fmt(params, "[ ");
json_object_start(obj, NULL); add_members(&params, &obj, &arr, ints);
json_array_start(arr, NULL); tal_append_fmt(&obj, "}");
add_members(&params, obj, arr, ints); tal_append_fmt(&arr, "]");
json_object_end(obj);
json_array_end(arr);
/* first test object version */ /* first test object version */
struct json *j = json_parse(params, obj->s); struct json *j = json_parse(params, obj);
assert(param_arr(cmd, j->buffer, j->toks, params)); assert(param_arr(cmd, j->buffer, j->toks, params));
for (int i = 0; i < tal_count(ints); ++i) { for (int i = 0; i < tal_count(ints); ++i) {
assert(ints[i]); assert(ints[i]);
@ -369,7 +371,7 @@ static void five_hundred_params(void)
} }
/* now test array */ /* now test array */
j = json_parse(params, arr->s); j = json_parse(params, arr);
assert(param_arr(cmd, j->buffer, j->toks, params)); assert(param_arr(cmd, j->buffer, j->toks, params));
for (int i = 0; i < tal_count(ints); ++i) { for (int i = 0; i < tal_count(ints); ++i) {
assert(*ints[i] == i); assert(*ints[i] == i);