gobject: Add to_string() functions for Enum and Flags types

These are useful for debugging.

https://bugzilla.gnome.org/show_bug.cgi?id=447907
This commit is contained in:
Garrett Regier
2015-05-14 03:09:30 -07:00
committed by Philip Withnall
parent 625936343d
commit 6c95cd22e9
5 changed files with 186 additions and 11 deletions

View File

@@ -15,6 +15,7 @@ test_enum_basic (void)
GEnumClass *class;
GEnumValue *val;
GValue value = G_VALUE_INIT;
gchar *to_string;
type = g_enum_register_static ("MyEnum", my_enum_values);
@@ -49,6 +50,14 @@ test_enum_basic (void)
val = g_enum_get_value_by_nick (class, "purple");
g_assert (val == NULL);
to_string = g_enum_to_string (type, 2);
g_assert_cmpstr (to_string, ==, "the second value");
g_free (to_string);
to_string = g_enum_to_string (type, 15);
g_assert_cmpstr (to_string, ==, "15");
g_free (to_string);
g_type_class_unref (class);
}
@@ -61,6 +70,12 @@ static const GFlagsValue my_flag_values[] =
{ 0, NULL, NULL }
};
static const GFlagsValue no_default_flag_values[] =
{
{ 1, "the first flag", "one" },
{ 0, NULL, NULL }
};
static void
test_flags_transform_to_string (const GValue *value)
{
@@ -74,12 +89,15 @@ test_flags_transform_to_string (const GValue *value)
static void
test_flags_basic (void)
{
GType type;
GType type, no_default_type;
GFlagsClass *class;
GFlagsValue *val;
GValue value = G_VALUE_INIT;
gchar *to_string;
type = g_flags_register_static ("MyFlags", my_flag_values);
no_default_type = g_flags_register_static ("NoDefaultFlags",
no_default_flag_values);
g_value_init (&value, type);
g_assert (G_VALUE_HOLDS_FLAGS (&value));
@@ -113,6 +131,30 @@ test_flags_basic (void)
test_flags_transform_to_string (&value);
g_value_unset (&value);
to_string = g_flags_to_string (type, 1|8);
g_assert_cmpstr (to_string, ==, "the first flag | the third flag");
g_free (to_string);
to_string = g_flags_to_string (type, 0);
g_assert_cmpstr (to_string, ==, "no flags");
g_free (to_string);
to_string = g_flags_to_string (type, 16);
g_assert_cmpstr (to_string, ==, "0x10");
g_free (to_string);
to_string = g_flags_to_string (type, 1|16);
g_assert_cmpstr (to_string, ==, "the first flag | 0x10");
g_free (to_string);
to_string = g_flags_to_string (no_default_type, 0);
g_assert_cmpstr (to_string, ==, "0x0");
g_free (to_string);
to_string = g_flags_to_string (no_default_type, 16);
g_assert_cmpstr (to_string, ==, "0x10");
g_free (to_string);
g_type_class_unref (class);
}