[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v4 23/28] tests: Test qobject_to_json() pretty forma
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH v4 23/28] tests: Test qobject_to_json() pretty formatting |
Date: |
Wed, 18 May 2016 22:41:09 -0600 |
It's risky to refactor qobject_to_json() without at least testing
that pretty output remains unchanged :)
Note that the new simple_pretty() test is a bit sensitive to our
current notion of prettiness, as well as to the hash ordering in
QDict (most of the tests in check-qobject-json intentionally do
not compare the original string to the round-trip string, because
we liberally accept more input forms than the canonical form that
we output).
Signed-off-by: Eric Blake <address@hidden>
---
v4: new patch, split from v3 12/18
---
tests/check-qobject-json.c | 66 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
diff --git a/tests/check-qobject-json.c b/tests/check-qobject-json.c
index 9814282..267fc67 100644
--- a/tests/check-qobject-json.c
+++ b/tests/check-qobject-json.c
@@ -1,6 +1,6 @@
/*
* Copyright IBM, Corp. 2009
- * Copyright (c) 2013, 2015 Red Hat Inc.
+ * Copyright (c) 2013-2016 Red Hat Inc.
*
* Authors:
* Anthony Liguori <address@hidden>
@@ -1383,6 +1383,69 @@ static void simple_whitespace(void)
}
}
+static void simple_pretty(void)
+{
+ int i;
+ struct {
+ const char *encoded;
+ LiteralQObject decoded;
+ } test_cases[] = {
+ {
+ .encoded =
+ "[\n"
+ " 43,\n"
+ " 42\n"
+ "]",
+ .decoded = QLIT_QLIST(((LiteralQObject[]){
+ QLIT_QINT(43),
+ QLIT_QINT(42),
+ { }
+ })),
+ },
+ {
+ .encoded =
+ "[\n"
+ " 43,\n"
+ " {\n"
+ " \"a\": 32,\n"
+ " \"h\": \"b\"\n"
+ " },\n"
+ " [\n"
+ " ],\n"
+ " 42\n"
+ "]",
+ .decoded = QLIT_QLIST(((LiteralQObject[]){
+ QLIT_QINT(43),
+ QLIT_QDICT(((LiteralQDictEntry[]){
+ { "a", QLIT_QINT(32) },
+ { "h", QLIT_QSTR("b") },
+ { } })),
+ QLIT_QLIST(((LiteralQObject[]){
+ { } })),
+ QLIT_QINT(42),
+ { }
+ })),
+ },
+ { }
+ };
+
+ for (i = 0; test_cases[i].encoded; i++) {
+ QObject *obj;
+ QString *str;
+
+ obj = qobject_from_json(test_cases[i].encoded);
+ g_assert(obj != NULL);
+ g_assert(qobject_type(obj) == QTYPE_QLIST);
+
+ g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
+
+ str = qobject_to_json(obj, true);
+ qobject_decref(obj);
+ g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
+ QDECREF(str);
+ }
+}
+
static void simple_varargs(void)
{
QObject *embedded_obj;
@@ -1520,6 +1583,7 @@ int main(int argc, char **argv)
g_test_add_func("/lists/simple_list", simple_list);
g_test_add_func("/whitespace/simple_whitespace", simple_whitespace);
+ g_test_add_func("/whitespace/simple_pretty", simple_pretty);
g_test_add_func("/varargs/simple_varargs", simple_varargs);
--
2.5.5
- [Qemu-devel] [PATCH v4 07/28] string-input-visitor: Favor new visit_free() function, (continued)
- [Qemu-devel] [PATCH v4 07/28] string-input-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 01/28] qapi: Rename (one) qjson.h to qobject-json.h, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 09/28] string-output-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 04/28] qapi: Add parameter to visit_end_*, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 15/28] replay: Use new QAPI cloning, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 11/28] tests: Factor out common code in qapi output tests, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 10/28] qmp-output-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 14/28] sockets: Use new QAPI cloning, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 16/28] qapi: Factor out JSON string escaping, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 23/28] tests: Test qobject_to_json() pretty formatting,
Eric Blake <=
- [Qemu-devel] [PATCH v4 13/28] qapi: Add new clone visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 18/28] qapi: Add qstring_append_printf(), Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 25/28] qapi: Support pretty printing in JSON output visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 26/28] qobject: Implement qobject_to_json() atop JSON visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 27/28] qapi: Add 'any' support to JSON output, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 20/28] qstring: Add qstring_consume_str(), Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 24/28] qapi: Add JSON output visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 22/28] qobject: Consolidate qobject_to_json() calls, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 28/28] qemu-img: Use new JSON output formatter, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 19/28] qapi: Use qstring_append_chr() where appropriate, Eric Blake, 2016/05/19