2 * String Output Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor)
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
15 #include "qemu-common.h"
16 #include "qapi/string-output-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qemu-objects.h"
21 typedef struct TestOutputVisitorData {
22 StringOutputVisitor *sov;
24 } TestOutputVisitorData;
26 static void visitor_output_setup(TestOutputVisitorData *data,
29 data->sov = string_output_visitor_new();
30 g_assert(data->sov != NULL);
32 data->ov = string_output_get_visitor(data->sov);
33 g_assert(data->ov != NULL);
36 static void visitor_output_teardown(TestOutputVisitorData *data,
39 string_output_visitor_cleanup(data->sov);
44 static void test_visitor_out_int(TestOutputVisitorData *data,
51 visit_type_int(data->ov, &value, NULL, &errp);
52 g_assert(error_is_set(&errp) == 0);
54 str = string_output_get_string(data->sov);
55 g_assert(str != NULL);
56 g_assert_cmpstr(str, ==, "-42");
60 static void test_visitor_out_bool(TestOutputVisitorData *data,
67 visit_type_bool(data->ov, &value, NULL, &errp);
68 g_assert(error_is_set(&errp) == 0);
70 str = string_output_get_string(data->sov);
71 g_assert(str != NULL);
72 g_assert_cmpstr(str, ==, "true");
76 static void test_visitor_out_number(TestOutputVisitorData *data,
83 visit_type_number(data->ov, &value, NULL, &errp);
84 g_assert(error_is_set(&errp) == 0);
86 str = string_output_get_string(data->sov);
87 g_assert(str != NULL);
88 g_assert_cmpstr(str, ==, "3.140000");
92 static void test_visitor_out_string(TestOutputVisitorData *data,
95 char *string = (char *) "Q E M U";
99 visit_type_str(data->ov, &string, NULL, &errp);
100 g_assert(error_is_set(&errp) == 0);
102 str = string_output_get_string(data->sov);
103 g_assert(str != NULL);
104 g_assert_cmpstr(str, ==, string);
108 static void test_visitor_out_no_string(TestOutputVisitorData *data,
115 /* A null string should return "" */
116 visit_type_str(data->ov, &string, NULL, &errp);
117 g_assert(error_is_set(&errp) == 0);
119 str = string_output_get_string(data->sov);
120 g_assert(str != NULL);
121 g_assert_cmpstr(str, ==, "");
125 static void test_visitor_out_enum(TestOutputVisitorData *data,
132 for (i = 0; i < ENUM_ONE_MAX; i++) {
133 visit_type_EnumOne(data->ov, &i, "unused", &errp);
134 g_assert(!error_is_set(&errp));
136 str = string_output_get_string(data->sov);
137 g_assert(str != NULL);
138 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
143 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
146 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
149 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
151 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
152 g_assert(error_is_set(&errp) == true);
157 static void output_visitor_test_add(const char *testpath,
158 TestOutputVisitorData *data,
159 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
161 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
162 test_func, visitor_output_teardown);
165 int main(int argc, char **argv)
167 TestOutputVisitorData out_visitor_data;
169 g_test_init(&argc, &argv, NULL);
171 output_visitor_test_add("/string-visitor/output/int",
172 &out_visitor_data, test_visitor_out_int);
173 output_visitor_test_add("/string-visitor/output/bool",
174 &out_visitor_data, test_visitor_out_bool);
175 output_visitor_test_add("/string-visitor/output/number",
176 &out_visitor_data, test_visitor_out_number);
177 output_visitor_test_add("/string-visitor/output/string",
178 &out_visitor_data, test_visitor_out_string);
179 output_visitor_test_add("/string-visitor/output/no-string",
180 &out_visitor_data, test_visitor_out_no_string);
181 output_visitor_test_add("/string-visitor/output/enum",
182 &out_visitor_data, test_visitor_out_enum);
183 output_visitor_test_add("/string-visitor/output/enum-errors",
184 &out_visitor_data, test_visitor_out_enum_errors);