summaryrefslogtreecommitdiff
path: root/unittest
diff options
context:
space:
mode:
authorSergei Golubchik <serg@mariadb.org>2019-01-17 13:07:26 +0100
committerSergei Golubchik <serg@mariadb.org>2019-02-04 15:54:10 +0100
commiteeaaf4a845268fe13c05a0cef4e79722d5835958 (patch)
tree6fb9b8f92b9ab65abd625584eba489636d290562 /unittest
parent5b996782be6b752ce50a0ecaa222b0688aa9e75d (diff)
downloadmariadb-git-eeaaf4a845268fe13c05a0cef4e79722d5835958.tar.gz
stricter json unit tests
Diffstat (limited to 'unittest')
-rw-r--r--unittest/strings/json-t.c133
1 files changed, 77 insertions, 56 deletions
diff --git a/unittest/strings/json-t.c b/unittest/strings/json-t.c
index ce0f04dc030..7c5f7957d42 100644
--- a/unittest/strings/json-t.c
+++ b/unittest/strings/json-t.c
@@ -17,66 +17,87 @@
#include <my_sys.h>
#include <json_lib.h>
-int json_locate_key(const char *js, const char *js_end, const char *kname,
- const char **key_start, const char **key_end,
- int *comma_pos);
-int main()
+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 , "
+ "\"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;
const char *value_start;
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;
- int result, comma_pos;
-
- plan(15);
-
-#define do_json(V) \
- do { \
- value_type= json_get_object_key(json, json+strlen(json), \
- V, &value_start, &value_len); \
- ok(value_type != JSV_BAD_JSON, V); \
- diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \
- } while(0)
-#define do_json_ar(N) \
- do { \
- value_type= json_get_array_item(json_ar, json_ar+strlen(json_ar), \
- N, &value_start, &value_len); \
- ok(value_type != JSV_BAD_JSON, #N); \
- diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \
- } while(0)
-#define do_json_locate(J, V) \
- do { \
- result= json_locate_key(J, J+strlen(J), \
- V, &key_start, &key_end, &comma_pos); \
- ok(result == 0, V); \
- if (key_start) \
- diag("key_str=\"%.*s\" comma_pos= %d", (int)(key_end - key_start), key_start, comma_pos); \
- else \
- diag("no key found"); \
- } while(0)
-
- do_json("int");
- do_json("str");
- do_json("bool");
- do_json("c");
- do_json("array");
-
- do_json_ar(0);
- do_json_ar(1);
- 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");
+ int res, comma_pos;
+
+ res= json_locate_key(json, json + strlen(json),
+ key, &key_start, &key_end, &comma_pos);
+ if (key_start)
+ ok(res == 0 && key_start - json == from && key_end - json == to &&
+ comma_pos == cp, "%s: [%d,%d,%d] %.*s%s", key, (int)(key_start-json),
+ (int)(key_end-json), comma_pos, (int)(key_start - json), json, key_end);
+ else
+ ok(res == 0 && from == -1, "%s: key not found", key);
+}
+
+int main()
+{
+ plan(18);
+
+ diag("%s", json);
+ do_json("int", 4, "1");
+ do_json("str", 3, "foo bar");
+ do_json("bool", 6, "false");
+ do_json("c", 0, "1234");
+ do_json("array", 2, "[10,20,{\"c\":\"d\"}]");
+ diag("%s", json_ar);
+ do_json_ar(0, 4, "1");
+ do_json_ar(1, 3, "foo bar");
+ do_json_ar(2, 2, "[10,20,{\"c\":\"d\"}]");
+ do_json_ar(3, 6, "false");
+ do_json_ar(4, 0, "1234");
+
+ do_json_locate(json, "bool", 50, 63, 1);
+ do_json_locate(json, "int", 1, 9, 2);
+ do_json_locate(json, "array", 24, 50, 1);
+ do_json_locate(json_w, "bool", 43, 61, 1);
+ do_json_locate(json_w, "int", 1, 12, 2);
+ do_json_locate(json_w, "array", 11, 43, 1);
+ do_json_locate(json_w, "c", -1, -1, -1);
+ do_json_locate(json_1, "str", 1, 22, 0);
+
return exit_status();
}