tests/qapi-schema: Cover flat union types

The test demonstrates a generator bug: the generated struct
UserDefFlatUnion doesn't include members for the indirect base
UserDefZero.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
This commit is contained in:
Markus Armbruster 2014-03-01 08:40:33 +01:00 committed by Luiz Capitulino
parent 7ad993b480
commit 2fc0043283
5 changed files with 93 additions and 0 deletions

View File

@ -37,6 +37,13 @@
'base': 'UserDefZero', 'base': 'UserDefZero',
'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } } 'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } }
{ 'union': 'UserDefFlatUnion',
'base': 'UserDefOne',
'discriminator': 'string',
'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } }
# FIXME generated struct UserDefFlatUnion has members for direct base
# UserDefOne, but lacks members for indirect base UserDefZero
{ 'union': 'UserDefAnonUnion', { 'union': 'UserDefAnonUnion',
'discriminator': {}, 'discriminator': {},
'data': { 'uda': 'UserDefA', 's': 'str', 'i': 'int' } } 'data': { 'uda': 'UserDefA', 's': 'str', 'i': 'int' } }

View File

@ -7,6 +7,7 @@
OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]), OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]), OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
OrderedDict([('union', 'UserDefUnion'), ('base', 'UserDefZero'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]), OrderedDict([('union', 'UserDefUnion'), ('base', 'UserDefZero'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]),
OrderedDict([('union', 'UserDefFlatUnion'), ('base', 'UserDefOne'), ('discriminator', 'string'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]),
OrderedDict([('union', 'UserDefAnonUnion'), ('discriminator', OrderedDict()), ('data', OrderedDict([('uda', 'UserDefA'), ('s', 'str'), ('i', 'int')]))]), OrderedDict([('union', 'UserDefAnonUnion'), ('discriminator', OrderedDict()), ('data', OrderedDict([('uda', 'UserDefA'), ('s', 'str'), ('i', 'int')]))]),
OrderedDict([('union', 'UserDefNativeListUnion'), ('data', OrderedDict([('integer', ['int']), ('s8', ['int8']), ('s16', ['int16']), ('s32', ['int32']), ('s64', ['int64']), ('u8', ['uint8']), ('u16', ['uint16']), ('u32', ['uint32']), ('u64', ['uint64']), ('number', ['number']), ('boolean', ['bool']), ('string', ['str'])]))]), OrderedDict([('union', 'UserDefNativeListUnion'), ('data', OrderedDict([('integer', ['int']), ('s8', ['int8']), ('s16', ['int16']), ('s32', ['int32']), ('s64', ['int64']), ('u8', ['uint8']), ('u16', ['uint16']), ('u32', ['uint32']), ('u64', ['uint64']), ('number', ['number']), ('boolean', ['bool']), ('string', ['str'])]))]),
OrderedDict([('command', 'user_def_cmd'), ('data', OrderedDict())]), OrderedDict([('command', 'user_def_cmd'), ('data', OrderedDict())]),
@ -16,6 +17,7 @@
OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))])] OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))])]
['EnumOne', ['EnumOne',
'UserDefUnionKind', 'UserDefUnionKind',
'UserDefFlatUnionKind',
'UserDefAnonUnionKind', 'UserDefAnonUnionKind',
'UserDefNativeListUnionKind'] 'UserDefNativeListUnionKind']
[OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]), [OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]),

View File

