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 "qapi/string-output-visitor.h"
16 #include "test-qapi-types.h"
17 #include "test-qapi-visit.h"
18 #include "qemu-objects.h"
20 typedef struct TestOutputVisitorData {
21 StringOutputVisitor *sov;
23 } TestOutputVisitorData;
25 static void visitor_output_setup(TestOutputVisitorData *data,
28 data->sov = string_output_visitor_new();
29 g_assert(data->sov != NULL);
31 data->ov = string_output_get_visitor(data->sov);
32 g_assert(data->ov != NULL);
35 static void visitor_output_teardown(TestOutputVisitorData *data,
38 string_output_visitor_cleanup(data->sov);
43 static void test_visitor_out_int(TestOutputVisitorData *data,
50 visit_type_int(data->ov, &value, NULL, &errp);
51 g_assert(error_is_set(&errp) == 0);
53 str = string_output_get_string(data->sov);
54 g_assert(str != NULL);
55 g_assert_cmpstr(str, ==, "-42");
59 static void test_visitor_out_bool(TestOutputVisitorData *data,
66 visit_type_bool(data->ov, &value, NULL, &errp);
67 g_assert(error_is_set(&errp) == 0);
69 str = string_output_get_string(data->sov);
70 g_assert(str != NULL);
71 g_assert_cmpstr(str, ==, "true");
75 static void test_visitor_out_number(TestOutputVisitorData *data,
82 visit_type_number(data->ov, &value, NULL, &errp);
83 g_assert(error_is_set(&errp) == 0);
85 str = string_output_get_string(data->sov);
86 g_assert(str != NULL);
87 g_assert_cmpstr(str, ==, "3.140000");
91 static void test_visitor_out_string(TestOutputVisitorData *data,
94 char *string = (char *) "Q E M U";
98 visit_type_str(data->ov, &string, NULL, &errp);
99 g_assert(error_is_set(&errp) == 0);
101 str = string_output_get_string(data->sov);
102 g_assert(str != NULL);
103 g_assert_cmpstr(str, ==, string);
107 static void test_visitor_out_no_string(TestOutputVisitorData *data,
114 /* A null string should return "" */
115 visit_type_str(data->ov, &string, NULL, &errp);
116 g_assert(error_is_set(&errp) == 0);
118 str = string_output_get_string(data->sov);
119 g_assert(str != NULL);
120 g_assert_cmpstr(str, ==, "");
124 static void test_visitor_out_enum(TestOutputVisitorData *data,
131 for (i = 0; i < ENUM_ONE_MAX; i++) {
132 visit_type_EnumOne(data->ov, &i, "unused", &errp);
133 g_assert(!error_is_set(&errp));
135 str = string_output_get_string(data->sov);
136 g_assert(str != NULL);
137 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
142 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
145 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
148 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
150 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
151 g_assert(error_is_set(&errp) == true);
156 static void output_visitor_test_add(const char *testpath,
157 TestOutputVisitorData *data,
158 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
160 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
161 test_func, visitor_output_teardown);
164 int main(int argc, char **argv)
166 TestOutputVisitorData out_visitor_data;
168 g_test_init(&argc, &argv, NULL);
170 output_visitor_test_add("/string-visitor/output/int",
171 &out_visitor_data, test_visitor_out_int);
172 output_visitor_test_add("/string-visitor/output/bool",
173 &out_visitor_data, test_visitor_out_bool);
174 output_visitor_test_add("/string-visitor/output/number",
175 &out_visitor_data, test_visitor_out_number);
176 output_visitor_test_add("/string-visitor/output/string",
177 &out_visitor_data, test_visitor_out_string);
178 output_visitor_test_add("/string-visitor/output/no-string",
179 &out_visitor_data, test_visitor_out_no_string);
180 output_visitor_test_add("/string-visitor/output/enum",
181 &out_visitor_data, test_visitor_out_enum);
182 output_visitor_test_add("/string-visitor/output/enum-errors",
183 &out_visitor_data, test_visitor_out_enum_errors);