2 * String Input Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-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.
16 #include "qapi/string-input-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qemu-objects.h"
21 typedef struct TestInputVisitorData
{
22 StringInputVisitor
*siv
;
23 } TestInputVisitorData
;
25 static void visitor_input_teardown(TestInputVisitorData
*data
,
29 string_input_visitor_cleanup(data
->siv
);
34 /* This is provided instead of a test setup function so that the JSON
35 string used by the tests are kept in the test functions (and not
38 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
43 data
->siv
= string_input_visitor_new(string
);
44 g_assert(data
->siv
!= NULL
);
46 v
= string_input_get_visitor(data
->siv
);
52 static void test_visitor_in_int(TestInputVisitorData
*data
,
55 int64_t res
= 0, value
= -42;
59 v
= visitor_input_test_init(data
, "-42");
61 visit_type_int(v
, &res
, NULL
, &errp
);
62 g_assert(!error_is_set(&errp
));
63 g_assert_cmpint(res
, ==, value
);
66 static void test_visitor_in_bool(TestInputVisitorData
*data
,
73 v
= visitor_input_test_init(data
, "true");
75 visit_type_bool(v
, &res
, NULL
, &errp
);
76 g_assert(!error_is_set(&errp
));
77 g_assert_cmpint(res
, ==, true);
78 visitor_input_teardown(data
, unused
);
80 v
= visitor_input_test_init(data
, "yes");
82 visit_type_bool(v
, &res
, NULL
, &errp
);
83 g_assert(!error_is_set(&errp
));
84 g_assert_cmpint(res
, ==, true);
85 visitor_input_teardown(data
, unused
);
87 v
= visitor_input_test_init(data
, "on");
89 visit_type_bool(v
, &res
, NULL
, &errp
);
90 g_assert(!error_is_set(&errp
));
91 g_assert_cmpint(res
, ==, true);
92 visitor_input_teardown(data
, unused
);
94 v
= visitor_input_test_init(data
, "false");
96 visit_type_bool(v
, &res
, NULL
, &errp
);
97 g_assert(!error_is_set(&errp
));
98 g_assert_cmpint(res
, ==, false);
99 visitor_input_teardown(data
, unused
);
101 v
= visitor_input_test_init(data
, "no");
103 visit_type_bool(v
, &res
, NULL
, &errp
);
104 g_assert(!error_is_set(&errp
));
105 g_assert_cmpint(res
, ==, false);
106 visitor_input_teardown(data
, unused
);
108 v
= visitor_input_test_init(data
, "off");
110 visit_type_bool(v
, &res
, NULL
, &errp
);
111 g_assert(!error_is_set(&errp
));
112 g_assert_cmpint(res
, ==, false);
115 static void test_visitor_in_number(TestInputVisitorData
*data
,
118 double res
= 0, value
= 3.14;
122 v
= visitor_input_test_init(data
, "3.14");
124 visit_type_number(v
, &res
, NULL
, &errp
);
125 g_assert(!error_is_set(&errp
));
126 g_assert_cmpfloat(res
, ==, value
);
129 static void test_visitor_in_string(TestInputVisitorData
*data
,
132 char *res
= NULL
, *value
= (char *) "Q E M U";
136 v
= visitor_input_test_init(data
, value
);
138 visit_type_str(v
, &res
, NULL
, &errp
);
139 g_assert(!error_is_set(&errp
));
140 g_assert_cmpstr(res
, ==, value
);
145 static void test_visitor_in_enum(TestInputVisitorData
*data
,
152 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
155 v
= visitor_input_test_init(data
, EnumOne_lookup
[i
]);
157 visit_type_EnumOne(v
, &res
, NULL
, &errp
);
158 g_assert(!error_is_set(&errp
));
159 g_assert_cmpint(i
, ==, res
);
161 visitor_input_teardown(data
, NULL
);
167 static void input_visitor_test_add(const char *testpath
,
168 TestInputVisitorData
*data
,
169 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
171 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
172 visitor_input_teardown
);
175 int main(int argc
, char **argv
)
177 TestInputVisitorData in_visitor_data
;
179 g_test_init(&argc
, &argv
, NULL
);
181 input_visitor_test_add("/string-visitor/input/int",
182 &in_visitor_data
, test_visitor_in_int
);
183 input_visitor_test_add("/string-visitor/input/bool",
184 &in_visitor_data
, test_visitor_in_bool
);
185 input_visitor_test_add("/string-visitor/input/number",
186 &in_visitor_data
, test_visitor_in_number
);
187 input_visitor_test_add("/string-visitor/input/string",
188 &in_visitor_data
, test_visitor_in_string
);
189 input_visitor_test_add("/string-visitor/input/enum",
190 &in_visitor_data
, test_visitor_in_enum
);