[libvirt] [PATCH v2 3/4] tests: virjson: Test formatting along with parsing of JSON objects

Peter Krempa posted 4 patches 8 years, 5 months ago
[libvirt] [PATCH v2 3/4] tests: virjson: Test formatting along with parsing of JSON objects
Posted by Peter Krempa 8 years, 5 months ago
Format the parsed string back and compare it to the original (or
modified) string for back and forth comparison.
---
 tests/virjsontest.c | 58 ++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 44 insertions(+), 14 deletions(-)

diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index 029a580f4..a6e158179 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -23,6 +23,8 @@ testJSONFromString(const void *data)
 {
     const struct testInfo *info = data;
     virJSONValuePtr json;
+    const char *expectstr = info->expect ? info->expect : info->doc;
+    char *formatted = NULL;
     int ret = -1;

     json = virJSONValueFromString(info->doc);
@@ -45,9 +47,20 @@ testJSONFromString(const void *data)

     VIR_TEST_DEBUG("Parsed %s\n", info->doc);

+    if (!(formatted = virJSONValueToString(json, false))) {
+        VIR_TEST_VERBOSE("Failed to format json data\n");
+        goto cleanup;
+    }
+
+    if (STRNEQ(expectstr, formatted)) {
+        virTestDifference(stderr, expectstr, formatted);
+        goto cleanup;
+    }
+
     ret = 0;

  cleanup:
+    VIR_FREE(formatted);
     virJSONValueFree(json);
     return ret;
 }
@@ -368,23 +381,39 @@ mymain(void)
             ret = -1;                                               \
     } while (0)

-#define DO_TEST_PARSE(name, doc)                \
-    DO_TEST_FULL(name, FromString, doc, NULL, true)
+/**
+ * DO_TEST_PARSE:
+ * @name: test name
+ * @doc: source JSON string
+ * @expect: expected output JSON formatted from parsed @doc
+ *
+ * Parses @doc and formats it back. If @expect is NULL the result has to be
+ * identical to @doc.
+ */
+#define DO_TEST_PARSE(name, doc, expect)                \
+    DO_TEST_FULL(name, FromString, doc, expect, true)

 #define DO_TEST_PARSE_FAIL(name, doc)           \
     DO_TEST_FULL(name, FromString, doc, NULL, false)


-    DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\": \"libvirt-1\"}");
+    DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\": \"libvirt-1\"}",
+                  "{\"return\":{},\"id\":\"libvirt-1\"}");
     DO_TEST_PARSE("NotSoSimple", "{\"QMP\": {\"version\": {\"qemu\":"
                   "{\"micro\": 91, \"minor\": 13, \"major\": 0},"
-                  "\"package\": \" (qemu-kvm-devel)\"}, \"capabilities\": []}}");
-
+                  "\"package\": \" (qemu-kvm-devel)\"}, \"capabilities\": []}}",
+                  "{\"QMP\":{\"version\":{\"qemu\":"
+                  "{\"micro\":91,\"minor\":13,\"major\":0},"
+                  "\"package\":\" (qemu-kvm-devel)\"},\"capabilities\":[]}}");

     DO_TEST_PARSE("Harder", "{\"return\": [{\"filename\": "
                   "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\","
                   "\"label\": \"charmonitor\"}, {\"filename\": \"pty:/dev/pts/158\","
-                  "\"label\": \"charserial0\"}], \"id\": \"libvirt-3\"}");
+                  "\"label\": \"charserial0\"}], \"id\": \"libvirt-3\"}",
+                  "{\"return\":[{\"filename\":"
+                  "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\","
+                  "\"label\":\"charmonitor\"},{\"filename\":\"pty:/dev/pts/158\","
+                  "\"label\":\"charserial0\"}],\"id\":\"libvirt-3\"}");

     DO_TEST_PARSE("VeryHard", "{\"return\":[{\"name\":\"quit\"},{\"name\":"
                   "\"eject\"},{\"name\":\"change\"},{\"name\":\"screendump\"},"
@@ -409,7 +438,7 @@ mymain(void)
                   "\"query-mice\"},{\"name\":\"query-vnc\"},{\"name\":"
                   "\"query-spice\"},{\"name\":\"query-name\"},{\"name\":"
                   "\"query-uuid\"},{\"name\":\"query-migrate\"},{\"name\":"
-                  "\"query-balloon\"}],\"id\":\"libvirt-2\"}");
+                  "\"query-balloon\"}],\"id\":\"libvirt-2\"}", NULL);

     DO_TEST_FULL("add and remove", AddRemove,
                  "{\"name\": \"sample\", \"value\": true}",
@@ -445,21 +474,22 @@ mymain(void)
                  "\"query-balloon\"}], \"id\": \"libvirt-2\"}", NULL, true);


