From: Matthias Clasen Date: Sun, 27 Nov 2011 21:51:13 +0000 (-0500) Subject: Add parser roundtrip tests X-Git-Tag: 2.31.4~81 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0760bf5850ebde4d87db035d3b38c1f1873f0d2a;p=platform%2Fupstream%2Fglib.git Add parser roundtrip tests --- diff --git a/gio/tests/gmenumodel.c b/gio/tests/gmenumodel.c index afc5c79..2df3ebc 100644 --- a/gio/tests/gmenumodel.c +++ b/gio/tests/gmenumodel.c @@ -580,7 +580,7 @@ roundtrip_step (gpointer data) } static void -test_roundtrip (void) +test_dbus_roundtrip (void) { struct roundtrip_state state; GDBusConnection *bus; @@ -608,6 +608,160 @@ test_roundtrip (void) g_object_unref (bus); } +static void +start_element (GMarkupParseContext *context, + const gchar *element_name, + const gchar **attribute_names, + const gchar **attribute_values, + gpointer user_data, + GError **error) +{ + if (g_strcmp0 (element_name, "menu") == 0) + g_menu_markup_parser_start_menu (context, "domain", NULL); +} + +static void +end_element (GMarkupParseContext *context, + const gchar *element_name, + gpointer user_data, + GError **error) +{ + GMenu **menu = user_data; + + if (g_strcmp0 (element_name, "menu") == 0) + *menu = g_menu_markup_parser_end_menu (context); +} + +static GMenuModel * +parse_menu_string (const gchar *string, GError **error) +{ + const GMarkupParser parser = { + start_element, end_element, NULL, NULL, NULL + }; + GMarkupParseContext *context; + GMenuModel *menu = NULL; + + context = g_markup_parse_context_new (&parser, 0, &menu, NULL); + g_markup_parse_context_parse (context, string, -1, error); + g_markup_parse_context_free (context); + + return menu; +} + +static gchar * +menu_to_string (GMenuModel *menu) +{ + GString *s; + + s = g_string_new ("\n"); + g_menu_markup_print_string (s, menu, 2, 2); + g_string_append (s, "\n"); + + return g_string_free (s, FALSE); +} + +static void +test_markup_roundtrip (void) +{ + const gchar data[] = + "\n" + "
\n" + " \n" + " '_Undo'\n" + " \n" + " \n" + "
\n" + "
\n" + "
\n" + " \n" + " \n" + " \n" + "
\n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " 'quite-some-action'\n" + " 36\n" + " ['a','b']\n" + " 's(uu)'\n" + " 'small blue thing'\n" + " \n" + "
\n" + "
\n"; + GError *error = NULL; + GMenuModel *a; + GMenuModel *b; + gchar *s; + gchar *s2; + + a = parse_menu_string (data, &error); + g_assert_no_error (error); + g_assert (G_IS_MENU_MODEL (a)); + + /* normalized representation */ + s = menu_to_string (a); + + b = parse_menu_string (s, &error); + g_assert_no_error (error); + g_assert (G_IS_MENU_MODEL (b)); + + assert_menus_equal (G_MENU_MODEL (a), G_MENU_MODEL (b)); + + s2 = menu_to_string (b); + + g_assert_cmpstr (s, ==, s2); + + g_object_unref (a); + g_object_unref (b); + g_free (s); + g_free (s2); +} + +static void +test_attributes (void) +{ + GMenu *menu; + GMenuItem *item; + GVariant *v; + + menu = g_menu_new (); + + item = g_menu_item_new ("test", NULL); + g_menu_item_set_attribute_value (item, "boolean", g_variant_new_boolean (FALSE)); + g_menu_item_set_attribute_value (item, "string", g_variant_new_string ("bla")); + g_menu_item_set_attribute_value (item, "double", g_variant_new_double (1.5)); + v = g_variant_new_parsed ("[('one', 1), ('two', %i), (%s, 3)]", 2, "three"); + g_menu_item_set_attribute_value (item, "complex", v); + g_menu_item_set_attribute_value (item, "test-123", g_variant_new_string ("test-123")); + + g_menu_append_item (menu, item); + + g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (menu)), ==, 1); + + v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "boolean", NULL); + g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN)); + g_variant_unref (v); + + v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "string", NULL); + g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING)); + g_variant_unref (v); + + v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "double", NULL); + g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_DOUBLE)); + g_variant_unref (v); + + v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "complex", NULL); + g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE("a(si)"))); + g_variant_unref (v); + + g_object_unref (menu); +} + /* Epilogue {{{1 */ int main (int argc, char **argv) @@ -618,7 +772,9 @@ main (int argc, char **argv) g_test_add_func ("/gmenu/equality", test_equality); g_test_add_func ("/gmenu/random", test_random); - g_test_add_func ("/gmenu/roundtrip", test_roundtrip); + g_test_add_func ("/gmenu/bus/roundtrip", test_dbus_roundtrip); + g_test_add_func ("/gmenu/markup/roundtrip", test_markup_roundtrip); + g_test_add_func ("/gmenu/attributes", test_attributes); return g_test_run (); }