Commit eeaaf4a8 authored by Sergei Golubchik's avatar Sergei Golubchik

stricter json unit tests

parent 5b996782
...@@ -17,66 +17,87 @@ ...@@ -17,66 +17,87 @@
#include <my_sys.h> #include <my_sys.h>
#include <json_lib.h> #include <json_lib.h>
int json_locate_key(const char *js, const char *js_end, const char *kname, const char *json="{\"int\":1,\"str\":\"foo bar\","
const char **key_start, const char **key_end, "\"array\":[10,20,{\"c\":\"d\"}],\"bool\":false}";
int *comma_pos);
int main() const char *json_ar="[1,\"foo bar\",[10,20,{\"c\":\"d\"}],false]";
const char *json_w="{\"int\" : 1 , "
"\"array\" : [10,20,{\"c\":\"d\"}] , \"bool\" : false }";
const char *json_1="{ \"str\" : \"foo bar\" }";
void do_json(const char *key, int type, const char *value)
{
enum json_types value_type;
const char *value_start;
int value_len;
value_type= json_get_object_key(json, json + strlen(json),
key, &value_start, &value_len);
if (type)
ok(value_type == type && value_len == (int)strlen(value) && !memcmp(value_start, value, value_len),
"%s: type=%u, value(%d)=\"%.*s\"", key, value_type, value_len, value_len, value_start);
else
ok(value_type == type && value_len == (int)strlen(value),
"%s: type=%u keys=%u end=\"%s\"", key, value_type, value_len, value_start);
}
void do_json_ar(int n, int type, const char *value)
{ {
const char *json="{\"int\":1, \"str\":\"foo bar\", "
"\"array\":[10,20,{\"c\":\"d\"}],\"bool\":false}";
const char *json_ar="[1, \"foo bar\", " "[10,20,{\"c\":\"d\"}], false]";
const char *json_w="{\"int\" : 1 , \"str\" : \"foo bar\" , "
"\"array\" : [10,20,{\"c\":\"d\"}] , \"bool\" : false }";
const char *json_1="{ \"str\" : \"foo bar\" }";
enum json_types value_type; enum json_types value_type;
const char *value_start; const char *value_start;
int value_len; int value_len;
value_type= json_get_array_item(json_ar, json_ar + strlen(json_ar),
n, &value_start, &value_len);
if (type)
ok(value_type == type && value_len == (int)strlen(value) && !memcmp(value_start, value, value_len),
"%i: type=%u, value(%d)=\"%.*s\"", n, value_type, value_len, value_len, value_start);
else
ok(value_type == type && value_len == (int)strlen(value),
"%i: type=%u keys=%u end=\"%s\"", n, value_type, value_len, value_start);
}
void do_json_locate(const char *json, const char *key, int from, int to, int cp)
{
const char *key_start, *key_end; const char *key_start, *key_end;
int result, comma_pos; int res, comma_pos;
plan(15); res= json_locate_key(json, json + strlen(json),
key, &key_start, &key_end, &comma_pos);
#define do_json(V) \ if (key_start)
do { \ ok(res == 0 && key_start - json == from && key_end - json == to &&
value_type= json_get_object_key(json, json+strlen(json), \ comma_pos == cp, "%s: [%d,%d,%d] %.*s%s", key, (int)(key_start-json),
V, &value_start, &value_len); \ (int)(key_end-json), comma_pos, (int)(key_start - json), json, key_end);
ok(value_type != JSV_BAD_JSON, V); \ else
diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \ ok(res == 0 && from == -1, "%s: key not found", key);
} while(0) }
#define do_json_ar(N) \
do { \ int main()
value_type= json_get_array_item(json_ar, json_ar+strlen(json_ar), \ {
N, &value_start, &value_len); \ plan(18);
ok(value_type != JSV_BAD_JSON, #N); \
diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \ diag("%s", json);
} while(0) do_json("int", 4, "1");
#define do_json_locate(J, V) \ do_json("str", 3, "foo bar");
do { \ do_json("bool", 6, "false");
result= json_locate_key(J, J+strlen(J), \ do_json("c", 0, "1234");
V, &key_start, &key_end, &comma_pos); \ do_json("array", 2, "[10,20,{\"c\":\"d\"}]");
ok(result == 0, V); \ diag("%s", json_ar);
if (key_start) \ do_json_ar(0, 4, "1");
diag("key_str=\"%.*s\" comma_pos= %d", (int)(key_end - key_start), key_start, comma_pos); \ do_json_ar(1, 3, "foo bar");
else \ do_json_ar(2, 2, "[10,20,{\"c\":\"d\"}]");
diag("no key found"); \ do_json_ar(3, 6, "false");
} while(0) do_json_ar(4, 0, "1234");
do_json("int"); do_json_locate(json, "bool", 50, 63, 1);
do_json("str"); do_json_locate(json, "int", 1, 9, 2);
do_json("bool"); do_json_locate(json, "array", 24, 50, 1);
do_json("c"); do_json_locate(json_w, "bool", 43, 61, 1);
do_json("array"); do_json_locate(json_w, "int", 1, 12, 2);
do_json_locate(json_w, "array", 11, 43, 1);
do_json_ar(0); do_json_locate(json_w, "c", -1, -1, -1);
do_json_ar(1); do_json_locate(json_1, "str", 1, 22, 0);
do_json_ar(2);
do_json_ar(3);
do_json_ar(4);
do_json_locate(json_w, "bool");
do_json_locate(json_w, "int");
do_json_locate(json_w, "array");
do_json_locate(json_1, "str");
do_json_locate(json_w, "c");
return exit_status(); return exit_status();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment