[systemd-devel] [PATCH v2 3/5] test/test-json: Tests for the tokenizer bugfix and the DOM parser

Pavel Odvody podvody at redhat.com
Tue May 19 07:29:53 PDT 2015


The DOM parser tests are accompanied with structure and element analysis
---
 src/test/test-json.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/src/test/test-json.c b/src/test/test-json.c
index 24dc700..c4b4a22 100644
--- a/src/test/test-json.c
+++ b/src/test/test-json.c
@@ -72,6 +72,98 @@ static void test_one(const char *data, ...) {
         va_end(ap);
 }
 
+typedef void (*Test)(JsonVariant *);
+
+static void test_file(const char *data, Test test) {
+        JsonVariant *v = NULL;
+        int r = json_parse(data, &v);
+
+        assert_se(r == 0);
+        assert_se(v != NULL);
+        assert_se(v->type == JSON_VARIANT_OBJECT);
+
+        if (test)
+                test(v);
+
+        json_variant_unref(v);
+}
+
+static void test_1(JsonVariant *v) {
+        JsonVariant *p, *q;
+        unsigned i;
+
+        /* 3 keys + 3 values */
+        assert_se(v->size == 6);
+
+        /* has k */
+        p = json_variant_value(v, "k");
+        assert_se(p && p->type == JSON_VARIANT_STRING);
+
+        /* k equals v */
+        assert_se(streq(json_variant_string(p), "v"));
+
+        /* has foo */
+        p = json_variant_value(v, "foo");
+        assert_se(p && p->type == JSON_VARIANT_ARRAY && p->size == 3);
+
+        /* check  foo[0] = 1, foo[1] = 2, foo[2] = 3 */
+        for (i = 0; i < 3; ++i) {
+                q = json_variant_element(p, i);
+                assert_se(q && q->type == JSON_VARIANT_INTEGER && json_variant_integer(q) == (i+1));
+        }
+
+        /* has bar */
+        p = json_variant_value(v, "bar");
+        assert_se(p && p->type == JSON_VARIANT_OBJECT && p->size == 2);
+
+        /* zap is null */
+        q = json_variant_value(p, "zap");
+        assert_se(q && q->type == JSON_VARIANT_NULL);
+}
+
+static void test_2(JsonVariant *v) {
+        JsonVariant *p, *q;
+
+        /* 2 keys + 2 values */
+        assert_se(v->size == 4);
+
+        /* has mutant */
+        p = json_variant_value(v, "mutant");
+        assert_se(p && p->type == JSON_VARIANT_ARRAY && p->size == 4);
+
+        /* mutant[0] == 1 */
+        q = json_variant_element(p, 0);
+        assert_se(q && q->type == JSON_VARIANT_INTEGER && json_variant_integer(q) == 1);
+
+        /* mutant[1] == null */
+        q = json_variant_element(p, 1);
+        assert_se(q && q->type == JSON_VARIANT_NULL);
+
+        /* mutant[2] == "1" */
+        q = json_variant_element(p, 2);
+        assert_se(q && q->type == JSON_VARIANT_STRING && streq(json_variant_string(q), "1"));
+
+        /* mutant[3] == JSON_VARIANT_OBJECT */
+        q = json_variant_element(p, 3);
+        assert_se(q && q->type == JSON_VARIANT_OBJECT && q->size == 2);
+
+        /* has 1 */
+        p = json_variant_value(q, "1");
+        assert_se(p && p->type == JSON_VARIANT_ARRAY && p->size == 2);
+
+        /* "1"[0] == 1 */
+        q = json_variant_element(p, 0);
+        assert_se(q && q->type == JSON_VARIANT_INTEGER && json_variant_integer(q) == 1);
+
+        /* "1"[1] == "1" */
+        q = json_variant_element(p, 1);
+        assert_se(q && q->type == JSON_VARIANT_STRING && streq(json_variant_string(q), "1"));
+
+        /* has blah */
+        p = json_variant_value(v, "blah");
+        assert_se(p && p->type == JSON_VARIANT_REAL && fabs(json_variant_real(p) - 1.27) < 0.001);
+}
+
 int main(int argc, char *argv[]) {
 
         test_one("x", -EINVAL);
@@ -102,5 +194,10 @@ int main(int argc, char *argv[]) {
         test_one("\"\\udc00\\udc00\"", -EINVAL);
         test_one("\"\\ud801\\udc37\"", JSON_STRING, "\xf0\x90\x90\xb7", JSON_END);
 
+        test_one("[1, 2]", JSON_ARRAY_OPEN, JSON_INTEGER, 1, JSON_COMMA, JSON_INTEGER, 2, JSON_ARRAY_CLOSE, JSON_END);
+
+        test_file("{\"k\": \"v\", \"foo\": [1, 2, 3], \"bar\": {\"zap\": null}}", test_1);
+        test_file("{\"mutant\": [1, null, \"1\", {\"1\": [1, \"1\"]}], \"blah\": 1.27}", test_2);
+
         return 0;
 }
-- 
2.1.0


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20150519/76115e73/attachment-0001.sig>


More information about the systemd-devel mailing list