Convert tests/gobject/accumulator.c to glib test framework

This commit is contained in:
Emmanuel Fleury 2022-03-13 17:42:01 +01:00 committed by Philip Withnall
parent ce585ba4d2
commit d378108b10

View File

@ -15,16 +15,7 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestAccumulator"
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#include <string.h>
#include <glib-object.h>
#include <glib-object.h>
#include "testmarshal.h"
#include "testcommon.h"
@ -37,9 +28,7 @@
* emission on TRUE returns.
*/
/*
* TestObject, a parent class for TestObject
*/
/* TestObject, a parent class for TestObject */
#define TEST_TYPE_OBJECT (test_object_get_type ())
typedef struct _TestObject TestObject;
typedef struct _TestObjectClass TestObjectClass;
@ -53,9 +42,9 @@ struct _TestObjectClass
GObjectClass parent_class;
gchar* (*test_signal1) (TestObject *tobject,
gint param);
gint param);
gboolean (*test_signal2) (TestObject *tobject,
gint param);
gint param);
GVariant* (*test_signal3) (TestObject *tobject,
gboolean *weak_ptr);
};
@ -64,9 +53,9 @@ static GType test_object_get_type (void);
static gboolean
test_signal1_accumulator (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer data)
GValue *return_accu,
const GValue *handler_return,
gpointer data)
{
const gchar *accu_string = g_value_get_string (return_accu);
const gchar *new_string = g_value_get_string (handler_return);
@ -86,30 +75,30 @@ test_signal1_accumulator (GSignalInvocationHint *ihint,
static gchar *
test_object_signal1_callback_before (TestObject *tobject,
gint param,
gpointer data)
gint param,
gpointer data)
{
return g_strdup ("<before>");
}
static gchar *
test_object_real_signal1 (TestObject *tobject,
gint param)
gint param)
{
return g_strdup ("<default>");
}
static gchar *
test_object_signal1_callback_after (TestObject *tobject,
gint param,
gpointer data)
gint param,
gpointer data)
{
return g_strdup ("<after>");
}
static gboolean
test_object_signal2_callback_before (TestObject *tobject,
gint param)
gint param)
{
switch (param)
{
@ -125,7 +114,7 @@ test_object_signal2_callback_before (TestObject *tobject,
static gboolean
test_object_real_signal2 (TestObject *tobject,
gint param)
gint param)
{
switch (param)
{
@ -134,14 +123,14 @@ test_object_real_signal2 (TestObject *tobject,
case 3: return FALSE;
case 4: return FALSE;
}
g_assert_not_reached ();
return FALSE;
}
static gboolean
test_object_signal2_callback_after (TestObject *tobject,
gint param)
gint param)
{
switch (param)
{
@ -150,21 +139,21 @@ test_object_signal2_callback_after (TestObject *tobject,
case 3: return TRUE;
case 4: return FALSE;
}
g_assert_not_reached ();
return FALSE;
}
static gboolean
test_signal3_accumulator (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer data)
GValue *return_accu,
const GValue *handler_return,
gpointer data)
{
GVariant *variant;
variant = g_value_get_variant (handler_return);
g_assert (!g_variant_is_floating (variant));
g_assert_false (g_variant_is_floating (variant));
g_value_set_variant (return_accu, variant);
@ -219,89 +208,97 @@ test_object_class_init (TestObjectClass *class)
class->test_signal1 = test_object_real_signal1;
class->test_signal2 = test_object_real_signal2;
class->test_signal3 = test_object_real_signal3;
g_signal_new ("test-signal1",
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal1),
test_signal1_accumulator, NULL,
test_marshal_STRING__INT,
G_TYPE_STRING, 1, G_TYPE_INT);
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal1),
test_signal1_accumulator, NULL,
test_marshal_STRING__INT,
G_TYPE_STRING, 1, G_TYPE_INT);
g_signal_new ("test-signal2",
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal2),
g_signal_accumulator_true_handled, NULL,
test_marshal_BOOLEAN__INT,
G_TYPE_BOOLEAN, 1, G_TYPE_INT);
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal2),
g_signal_accumulator_true_handled, NULL,
test_marshal_BOOLEAN__INT,
G_TYPE_BOOLEAN, 1, G_TYPE_INT);
g_signal_new ("test-signal3",
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal3),
test_signal3_accumulator, NULL,
test_marshal_VARIANT__POINTER,
G_TYPE_VARIANT, 1, G_TYPE_POINTER);
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal3),
test_signal3_accumulator, NULL,
test_marshal_VARIANT__POINTER,
G_TYPE_VARIANT, 1, G_TYPE_POINTER);
}
static DEFINE_TYPE(TestObject, test_object,
test_object_class_init, NULL, NULL,
G_TYPE_OBJECT)
test_object_class_init, NULL, NULL,
G_TYPE_OBJECT);
int
main (int argc,
char *argv[])
static void
test_accumulator (void)
{
TestObject *object;
gchar *string_result;
gboolean bool_result;
gboolean variant_finalised;
GVariant *variant_result;
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_signal_connect (object, "test-signal1",
G_CALLBACK (test_object_signal1_callback_before), NULL);
G_CALLBACK (test_object_signal1_callback_before), NULL);
g_signal_connect_after (object, "test-signal1",
G_CALLBACK (test_object_signal1_callback_after), NULL);
G_CALLBACK (test_object_signal1_callback_after), NULL);
g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
g_assert (strcmp (string_result, "<before><default><after>") == 0);
g_assert_cmpstr (string_result, ==, "<before><default><after>");
g_free (string_result);
g_signal_connect (object, "test-signal2",
G_CALLBACK (test_object_signal2_callback_before), NULL);
G_CALLBACK (test_object_signal2_callback_before), NULL);
g_signal_connect_after (object, "test-signal2",
G_CALLBACK (test_object_signal2_callback_after), NULL);
G_CALLBACK (test_object_signal2_callback_after), NULL);
bool_result = FALSE;
g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
g_assert (bool_result == TRUE);
g_assert_true (bool_result);
bool_result = FALSE;
g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
g_assert (bool_result == TRUE);
g_assert_true (bool_result);
bool_result = FALSE;
g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
g_assert (bool_result == TRUE);
g_assert_true (bool_result);
bool_result = TRUE;
g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
g_assert (bool_result == FALSE);
g_assert_false (bool_result);
variant_finalised = FALSE;
variant_result = NULL;
g_signal_emit_by_name (object, "test-signal3", &variant_finalised, &variant_result);
g_assert (variant_result != NULL);
g_assert (!g_variant_is_floating (variant_result));
g_assert_nonnull (variant_result);
g_assert_false (g_variant_is_floating (variant_result));
/* Test that variant_result had refcount 1 */
g_assert (!variant_finalised);
g_assert_false (variant_finalised);
g_variant_unref (variant_result);
g_assert (variant_finalised);
g_assert_true (variant_finalised);
g_object_unref (object);
return 0;
}
int
main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/accumulator", test_accumulator);
return g_test_run ();
}