tests: Don't check qobject_type() before qobject_to_qstring()

qobject_to_qstring(obj) returns NULL when obj isn't a QString.  Check
that instead of qobject_type(obj) == QTYPE_QSTRING.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <1487363905-9480-10-git-send-email-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2017-02-17 21:38:20 +01:00
parent cd17ba51f5
commit 363e13f86e
2 changed files with 25 additions and 43 deletions

View File

@ -54,11 +54,8 @@ static void escaped_string(void)
QString *str;
obj = qobject_from_json(test_cases[i].encoded);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
if (test_cases[i].skip == 0) {
@ -89,11 +86,8 @@ static void simple_string(void)
QString *str;
obj = qobject_from_json(test_cases[i].encoded);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
str = qobject_to_json(obj);
@ -123,11 +117,8 @@ static void single_quote_string(void)
QString *str;
obj = qobject_from_json(test_cases[i].encoded);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
QDECREF(str);
@ -820,9 +811,8 @@ static void utf8_string(void)
obj = qobject_from_json(json_in);
if (utf8_out) {
g_assert(obj);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
} else {
g_assert(!obj);
@ -847,9 +837,8 @@ static void utf8_string(void)
*/
if (0 && json_out != json_in) {
obj = qobject_from_json(json_out);
g_assert(obj);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
}
}
@ -867,15 +856,11 @@ static void vararg_string(void)
};
for (i = 0; test_cases[i].decoded; i++) {
QObject *obj;
QString *str;
obj = qobject_from_jsonf("%s", test_cases[i].decoded);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
str = qobject_to_qstring(qobject_from_jsonf("%s",
test_cases[i].decoded));
g_assert(str);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
QDECREF(str);

View File

@ -97,42 +97,41 @@ static void test_visitor_out_string(TestOutputVisitorData *data,
const void *unused)
{
char *string = (char *) "Q E M U";
QObject *obj;
QString *qstr;
visit_type_str(data->ov, NULL, &string, &error_abort);
obj = visitor_get(data);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);
qstr = qobject_to_qstring(visitor_get(data));
g_assert(qstr);
g_assert_cmpstr(qstring_get_str(qstr), ==, string);
}
static void test_visitor_out_no_string(TestOutputVisitorData *data,
const void *unused)
{
char *string = NULL;
QObject *obj;
QString *qstr;
/* A null string should return "" */
visit_type_str(data->ov, NULL, &string, &error_abort);
obj = visitor_get(data);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
qstr = qobject_to_qstring(visitor_get(data));
g_assert(qstr);
g_assert_cmpstr(qstring_get_str(qstr), ==, "");
}
static void test_visitor_out_enum(TestOutputVisitorData *data,
const void *unused)
{
QObject *obj;
EnumOne i;
QString *qstr;
for (i = 0; i < ENUM_ONE__MAX; i++) {
visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
obj = visitor_get(data);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
EnumOne_lookup[i]);
qstr = qobject_to_qstring(visitor_get(data));
g_assert(qstr);
g_assert_cmpstr(qstring_get_str(qstr), ==, EnumOne_lookup[i]);
visitor_reset(data);
}
}
@ -365,9 +364,7 @@ static void test_visitor_out_any(TestOutputVisitorData *data,
qbool = qobject_to_qbool(qobj);
g_assert(qbool);
g_assert(qbool_get_bool(qbool) == true);
qobj = qdict_get(qdict, "string");
g_assert(qobj);
qstring = qobject_to_qstring(qobj);
qstring = qobject_to_qstring(qdict_get(qdict, "string"));
g_assert(qstring);
g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
}
@ -399,6 +396,7 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data,
{
QObject *arg;
UserDefAlternate *tmp;
QString *qstr;
QDict *qdict;
tmp = g_new0(UserDefAlternate, 1);
@ -419,10 +417,9 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data,
tmp->u.s = g_strdup("hello");
visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
arg = visitor_get(data);
g_assert(qobject_type(arg) == QTYPE_QSTRING);
g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");
qstr = qobject_to_qstring(visitor_get(data));
g_assert(qstr);
g_assert_cmpstr(qstring_get_str(qstr), ==, "hello");
qapi_free_UserDefAlternate(tmp);