[json-glib] tests: Improve coverage
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib] tests: Improve coverage
- Date: Fri, 26 Oct 2012 17:12:23 +0000 (UTC)
commit e83baeab7ce16db4597247e793932fd2e507e855
Author: Emmanuele Bassi <ebassi gnome org>
Date: Fri Oct 26 17:21:48 2012 +0100
tests: Improve coverage
json-glib/tests/serialize-full.c | 156 +++++++++++++++++++++++++-------------
1 files changed, 104 insertions(+), 52 deletions(-)
---
diff --git a/json-glib/tests/serialize-full.c b/json-glib/tests/serialize-full.c
index 69280d4..14867f2 100644
--- a/json-glib/tests/serialize-full.c
+++ b/json-glib/tests/serialize-full.c
@@ -8,6 +8,7 @@
#include <json-glib/json-gobject.h>
#define TEST_TYPE_ENUM (test_enum_get_type ())
+#define TEST_TYPE_FLAGS (test_flags_get_type ())
#define TEST_TYPE_BOXED (test_boxed_get_type ())
#define TEST_TYPE_OBJECT (test_object_get_type ())
#define TEST_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_OBJECT, TestObject))
@@ -22,6 +23,13 @@ typedef enum {
TEST_ENUM_BAZ
} TestEnum;
+typedef enum {
+ TEST_FLAGS_NONE = 0,
+ TEST_FLAGS_FOO = 1 << 0,
+ TEST_FLAGS_BAR = 1 << 1,
+ TEST_FLAGS_BAZ = 1 << 2
+} TestFlags;
+
typedef struct _TestBoxed TestBoxed;
typedef struct _TestObject TestObject;
typedef struct _TestObjectClass TestObjectClass;
@@ -36,14 +44,15 @@ struct _TestObject
{
GObject parent_instance;
- gint foo;
- gboolean bar;
- gchar *baz;
- TestBoxed blah;
- TestEnum meh;
- gchar **mah;
+ gint m_int;
+ gboolean m_bool;
+ gchar *m_str;
+ TestBoxed m_boxed;
+ TestEnum m_enum;
+ gchar **m_strv;
+ TestFlags m_flags;
- TestObject *test;
+ TestObject *m_obj;
};
struct _TestObjectClass
@@ -107,6 +116,27 @@ test_enum_get_type (void)
return e_type;
}
+GType
+test_flags_get_type (void)
+{
+ static GType e_type = 0;
+
+ if (G_UNLIKELY (e_type == 0))
+ {
+ static const GFlagsValue values[] = {
+ { TEST_FLAGS_NONE, "TEST_FLAGS_NONE", "none" },
+ { TEST_FLAGS_FOO, "TEST_FLAGS_FOO", "foo" },
+ { TEST_FLAGS_BAR, "TEST_FLAGS_BAR", "bar" },
+ { TEST_FLAGS_BAZ, "TEST_FLAGS_BAZ", "baz" },
+ { 0, NULL, NULL }
+ };
+
+ e_type = g_flags_register_static ("TestFlags", values);
+ }
+
+ return e_type;
+}
+
enum
{
PROP_0,
@@ -117,6 +147,7 @@ enum
PROP_BLAH,
PROP_MEH,
PROP_MAH,
+ PROP_FLAGS,
PROP_TEST
};
@@ -177,11 +208,11 @@ json_serializable_iface_init (gpointer g_iface)
static void
test_object_finalize (GObject *gobject)
{
- g_free (TEST_OBJECT (gobject)->baz);
- g_strfreev (TEST_OBJECT (gobject)->mah);
+ g_free (TEST_OBJECT (gobject)->m_str);
+ g_strfreev (TEST_OBJECT (gobject)->m_strv);
- if (TEST_OBJECT (gobject)->test != NULL)
- g_object_unref (TEST_OBJECT (gobject)->test);
+ if (TEST_OBJECT (gobject)->m_obj != NULL)
+ g_object_unref (TEST_OBJECT (gobject)->m_obj);
G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
}
@@ -195,28 +226,34 @@ test_object_set_property (GObject *gobject,
switch (prop_id)
{
case PROP_FOO:
- TEST_OBJECT (gobject)->foo = g_value_get_int (value);
+ TEST_OBJECT (gobject)->m_int = g_value_get_int (value);
break;
case PROP_BAR:
- TEST_OBJECT (gobject)->bar = g_value_get_boolean (value);
+ TEST_OBJECT (gobject)->m_bool = g_value_get_boolean (value);
break;
case PROP_BAZ:
- g_free (TEST_OBJECT (gobject)->baz);
- TEST_OBJECT (gobject)->baz = g_value_dup_string (value);
+ g_free (TEST_OBJECT (gobject)->m_str);
+ TEST_OBJECT (gobject)->m_str = g_value_dup_string (value);
break;
case PROP_MEH:
- TEST_OBJECT (gobject)->meh = g_value_get_enum (value);
+ TEST_OBJECT (gobject)->m_enum = g_value_get_enum (value);
break;
case PROP_MAH:
- TEST_OBJECT (gobject)->mah = g_strdupv (g_value_get_boxed (value));
+ g_strfreev (TEST_OBJECT (gobject)->m_strv);
+ TEST_OBJECT (gobject)->m_strv = g_strdupv (g_value_get_boxed (value));
+ break;
+
+ case PROP_FLAGS:
+ TEST_OBJECT (gobject)->m_flags = g_value_get_flags (value);
break;
case PROP_TEST:
- TEST_OBJECT (gobject)->test = g_value_dup_object (value);
+ g_clear_object (&(TEST_OBJECT (gobject)->m_obj));
+ TEST_OBJECT (gobject)->m_obj = g_value_dup_object (value);
break;
default:
@@ -233,31 +270,35 @@ test_object_get_property (GObject *gobject,
switch (prop_id)
{
case PROP_FOO:
- g_value_set_int (value, TEST_OBJECT (gobject)->foo);
+ g_value_set_int (value, TEST_OBJECT (gobject)->m_int);
break;
case PROP_BAR:
- g_value_set_boolean (value, TEST_OBJECT (gobject)->bar);
+ g_value_set_boolean (value, TEST_OBJECT (gobject)->m_bool);
break;
case PROP_BAZ:
- g_value_set_string (value, TEST_OBJECT (gobject)->baz);
+ g_value_set_string (value, TEST_OBJECT (gobject)->m_str);
break;
case PROP_BLAH:
- g_value_set_boxed (value, &(TEST_OBJECT (gobject)->blah));
+ g_value_set_boxed (value, &(TEST_OBJECT (gobject)->m_boxed));
break;
case PROP_MEH:
- g_value_set_enum (value, TEST_OBJECT (gobject)->meh);
+ g_value_set_enum (value, TEST_OBJECT (gobject)->m_enum);
break;
case PROP_MAH:
- g_value_set_boxed (value, TEST_OBJECT (gobject)->mah);
+ g_value_set_boxed (value, TEST_OBJECT (gobject)->m_strv);
+ break;
+
+ case PROP_FLAGS:
+ g_value_set_flags (value, TEST_OBJECT (gobject)->m_flags);
break;
case PROP_TEST:
- g_value_set_object (value, TEST_OBJECT (gobject)->test);
+ g_value_set_object (value, TEST_OBJECT (gobject)->m_obj);
break;
default:
@@ -308,6 +349,13 @@ test_object_class_init (TestObjectClass *klass)
G_TYPE_STRV,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
+ PROP_FLAGS,
+ g_param_spec_flags ("flags", "Flags", "Flags",
+ TEST_TYPE_FLAGS,
+ TEST_FLAGS_NONE,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT));
+ g_object_class_install_property (gobject_class,
PROP_TEST,
g_param_spec_object ("test", "Test", "Test",
TEST_TYPE_OBJECT,
@@ -317,18 +365,20 @@ test_object_class_init (TestObjectClass *klass)
static void
test_object_init (TestObject *object)
{
- object->foo = 0;
- object->bar = FALSE;
- object->baz = NULL;
+ object->m_int = 0;
+ object->m_bool = FALSE;
+ object->m_str = NULL;
+
+ object->m_boxed.foo = object->m_int;
+ object->m_boxed.bar = object->m_bool;
- object->blah.foo = object->foo;
- object->blah.bar = object->bar;
+ object->m_enum = TEST_ENUM_BAR;
- object->meh = TEST_ENUM_BAR;
+ object->m_strv = NULL;
- object->mah = NULL;
+ object->m_flags = TEST_FLAGS_NONE;
- object->test = NULL;
+ object->m_obj = NULL;
}
static const gchar *var_test =
@@ -339,9 +389,10 @@ static const gchar *var_test =
" \"meh\" : \"baz\",\n"
" \"mah\" : [ \"hello\", \", \", \"world\", \"!\" ],\n"
" \"test\" : {\n"
-" \"bar\" : true,\n"
-" \"baz\" : \"world\",\n"
-" \"meh\" : \"foo\"\n"
+" \"bar\" : true,\n"
+" \"baz\" : \"world\",\n"
+" \"meh\" : 0,\n"
+" \"flags\" : \"foo|bar\""
" }\n"
"}";
@@ -364,28 +415,29 @@ test_deserialize (void)
" bar: %s\n"
" baz: %s\n"
" meh: %s\n",
- TEST_OBJECT (object)->foo == 42 ? "<true>" : "<false>",
- TEST_OBJECT (object)->bar == TRUE ? "<true>" : "<false>",
- TEST_OBJECT (object)->baz != NULL ? "<true>" : "<false>",
- TEST_OBJECT (object)->meh == TEST_ENUM_BAZ ? "<true>" : "<false>");
+ TEST_OBJECT (object)->m_int == 42 ? "<true>" : "<false>",
+ TEST_OBJECT (object)->m_bool == TRUE ? "<true>" : "<false>",
+ TEST_OBJECT (object)->m_str != NULL ? "<true>" : "<false>",
+ TEST_OBJECT (object)->m_enum == TEST_ENUM_BAZ ? "<true>" : "<false>");
- g_assert_cmpint (TEST_OBJECT (object)->foo, ==, 42);
- g_assert (TEST_OBJECT (object)->bar);
- g_assert_cmpstr (TEST_OBJECT (object)->baz, ==, "hello");
- g_assert_cmpint (TEST_OBJECT (object)->meh, ==, TEST_ENUM_BAZ);
+ g_assert_cmpint (TEST_OBJECT (object)->m_int, ==, 42);
+ g_assert (TEST_OBJECT (object)->m_bool);
+ g_assert_cmpstr (TEST_OBJECT (object)->m_str, ==, "hello");
+ g_assert_cmpint (TEST_OBJECT (object)->m_enum, ==, TEST_ENUM_BAZ);
- g_assert (TEST_OBJECT (object)->mah != NULL);
- g_assert_cmpint (g_strv_length (TEST_OBJECT (object)->mah), ==, 4);
+ g_assert (TEST_OBJECT (object)->m_strv != NULL);
+ g_assert_cmpint (g_strv_length (TEST_OBJECT (object)->m_strv), ==, 4);
- str = g_strjoinv (NULL, TEST_OBJECT (object)->mah);
+ str = g_strjoinv (NULL, TEST_OBJECT (object)->m_strv);
g_assert_cmpstr (str, ==, "hello, world!");
g_free (str);
- g_assert (TEST_IS_OBJECT (TEST_OBJECT (object)->test));
- test = TEST_OBJECT (TEST_OBJECT (object)->test);
- g_assert (test->bar);
- g_assert_cmpstr (test->baz, ==, "world");
- g_assert_cmpint (test->meh, ==, TEST_ENUM_FOO);
+ g_assert (TEST_IS_OBJECT (TEST_OBJECT (object)->m_obj));
+ test = TEST_OBJECT (TEST_OBJECT (object)->m_obj);
+ g_assert (test->m_bool);
+ g_assert_cmpstr (test->m_str, ==, "world");
+ g_assert_cmpint (test->m_enum, ==, TEST_ENUM_FOO);
+ g_assert_cmpint (test->m_flags, ==, TEST_FLAGS_FOO | TEST_FLAGS_BAR);
g_object_unref (object);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]