diff options
author | Eric Blake <eblake@redhat.com> | 2016-06-09 10:48:42 -0600 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2016-07-06 10:52:04 +0200 |
commit | 23d1705f425ca847f11d92e4363cd7f67f369fd8 (patch) | |
tree | c768e3e2068f9ddf8b1aae1b3cbf8f8274b7a4fb | |
parent | a8fff94d28bd2f0db37b2194cbfa197ba998b7f7 (diff) | |
download | qemu-23d1705f425ca847f11d92e4363cd7f67f369fd8.zip qemu-23d1705f425ca847f11d92e4363cd7f67f369fd8.tar.gz qemu-23d1705f425ca847f11d92e4363cd7f67f369fd8.tar.bz2 |
tests: Factor out common code in qapi output tests
Create a new visitor_get() function to capture common
actions taken in collecting output from an output visitor,
to make it easier to refactor the output visitors in a
later patch.
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1465490926-28625-12-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
-rw-r--r-- | tests/test-qmp-output-visitor.c | 78 | ||||
-rw-r--r-- | tests/test-string-output-visitor.c | 38 |
2 files changed, 44 insertions, 72 deletions
diff --git a/tests/test-qmp-output-visitor.c b/tests/test-qmp-output-visitor.c index 45d87fb..af24b10 100644 --- a/tests/test-qmp-output-visitor.c +++ b/tests/test-qmp-output-visitor.c @@ -23,6 +23,7 @@ typedef struct TestOutputVisitorData { QmpOutputVisitor *qov; Visitor *ov; + QObject *obj; } TestOutputVisitorData; static void visitor_output_setup(TestOutputVisitorData *data, @@ -41,6 +42,15 @@ static void visitor_output_teardown(TestOutputVisitorData *data, visit_free(data->ov); data->qov = NULL; data->ov = NULL; + qobject_decref(data->obj); + data->obj = NULL; +} + +static QObject *visitor_get(TestOutputVisitorData *data) +{ + data->obj = qmp_output_get_qobject(data->qov); + g_assert(data->obj); + return data->obj; } static void visitor_reset(TestOutputVisitorData *data) @@ -57,12 +67,9 @@ static void test_visitor_out_int(TestOutputVisitorData *data, visit_type_int(data->ov, NULL, &value, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QINT); g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value); - - qobject_decref(obj); } static void test_visitor_out_bool(TestOutputVisitorData *data, @@ -73,12 +80,9 @@ static void test_visitor_out_bool(TestOutputVisitorData *data, visit_type_bool(data->ov, NULL, &value, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QBOOL); g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value); - - qobject_decref(obj); } static void test_visitor_out_number(TestOutputVisitorData *data, @@ -89,12 +93,9 @@ static void test_visitor_out_number(TestOutputVisitorData *data, visit_type_number(data->ov, NULL, &value, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QFLOAT); g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value); - - qobject_decref(obj); } static void test_visitor_out_string(TestOutputVisitorData *data, @@ -105,12 +106,9 @@ static void test_visitor_out_string(TestOutputVisitorData *data, visit_type_str(data->ov, NULL, &string, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QSTRING); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); - - qobject_decref(obj); } static void test_visitor_out_no_string(TestOutputVisitorData *data, @@ -122,12 +120,9 @@ static void test_visitor_out_no_string(TestOutputVisitorData *data, /* A null string should return "" */ visit_type_str(data->ov, NULL, &string, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QSTRING); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, ""); - - qobject_decref(obj); } static void test_visitor_out_enum(TestOutputVisitorData *data, @@ -139,12 +134,10 @@ static void test_visitor_out_enum(TestOutputVisitorData *data, for (i = 0; i < ENUM_ONE__MAX; i++) { visit_type_EnumOne(data->ov, "unused", &i, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QSTRING); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, EnumOne_lookup[i]); - qobject_decref(obj); visitor_reset(data); } } @@ -177,8 +170,7 @@ static void test_visitor_out_struct(TestOutputVisitorData *data, visit_type_TestStruct(data->ov, NULL, &p, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QDICT); qdict = qobject_to_qdict(obj); @@ -186,8 +178,6 @@ static void test_visitor_out_struct(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42); g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false); g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo"); - - QDECREF(qdict); } static void test_visitor_out_struct_nested(TestOutputVisitorData *data, @@ -222,8 +212,7 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data, visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QDICT); qdict = qobject_to_qdict(obj); @@ -250,7 +239,6 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); - QDECREF(qdict); qapi_free_UserDefTwo(ud2); } @@ -302,8 +290,7 @@ static void test_visitor_out_list(TestOutputVisitorData *data, visit_type_TestStructList(data->ov, NULL, &head, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QLIST); qlist = qobject_to_qlist(obj); @@ -324,7 +311,6 @@ static void test_visitor_out_list(TestOutputVisitorData *data, } g_assert_cmpint(i, ==, max_items); - QDECREF(qlist); qapi_free_TestStructList(head); } @@ -368,11 +354,9 @@ static void test_visitor_out_any(TestOutputVisitorData *data, qobj = QOBJECT(qint_from_int(-42)); visit_type_any(data->ov, NULL, &qobj, &error_abort); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); g_assert(qobject_type(obj) == QTYPE_QINT); g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42); - qobject_decref(obj); qobject_decref(qobj); visitor_reset(data); @@ -383,8 +367,7 @@ static void test_visitor_out_any(TestOutputVisitorData *data, qobj = QOBJECT(qdict); visit_type_any(data->ov, NULL, &qobj, &error_abort); qobject_decref(qobj); - obj = qmp_output_get_qobject(data->qov); - g_assert(obj != NULL); + obj = visitor_get(data); qdict = qobject_to_qdict(obj); g_assert(qdict); qobj = qdict_get(qdict, "integer"); @@ -402,7 +385,6 @@ static void test_visitor_out_any(TestOutputVisitorData *data, qstring = qobject_to_qstring(qobj); g_assert(qstring); g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); - qobject_decref(obj); } static void test_visitor_out_union_flat(TestOutputVisitorData *data, @@ -418,7 +400,7 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data, tmp->u.value1.boolean = true; visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort); - arg = qmp_output_get_qobject(data->qov); + arg = visitor_get(data); g_assert(qobject_type(arg) == QTYPE_QDICT); qdict = qobject_to_qdict(arg); @@ -429,7 +411,6 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true); qapi_free_UserDefFlatUnion(tmp); - QDECREF(qdict); } static void test_visitor_out_alternate(TestOutputVisitorData *data, @@ -444,13 +425,12 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.i = 42; visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); - arg = qmp_output_get_qobject(data->qov); + arg = visitor_get(data); g_assert(qobject_type(arg) == QTYPE_QINT); g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42); qapi_free_UserDefAlternate(tmp); - qobject_decref(arg); visitor_reset(data); tmp = g_new0(UserDefAlternate, 1); @@ -458,13 +438,12 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.s = g_strdup("hello"); visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); - arg = qmp_output_get_qobject(data->qov); + arg = visitor_get(data); g_assert(qobject_type(arg) == QTYPE_QSTRING); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello"); qapi_free_UserDefAlternate(tmp); - qobject_decref(arg); visitor_reset(data); tmp = g_new0(UserDefAlternate, 1); @@ -475,7 +454,7 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.udfu.u.value1.boolean = true; visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); - arg = qmp_output_get_qobject(data->qov); + arg = visitor_get(data); g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT); qdict = qobject_to_qdict(arg); @@ -486,7 +465,6 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true); qapi_free_UserDefAlternate(tmp); - qobject_decref(arg); } static void test_visitor_out_null(TestOutputVisitorData *data, @@ -500,14 +478,13 @@ static void test_visitor_out_null(TestOutputVisitorData *data, visit_type_null(data->ov, "a", &error_abort); visit_check_struct(data->ov, &error_abort); visit_end_struct(data->ov, NULL); - arg = qmp_output_get_qobject(data->qov); + arg = visitor_get(data); g_assert(qobject_type(arg) == QTYPE_QDICT); qdict = qobject_to_qdict(arg); g_assert_cmpint(qdict_size(qdict), ==, 1); nil = qdict_get(qdict, "a"); g_assert(nil); g_assert(qobject_type(nil) == QTYPE_QNULL); - qobject_decref(arg); } static void init_native_list(UserDefNativeListUnion *cvalue) @@ -738,10 +715,9 @@ static void test_native_list(TestOutputVisitorData *data, visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort); - obj = qmp_output_get_qobject(data->qov); + obj = visitor_get(data); check_native_list(obj, cvalue->type); qapi_free_UserDefNativeListUnion(cvalue); - qobject_decref(obj); } static void test_visitor_out_native_list_int(TestOutputVisitorData *data, diff --git a/tests/test-string-output-visitor.c b/tests/test-string-output-visitor.c index d57a4d3..e17035b 100644 --- a/tests/test-string-output-visitor.c +++ b/tests/test-string-output-visitor.c @@ -22,6 +22,7 @@ typedef struct TestOutputVisitorData { StringOutputVisitor *sov; Visitor *ov; + char *str; bool human; } TestOutputVisitorData; @@ -53,6 +54,15 @@ static void visitor_output_teardown(TestOutputVisitorData *data, visit_free(data->ov); data->sov = NULL; data->ov = NULL; + g_free(data->str); + data->str = NULL; +} + +static char *visitor_get(TestOutputVisitorData *data) +{ + data->str = string_output_get_string(data->sov); + g_assert(data->str); + return data->str; } static void visitor_reset(TestOutputVisitorData *data) @@ -73,14 +83,12 @@ static void test_visitor_out_int(TestOutputVisitorData *data, visit_type_int(data->ov, NULL, &value, &err); g_assert(!err); - str = string_output_get_string(data->sov); - g_assert(str != NULL); + str = visitor_get(data); if (data->human) { g_assert_cmpstr(str, ==, "42 (0x2a)"); } else { g_assert_cmpstr(str, ==, "42"); } - g_free(str); } static void test_visitor_out_intList(TestOutputVisitorData *data, @@ -102,8 +110,7 @@ static void test_visitor_out_intList(TestOutputVisitorData *data, visit_type_intList(data->ov, NULL, &list, &err); g_assert(err == NULL); - str = string_output_get_string(data->sov); - g_assert(str != NULL); + str = visitor_get(data); if (data->human) { g_assert_cmpstr(str, ==, "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 " @@ -113,7 +120,6 @@ static void test_visitor_out_intList(TestOutputVisitorData *data, g_assert_cmpstr(str, ==, "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807"); } - g_free(str); qapi_free_intList(list); } @@ -127,10 +133,8 @@ static void test_visitor_out_bool(TestOutputVisitorData *data, visit_type_bool(data->ov, NULL, &value, &err); g_assert(!err); - str = string_output_get_string(data->sov); - g_assert(str != NULL); + str = visitor_get(data); g_assert_cmpstr(str, ==, "true"); - g_free(str); } static void test_visitor_out_number(TestOutputVisitorData *data, @@ -143,10 +147,8 @@ static void test_visitor_out_number(TestOutputVisitorData *data, visit_type_number(data->ov, NULL, &value, &err); g_assert(!err); - str = string_output_get_string(data->sov); - g_assert(str != NULL); + str = visitor_get(data); g_assert_cmpstr(str, ==, "3.140000"); - g_free(str); } static void test_visitor_out_string(TestOutputVisitorData *data, @@ -160,14 +162,12 @@ static void test_visitor_out_string(TestOutputVisitorData *data, visit_type_str(data->ov, NULL, &string, &err); g_assert(!err); - str = string_output_get_string(data->sov); - g_assert(str != NULL); + str = visitor_get(data); if (data->human) { g_assert_cmpstr(str, ==, string_human); } else { g_assert_cmpstr(str, ==, string); } - g_free(str); } static void test_visitor_out_no_string(TestOutputVisitorData *data, @@ -179,14 +179,12 @@ static void test_visitor_out_no_string(TestOutputVisitorData *data, /* A null string should return "" */ visit_type_str(data->ov, NULL, &string, &error_abort); - str = string_output_get_string(data->sov); - g_assert(str != NULL); + str = visitor_get(data); if (data->human) { g_assert_cmpstr(str, ==, "<null>"); } else { g_assert_cmpstr(str, ==, ""); } - g_free(str); } static void test_visitor_out_enum(TestOutputVisitorData *data, @@ -198,8 +196,7 @@ static void test_visitor_out_enum(TestOutputVisitorData *data, for (i = 0; i < ENUM_ONE__MAX; i++) { visit_type_EnumOne(data->ov, "unused", &i, &error_abort); - str = string_output_get_string(data->sov); - g_assert(str != NULL); + str = visitor_get(data); if (data->human) { char *str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]); @@ -208,7 +205,6 @@ static void test_visitor_out_enum(TestOutputVisitorData *data, } else { g_assert_cmpstr(str, ==, EnumOne_lookup[i]); } - g_free(str); visitor_reset(data); } } |