-    DO_TEST_PARSE("almost nothing", "[]");
+    DO_TEST_PARSE("almost nothing", "[]", NULL);
     DO_TEST_PARSE_FAIL("nothing", "");

-    DO_TEST_PARSE("number without garbage", "[ 234545 ]");
+    DO_TEST_PARSE("number without garbage", "[ 234545 ]", "[234545]");
     DO_TEST_PARSE_FAIL("number with garbage", "[ 2345b45 ]");

-    DO_TEST_PARSE("float without garbage", "[ 0.0314159e+100 ]");
+    DO_TEST_PARSE("float without garbage", "[ 0.0314159e+100 ]", "[0.0314159e+100]");
     DO_TEST_PARSE_FAIL("float with garbage", "[ 0.0314159ee+100 ]");

-    DO_TEST_PARSE("string", "[ \"The meaning of life\" ]");
+    DO_TEST_PARSE("string", "[ \"The meaning of life\" ]",
+                  "[\"The meaning of life\"]");
     DO_TEST_PARSE_FAIL("unterminated string", "[ \"The meaning of lif ]");

-    DO_TEST_PARSE("integer", "1");
-    DO_TEST_PARSE("boolean", "true");
-    DO_TEST_PARSE("null", "null");
+    DO_TEST_PARSE("integer", "1", NULL);
+    DO_TEST_PARSE("boolean", "true", NULL);
+    DO_TEST_PARSE("null", "null", NULL);
     DO_TEST_PARSE_FAIL("incomplete keyword", "tr");
     DO_TEST_PARSE_FAIL("overdone keyword", "[ truest ]");
     DO_TEST_PARSE_FAIL("unknown keyword", "huh");
-- 
2.12.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 3/4] tests: virjson: Test formatting along with parsing of JSON objects
Posted by John Ferlan 8 years, 4 months ago

On 07/11/2017 08:56 AM, Peter Krempa wrote:
> Format the parsed string back and compare it to the original (or
> modified) string for back and forth comparison.
> ---
>  tests/virjsontest.c | 58 ++++++++++++++++++++++++++++++++++++++++-------------
>  1 file changed, 44 insertions(+), 14 deletions(-)
> 

Is there a reason why the Copy doesn't need the space reduction? Is that
expected (so to speak).

What's here looks fine, I'm mainly curious.

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 3/4] tests: virjson: Test formatting along with parsing of JSON objects
Posted by Peter Krempa 8 years, 4 months ago
On Wed, Jul 19, 2017 at 08:36:27 -0400, John Ferlan wrote:
> 
> 
> On 07/11/2017 08:56 AM, Peter Krempa wrote:
> > Format the parsed string back and compare it to the original (or
> > modified) string for back and forth comparison.
> > ---
> >  tests/virjsontest.c | 58 ++++++++++++++++++++++++++++++++++++++++-------------
> >  1 file changed, 44 insertions(+), 14 deletions(-)
> > 
> 
> Is there a reason why the Copy doesn't need the space reduction? Is that
> expected (so to speak).

You mean the cases in testJSONCopy? It's because the test case parses
the JSON provided when calling the test case, then copies it and then
formats back strings from the first parsed structure and after the copy.

This means that both went through the formatter and thus have same
spacing rules applied. The output of formatting the copy is never
compared to the original string provided when calling the test case.
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list