@ -139,6 +139,21 @@ static void test_validate_union(TestInputVisitorData *data,
qapi_free_UserDefUnion(tmp); qapi_free_UserDefUnion(tmp);
} }
static void test_validate_union_flat(TestInputVisitorData *data,
const void *unused)
{
UserDefFlatUnion *tmp = NULL;
Visitor *v;
Error *errp = NULL;
v = validate_test_init(data, "{ 'string': 'a', 'boolean': true }");
/* TODO when generator bug is fixed, add 'integer': 41 */
visit_type_UserDefFlatUnion(v, &tmp, NULL, &errp);
g_assert(!error_is_set(&errp));
qapi_free_UserDefFlatUnion(tmp);
}
static void test_validate_union_anon(TestInputVisitorData *data, static void test_validate_union_anon(TestInputVisitorData *data,
const void *unused) const void *unused)
{ {
@ -212,6 +227,20 @@ static void test_validate_fail_union(TestInputVisitorData *data,
qapi_free_UserDefUnion(tmp); qapi_free_UserDefUnion(tmp);
} }
static void test_validate_fail_union_flat(TestInputVisitorData *data,
const void *unused)
{
UserDefFlatUnion *tmp = NULL;
Error *errp = NULL;
Visitor *v;
v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
visit_type_UserDefFlatUnion(v, &tmp, NULL, &errp);
g_assert(error_is_set(&errp));
qapi_free_UserDefFlatUnion(tmp);
}
static void test_validate_fail_union_anon(TestInputVisitorData *data, static void test_validate_fail_union_anon(TestInputVisitorData *data,
const void *unused) const void *unused)
{ {
@ -248,6 +277,8 @@ int main(int argc, char **argv)
&testdata, test_validate_list); &testdata, test_validate_list);
validate_test_add("/visitor/input-strict/pass/union", validate_test_add("/visitor/input-strict/pass/union",
&testdata, test_validate_union); &testdata, test_validate_union);
validate_test_add("/visitor/input-strict/pass/union-flat",
&testdata, test_validate_union_flat);
validate_test_add("/visitor/input-strict/pass/union-anon", validate_test_add("/visitor/input-strict/pass/union-anon",
&testdata, test_validate_union_anon); &testdata, test_validate_union_anon);
validate_test_add("/visitor/input-strict/fail/struct", validate_test_add("/visitor/input-strict/fail/struct",
@ -258,6 +289,8 @@ int main(int argc, char **argv)
&testdata, test_validate_fail_list); &testdata, test_validate_fail_list);
validate_test_add("/visitor/input-strict/fail/union", validate_test_add("/visitor/input-strict/fail/union",
&testdata, test_validate_fail_union); &testdata, test_validate_fail_union);
validate_test_add("/visitor/input-strict/fail/union-flat",
&testdata, test_validate_fail_union_flat);
validate_test_add("/visitor/input-strict/fail/union-anon", validate_test_add("/visitor/input-strict/fail/union-anon",
&testdata, test_validate_fail_union_anon); &testdata, test_validate_fail_union_anon);

View File

@ -303,6 +303,24 @@ static void test_visitor_in_union(TestInputVisitorData *data,
qapi_free_UserDefUnion(tmp); qapi_free_UserDefUnion(tmp);
} }
static void test_visitor_in_union_flat(TestInputVisitorData *data,
const void *unused)
{
Visitor *v;
Error *err = NULL;
UserDefFlatUnion *tmp;
v = visitor_input_test_init(data, "{ 'string': 'a', 'boolean': true }");
/* TODO when generator bug is fixed, add 'integer': 41 */
visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
g_assert(err == NULL);
g_assert_cmpint(tmp->kind, ==, USER_DEF_UNION_KIND_A);
/* TODO g_assert_cmpint(tmp->integer, ==, 41); */
g_assert_cmpint(tmp->a->boolean, ==, true);
qapi_free_UserDefFlatUnion(tmp);
}
static void test_visitor_in_union_anon(TestInputVisitorData *data, static void test_visitor_in_union_anon(TestInputVisitorData *data,
const void *unused) const void *unused)
{ {
@ -652,6 +670,8 @@ int main(int argc, char **argv)
&in_visitor_data, test_visitor_in_list); &in_visitor_data, test_visitor_in_list);
input_visitor_test_add("/visitor/input/union", input_visitor_test_add("/visitor/input/union",
&in_visitor_data, test_visitor_in_union); &in_visitor_data, test_visitor_in_union);
input_visitor_test_add("/visitor/input/union-flat",
&in_visitor_data, test_visitor_in_union_flat);
input_visitor_test_add("/visitor/input/union-anon", input_visitor_test_add("/visitor/input/union-anon",
&in_visitor_data, test_visitor_in_union_anon); &in_visitor_data, test_visitor_in_union_anon);
input_visitor_test_add("/visitor/input/errors", input_visitor_test_add("/visitor/input/errors",

View File

@ -440,6 +440,35 @@ static void test_visitor_out_union(TestOutputVisitorData *data,
QDECREF(qdict); QDECREF(qdict);
} }
static void test_visitor_out_union_flat(TestOutputVisitorData *data,
const void *unused)
{
QObject *arg;
QDict *qdict;
Error *err = NULL;
UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
tmp->kind = USER_DEF_UNION_KIND_A;
tmp->a = g_malloc0(sizeof(UserDefA));
/* TODO when generator bug is fixed: tmp->integer = 41; */
tmp->a->boolean = true;
visit_type_UserDefFlatUnion(data->ov, &tmp, NULL, &err);
g_assert(err == NULL);
arg = qmp_output_get_qobject(data->qov);
g_assert(qobject_type(arg) == QTYPE_QDICT);
qdict = qobject_to_qdict(arg);
g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "a");
/* TODO g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41); */
g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
qapi_free_UserDefFlatUnion(tmp);
QDECREF(qdict);
}
static void test_visitor_out_union_anon(TestOutputVisitorData *data, static void test_visitor_out_union_anon(TestOutputVisitorData *data,
const void *unused) const void *unused)
{ {
@ -808,6 +837,8 @@ int main(int argc, char **argv)
&out_visitor_data, test_visitor_out_list_qapi_free); &out_visitor_data, test_visitor_out_list_qapi_free);
output_visitor_test_add("/visitor/output/union", output_visitor_test_add("/visitor/output/union",
&out_visitor_data, test_visitor_out_union); &out_visitor_data, test_visitor_out_union);
output_visitor_test_add("/visitor/output/union-flat",
&out_visitor_data, test_visitor_out_union_flat);
output_visitor_test_add("/visitor/output/union-anon", output_visitor_test_add("/visitor/output/union-anon",
&out_visitor_data, test_visitor_out_union_anon); &out_visitor_data, test_visitor_out_union_anon);
output_visitor_test_add("/visitor/output/native_list/int", output_visitor_test_add("/visitor/output/native_list/int",