mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-03 22:52:09 +01:00
Convert ifaceproperties.c to test framework
Also add a test that checks warnings for failure to implement interface properties. See https://bugzilla.gnome.org/show_bug.cgi?id=637738
This commit is contained in:
parent
58e36daf29
commit
2a5e0cf9e0
@ -17,13 +17,7 @@
|
|||||||
* Boston, MA 02111-1307, USA.
|
* Boston, MA 02111-1307, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#undef G_LOG_DOMAIN
|
#include <stdlib.h>
|
||||||
#define G_LOG_DOMAIN "TestIfaceProperties"
|
|
||||||
|
|
||||||
#undef G_DISABLE_ASSERT
|
|
||||||
#undef G_DISABLE_CHECKS
|
|
||||||
#undef G_DISABLE_CAST_CHECKS
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <glib-object.h>
|
#include <glib-object.h>
|
||||||
@ -398,8 +392,7 @@ static DEFINE_TYPE (DerivedObject, derived_object,
|
|||||||
derived_object_class_init, NULL, NULL,
|
derived_object_class_init, NULL, NULL,
|
||||||
BASE_TYPE_OBJECT)
|
BASE_TYPE_OBJECT)
|
||||||
|
|
||||||
/* Helper function for testing ...list_properties()
|
/* Helper function for testing ...list_properties() */
|
||||||
*/
|
|
||||||
static void
|
static void
|
||||||
assert_in_properties (GParamSpec *param_spec,
|
assert_in_properties (GParamSpec *param_spec,
|
||||||
GParamSpec **properties,
|
GParamSpec **properties,
|
||||||
@ -417,27 +410,15 @@ assert_in_properties (GParamSpec *param_spec,
|
|||||||
g_assert (found);
|
g_assert (found);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
/* Test setting and getting the properties */
|
||||||
main (gint argc,
|
static void
|
||||||
gchar *argv[])
|
test_set (void)
|
||||||
{
|
{
|
||||||
BaseObject *object;
|
BaseObject *object;
|
||||||
GObjectClass *object_class;
|
|
||||||
TestIfaceClass *iface_vtable;
|
|
||||||
GParamSpec **properties;
|
|
||||||
guint n_properties;
|
|
||||||
|
|
||||||
gint val1, val2, val3, val4;
|
gint val1, val2, val3, val4;
|
||||||
|
|
||||||
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
|
|
||||||
G_LOG_LEVEL_WARNING |
|
|
||||||
G_LOG_LEVEL_CRITICAL);
|
|
||||||
g_type_init ();
|
|
||||||
|
|
||||||
object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
|
object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
|
||||||
|
|
||||||
/* Test setting and getting the properties
|
|
||||||
*/
|
|
||||||
g_object_set (object,
|
g_object_set (object,
|
||||||
"prop1", 0x0101,
|
"prop1", 0x0101,
|
||||||
"prop2", 0x0202,
|
"prop2", 0x0202,
|
||||||
@ -456,8 +437,17 @@ main (gint argc,
|
|||||||
g_assert (val3 == 0x0303);
|
g_assert (val3 == 0x0303);
|
||||||
g_assert (val4 == 0x0404);
|
g_assert (val4 == 0x0404);
|
||||||
|
|
||||||
/* Test that the right spec is passed on explicit notifications
|
g_object_unref (object);
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
/* Test that the right spec is passed on explicit notifications */
|
||||||
|
static void
|
||||||
|
test_notify (void)
|
||||||
|
{
|
||||||
|
BaseObject *object;
|
||||||
|
|
||||||
|
object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
|
||||||
|
|
||||||
g_object_freeze_notify (G_OBJECT (object));
|
g_object_freeze_notify (G_OBJECT (object));
|
||||||
g_object_notify (G_OBJECT (object), "prop1");
|
g_object_notify (G_OBJECT (object), "prop1");
|
||||||
g_object_notify (G_OBJECT (object), "prop2");
|
g_object_notify (G_OBJECT (object), "prop2");
|
||||||
@ -465,17 +455,33 @@ main (gint argc,
|
|||||||
g_object_notify (G_OBJECT (object), "prop4");
|
g_object_notify (G_OBJECT (object), "prop4");
|
||||||
g_object_thaw_notify (G_OBJECT (object));
|
g_object_thaw_notify (G_OBJECT (object));
|
||||||
|
|
||||||
/* Test g_object_class_find_property() for overridden properties
|
g_object_unref (object);
|
||||||
*/
|
}
|
||||||
object_class = G_OBJECT_GET_CLASS (object);
|
|
||||||
|
/* Test g_object_class_find_property() for overridden properties */
|
||||||
|
static void
|
||||||
|
test_find_overridden (void)
|
||||||
|
{
|
||||||
|
GObjectClass *object_class;
|
||||||
|
|
||||||
|
object_class = g_type_class_peek (DERIVED_TYPE_OBJECT);
|
||||||
|
|
||||||
g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1);
|
g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1);
|
||||||
g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2);
|
g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2);
|
||||||
g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3);
|
g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3);
|
||||||
g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4);
|
g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test g_object_class_list_properties() for overridden properties */
|
||||||
|
static void
|
||||||
|
test_list_overridden (void)
|
||||||
|
{
|
||||||
|
GObjectClass *object_class;
|
||||||
|
GParamSpec **properties;
|
||||||
|
guint n_properties;
|
||||||
|
|
||||||
|
object_class = g_type_class_peek (DERIVED_TYPE_OBJECT);
|
||||||
|
|
||||||
/* Test g_object_class_list_properties() for overridden properties
|
|
||||||
*/
|
|
||||||
properties = g_object_class_list_properties (object_class, &n_properties);
|
properties = g_object_class_list_properties (object_class, &n_properties);
|
||||||
g_assert (n_properties == 4);
|
g_assert (n_properties == 4);
|
||||||
assert_in_properties (inherited_spec1, properties, n_properties);
|
assert_in_properties (inherited_spec1, properties, n_properties);
|
||||||
@ -483,25 +489,160 @@ main (gint argc,
|
|||||||
assert_in_properties (inherited_spec3, properties, n_properties);
|
assert_in_properties (inherited_spec3, properties, n_properties);
|
||||||
assert_in_properties (inherited_spec4, properties, n_properties);
|
assert_in_properties (inherited_spec4, properties, n_properties);
|
||||||
g_free (properties);
|
g_free (properties);
|
||||||
|
}
|
||||||
|
|
||||||
/* Test g_object_interface_find_property()
|
/* Test g_object_interface_find_property() */
|
||||||
*/
|
static void
|
||||||
iface_vtable = g_type_default_interface_peek (TEST_TYPE_IFACE);
|
test_find_interface (void)
|
||||||
|
{
|
||||||
|
TestIfaceClass *iface;
|
||||||
|
|
||||||
g_assert (g_object_interface_find_property (iface_vtable, "prop1") == iface_spec1);
|
iface = g_type_default_interface_peek (TEST_TYPE_IFACE);
|
||||||
g_assert (g_object_interface_find_property (iface_vtable, "prop2") == iface_spec2);
|
|
||||||
g_assert (g_object_interface_find_property (iface_vtable, "prop3") == iface_spec3);
|
|
||||||
|
|
||||||
/* Test g_object_interface_list_properties()
|
g_assert (g_object_interface_find_property (iface, "prop1") == iface_spec1);
|
||||||
*/
|
g_assert (g_object_interface_find_property (iface, "prop2") == iface_spec2);
|
||||||
properties = g_object_interface_list_properties (iface_vtable, &n_properties);
|
g_assert (g_object_interface_find_property (iface, "prop3") == iface_spec3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test g_object_interface_list_properties() */
|
||||||
|
static void
|
||||||
|
test_list_interface (void)
|
||||||
|
{
|
||||||
|
TestIfaceClass *iface;
|
||||||
|
GParamSpec **properties;
|
||||||
|
guint n_properties;
|
||||||
|
|
||||||
|
iface = g_type_default_interface_peek (TEST_TYPE_IFACE);
|
||||||
|
|
||||||
|
properties = g_object_interface_list_properties (iface, &n_properties);
|
||||||
g_assert (n_properties == 3);
|
g_assert (n_properties == 3);
|
||||||
assert_in_properties (iface_spec1, properties, n_properties);
|
assert_in_properties (iface_spec1, properties, n_properties);
|
||||||
assert_in_properties (iface_spec2, properties, n_properties);
|
assert_in_properties (iface_spec2, properties, n_properties);
|
||||||
assert_in_properties (iface_spec3, properties, n_properties);
|
assert_in_properties (iface_spec3, properties, n_properties);
|
||||||
g_free (properties);
|
g_free (properties);
|
||||||
|
}
|
||||||
g_object_unref (object);
|
|
||||||
|
/* Base2Object, which implements the interface but fails
|
||||||
return 0;
|
* to override some of its properties
|
||||||
|
*/
|
||||||
|
#define BASE2_TYPE_OBJECT (base2_object_get_type ())
|
||||||
|
#define BASE2_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE2_TYPE_OBJECT, Base2Object))
|
||||||
|
|
||||||
|
typedef struct _Base2Object Base2Object;
|
||||||
|
typedef struct _Base2ObjectClass Base2ObjectClass;
|
||||||
|
|
||||||
|
static void
|
||||||
|
base2_object_test_iface_init (TestIfaceClass *iface)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BASE2_PROP_0,
|
||||||
|
BASE2_PROP1,
|
||||||
|
BASE2_PROP2
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _Base2Object
|
||||||
|
{
|
||||||
|
GObject parent_instance;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _Base2ObjectClass
|
||||||
|
{
|
||||||
|
GObjectClass parent_class;
|
||||||
|
};
|
||||||
|
|
||||||
|
G_DEFINE_TYPE_WITH_CODE (Base2Object, base2_object, G_TYPE_OBJECT,
|
||||||
|
G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE,
|
||||||
|
base2_object_test_iface_init))
|
||||||
|
|
||||||
|
static void
|
||||||
|
base2_object_get_property (GObject *object,
|
||||||
|
guint prop_id,
|
||||||
|
GValue *value,
|
||||||
|
GParamSpec *pspec)
|
||||||
|
{
|
||||||
|
switch (prop_id)
|
||||||
|
{
|
||||||
|
case BASE2_PROP1:
|
||||||
|
g_value_set_int (value, 0);
|
||||||
|
break;
|
||||||
|
case BASE2_PROP2:
|
||||||
|
g_value_set_int (value, 0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
base2_object_set_property (GObject *object,
|
||||||
|
guint prop_id,
|
||||||
|
const GValue *value,
|
||||||
|
GParamSpec *pspec)
|
||||||
|
{
|
||||||
|
switch (prop_id)
|
||||||
|
{
|
||||||
|
case BASE2_PROP1:
|
||||||
|
break;
|
||||||
|
case BASE2_PROP2:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
base2_object_class_init (Base2ObjectClass *class)
|
||||||
|
{
|
||||||
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
||||||
|
|
||||||
|
object_class->set_property = base2_object_set_property;
|
||||||
|
object_class->get_property = base2_object_get_property;
|
||||||
|
|
||||||
|
g_object_class_override_property (object_class, BASE2_PROP1, "prop1");
|
||||||
|
g_object_class_override_property (object_class, BASE2_PROP2, "prop2");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
base2_object_init (Base2Object *object)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_not_overridden (void)
|
||||||
|
{
|
||||||
|
g_test_bug ("637738");
|
||||||
|
|
||||||
|
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT|G_TEST_TRAP_SILENCE_STDERR))
|
||||||
|
{
|
||||||
|
Base2Object *object;
|
||||||
|
|
||||||
|
object = g_object_new (BASE2_TYPE_OBJECT, NULL);
|
||||||
|
g_object_unref (object);
|
||||||
|
exit (0);
|
||||||
|
}
|
||||||
|
g_test_trap_assert_failed ();
|
||||||
|
g_test_trap_assert_stderr ("*Base2Object doesn't implement property 'prop3' from interface 'TestIface'*");
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
g_type_init ();
|
||||||
|
|
||||||
|
g_test_init (&argc, &argv, NULL);
|
||||||
|
g_test_bug_base ("http://bugzilla.gnome.org/");
|
||||||
|
|
||||||
|
g_test_add_func ("/interface/properties/set", test_set);
|
||||||
|
g_test_add_func ("/interface/properties/notify", test_notify);
|
||||||
|
g_test_add_func ("/interface/properties/find-overridden", test_find_overridden);
|
||||||
|
g_test_add_func ("/interface/properties/list-overridden", test_list_overridden);
|
||||||
|
g_test_add_func ("/interface/properties/find-interface", test_find_interface);
|
||||||
|
g_test_add_func ("/interface/properties/list-interface", test_list_interface);
|
||||||
|
g_test_add_func ("/interface/properties/not-overridden", test_not_overridden);
|
||||||
|
|
||||||
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user