aboutsummaryrefslogtreecommitdiff
path: root/tests/test-string-output-visitor.c
diff options
context:
space:
mode:
authorEric Blake <eblake@redhat.com>2016-06-09 10:48:42 -0600
committerMarkus Armbruster <armbru@redhat.com>2016-07-06 10:52:04 +0200
commit23d1705f425ca847f11d92e4363cd7f67f369fd8 (patch)
treec768e3e2068f9ddf8b1aae1b3cbf8f8274b7a4fb /tests/test-string-output-visitor.c
parenta8fff94d28bd2f0db37b2194cbfa197ba998b7f7 (diff)
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>
Diffstat (limited to 'tests/test-string-output-visitor.c')
-rw-r--r--tests/test-string-output-visitor.c38
1 files changed, 17 insertions, 21 deletions
diff --git a/tests/test-string-output-visitor.c b/tests/test-string-output-visitor.c
index d57a4d3e37..e17035be5e 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);
}
}