diff --git a/gio/gliststore.c b/gio/gliststore.c index 5b7d42678..ae8e2c1d8 100644 --- a/gio/gliststore.c +++ b/gio/gliststore.c @@ -125,10 +125,8 @@ g_list_store_set_property (GObject *object, switch (property_id) { case PROP_ITEM_TYPE: /* construct-only */ + g_assert (g_type_is_a (g_value_get_gtype (value), G_TYPE_OBJECT)); store->item_type = g_value_get_gtype (value); - if (!g_type_is_a (store->item_type, G_TYPE_OBJECT)) - g_critical ("GListStore cannot store items of type '%s'. Items must be GObjects.", - g_type_name (store->item_type)); break; default: diff --git a/gio/tests/glistmodel.c b/gio/tests/glistmodel.c index dcf571f29..533e2e47d 100644 --- a/gio/tests/glistmodel.c +++ b/gio/tests/glistmodel.c @@ -21,6 +21,53 @@ #include +/* Wrapper around g_list_model_get_item() and g_list_model_get_object() which + * checks they return the same thing. */ +static gpointer +list_model_get (GListModel *model, + guint position) +{ + GObject *item = g_list_model_get_item (model, position); + GObject *object = g_list_model_get_object (model, position); + + g_assert_true (item == object); + + g_clear_object (&object); + return g_steal_pointer (&item); +} + +/* Test that constructing/getting/setting properties on a #GListStore works. */ +static void +test_store_properties (void) +{ + GListStore *store = NULL; + GType item_type; + + store = g_list_store_new (G_TYPE_MENU_ITEM); + g_object_get (store, "item-type", &item_type, NULL); + g_assert_cmpint (item_type, ==, G_TYPE_MENU_ITEM); + + g_clear_object (&store); +} + +/* Test that #GListStore rejects non-GObject item types. */ +static void +test_store_non_gobjects (void) +{ + if (g_test_subprocess ()) + { + /* We have to use g_object_new() since g_list_store_new() checks the item + * type. We want to check the property setter code works properly. */ + g_object_new (G_TYPE_LIST_STORE, "item-type", G_TYPE_LONG, NULL); + return; + } + + g_test_trap_subprocess (NULL, 0, 0); + g_test_trap_assert_failed (); + g_test_trap_assert_stderr ("*WARNING*value * of type 'GType' is invalid or " + "out of range for property 'item-type'*"); +} + static void test_store_boundaries (void) { @@ -92,7 +139,7 @@ test_store_refcounts (void) store = g_list_store_new (G_TYPE_MENU_ITEM); g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 0); - g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), 0)); + g_assert_null (list_model_get (G_LIST_MODEL (store), 0)); n_items = G_N_ELEMENTS (items); for (i = 0; i < n_items; i++) @@ -106,17 +153,17 @@ test_store_refcounts (void) } g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, n_items); - g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), n_items)); + g_assert_null (list_model_get (G_LIST_MODEL (store), n_items)); - tmp = g_list_model_get_item (G_LIST_MODEL (store), 3); - g_assert (tmp == items[3]); + tmp = list_model_get (G_LIST_MODEL (store), 3); + g_assert_true (tmp == items[3]); g_object_unref (tmp); g_list_store_remove (store, 4); g_assert_null (items[4]); n_items--; g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, n_items); - g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), n_items)); + g_assert_null (list_model_get (G_LIST_MODEL (store), n_items)); g_object_unref (store); for (i = 0; i < G_N_ELEMENTS (items); i++) @@ -189,8 +236,8 @@ test_store_sorted (void) GObject *a, *b; /* should see our two copies */ - a = g_list_model_get_item (G_LIST_MODEL (store), i * 2); - b = g_list_model_get_item (G_LIST_MODEL (store), i * 2 + 1); + a = list_model_get (G_LIST_MODEL (store), i * 2); + b = list_model_get (G_LIST_MODEL (store), i * 2 + 1); g_assert (compare_items (a, b, GUINT_TO_POINTER(0x1234)) == 0); g_assert (a != b); @@ -199,7 +246,7 @@ test_store_sorted (void) { GObject *c; - c = g_list_model_get_item (G_LIST_MODEL (store), i * 2 - 1); + c = list_model_get (G_LIST_MODEL (store), i * 2 - 1); g_assert (c != a); g_assert (c != b); @@ -241,13 +288,13 @@ test_store_splice_replace_middle (void) g_list_store_splice (store, 0, 0, array->pdata, 3); g_assert_cmpuint (g_list_model_get_n_items (model), ==, 3); - item = g_list_model_get_item (model, 0); + item = list_model_get (model, 0); g_assert_cmpstr (g_action_get_name (item), ==, "1"); g_object_unref (item); - item = g_list_model_get_item (model, 1); + item = list_model_get (model, 1); g_assert_cmpstr (g_action_get_name (item), ==, "2"); g_object_unref (item); - item = g_list_model_get_item (model, 2); + item = list_model_get (model, 2); g_assert_cmpstr (g_action_get_name (item), ==, "3"); g_object_unref (item); @@ -255,16 +302,16 @@ test_store_splice_replace_middle (void) g_list_store_splice (store, 1, 1, array->pdata + 3, 2); g_assert_cmpuint (g_list_model_get_n_items (model), ==, 4); - item = g_list_model_get_item (model, 0); + item = list_model_get (model, 0); g_assert_cmpstr (g_action_get_name (item), ==, "1"); g_object_unref (item); - item = g_list_model_get_item (model, 1); + item = list_model_get (model, 1); g_assert_cmpstr (g_action_get_name (item), ==, "4"); g_object_unref (item); - item = g_list_model_get_item (model, 2); + item = list_model_get (model, 2); g_assert_cmpstr (g_action_get_name (item), ==, "5"); g_object_unref (item); - item = g_list_model_get_item (model, 3); + item = list_model_get (model, 3); g_assert_cmpstr (g_action_get_name (item), ==, "3"); g_object_unref (item); @@ -296,10 +343,10 @@ test_store_splice_replace_all (void) g_list_store_splice (store, 0, 0, array->pdata, 2); g_assert_cmpuint (g_list_model_get_n_items (model), ==, 2); - item = g_list_model_get_item (model, 0); + item = list_model_get (model, 0); g_assert_cmpstr (g_action_get_name (item), ==, "1"); g_object_unref (item); - item = g_list_model_get_item (model, 1); + item = list_model_get (model, 1); g_assert_cmpstr (g_action_get_name (item), ==, "2"); g_object_unref (item); @@ -307,10 +354,10 @@ test_store_splice_replace_all (void) g_list_store_splice (store, 0, 2, array->pdata + 2, 2); g_assert_cmpuint (g_list_model_get_n_items (model), ==, 2); - item = g_list_model_get_item (model, 0); + item = list_model_get (model, 0); g_assert_cmpstr (g_action_get_name (item), ==, "3"); g_object_unref (item); - item = g_list_model_get_item (model, 1); + item = list_model_get (model, 1); g_assert_cmpstr (g_action_get_name (item), ==, "4"); g_object_unref (item); @@ -344,7 +391,7 @@ test_store_splice_noop (void) g_list_store_splice (store, 1, 0, NULL, 0); g_assert_cmpuint (g_list_model_get_n_items (model), ==, 1); - item = g_list_model_get_item (model, 0); + item = list_model_get (model, 0); g_assert_cmpstr (g_action_get_name (item), ==, "1"); g_object_unref (item); @@ -364,7 +411,7 @@ model_array_equal (GListModel *model, GPtrArray *array) GObject *ptr; gboolean ptrs_equal; - ptr = g_list_model_get_item (model, i); + ptr = list_model_get (model, i); ptrs_equal = (g_ptr_array_index (array, i) == ptr); g_object_unref (ptr); if (!ptrs_equal) @@ -589,33 +636,33 @@ test_store_get_item_cache (void) g_list_store_append (store, item2); /* Clear the cache */ - g_assert_null (g_list_model_get_item (model, 42)); + g_assert_null (list_model_get (model, 42)); /* Access the same position twice */ - temp = g_list_model_get_item (model, 1); + temp = list_model_get (model, 1); g_assert (temp == item2); g_object_unref (temp); - temp = g_list_model_get_item (model, 1); + temp = list_model_get (model, 1); g_assert (temp == item2); g_object_unref (temp); - g_assert_null (g_list_model_get_item (model, 42)); + g_assert_null (list_model_get (model, 42)); /* Access forwards */ - temp = g_list_model_get_item (model, 0); + temp = list_model_get (model, 0); g_assert (temp == item1); g_object_unref (temp); - temp = g_list_model_get_item (model, 1); + temp = list_model_get (model, 1); g_assert (temp == item2); g_object_unref (temp); - g_assert_null (g_list_model_get_item (model, 42)); + g_assert_null (list_model_get (model, 42)); /* Access backwards */ - temp = g_list_model_get_item (model, 1); + temp = list_model_get (model, 1); g_assert (temp == item2); g_object_unref (temp); - temp = g_list_model_get_item (model, 0); + temp = list_model_get (model, 0); g_assert (temp == item1); g_object_unref (temp); @@ -736,6 +783,8 @@ int main (int argc, char *argv[]) g_test_init (&argc, &argv, NULL); g_test_bug_base ("https://bugzilla.gnome.org/"); + g_test_add_func ("/glistmodel/store/properties", test_store_properties); + g_test_add_func ("/glistmodel/store/non-gobjects", test_store_non_gobjects); g_test_add_func ("/glistmodel/store/boundaries", test_store_boundaries); g_test_add_func ("/glistmodel/store/refcounts", test_store_refcounts); g_test_add_func ("/glistmodel/store/sorted", test_store_sorted); diff --git a/glib/gtestutils.c b/glib/gtestutils.c index 7b29c274e..119c55d92 100644 --- a/glib/gtestutils.c +++ b/glib/gtestutils.c @@ -3343,7 +3343,8 @@ g_test_trap_assertions (const char *domain, logged_child_output = logged_child_output || log_child_output (process_id); - msg = g_strdup_printf ("stdout of child process (%s) %s: %s", process_id, match_error, stdout_pattern); + msg = g_strdup_printf ("stdout of child process (%s) %s: %s\nstderr was:\n%s", + process_id, match_error, stdout_pattern, test_trap_last_stdout); g_assertion_message (domain, file, line, func, msg); g_free (msg); } @@ -3353,7 +3354,8 @@ g_test_trap_assertions (const char *domain, logged_child_output = logged_child_output || log_child_output (process_id); - msg = g_strdup_printf ("stderr of child process (%s) %s: %s", process_id, match_error, stderr_pattern); + msg = g_strdup_printf ("stderr of child process (%s) %s: %s\nstderr was:\n%s", + process_id, match_error, stderr_pattern, test_trap_last_stderr); g_assertion_message (domain, file, line, func, msg); g_free (msg); }