gio/tests/task: Also check for formatted error messages

This commit is contained in:
Marco Trevisan (Treviño) 2023-12-19 18:32:10 +01:00
parent 0e475a6e11
commit 39f820e507

View File

@ -129,12 +129,19 @@ test_basic (void)
/* test_error */
typedef struct {
GQuark expected_domain;
int expected_code;
char *expected_message;
gssize int_result;
} TaskErrorResult;
static void
error_callback (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
gssize *result_out = user_data;
TaskErrorResult *result_inout = user_data;
GError *error = NULL;
g_assert (object == NULL);
@ -143,8 +150,9 @@ error_callback (GObject *object,
g_assert (g_task_had_error (G_TASK (result)));
g_assert_false (g_task_get_completed (G_TASK (result)));
*result_out = g_task_propagate_int (G_TASK (result), &error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
result_inout->int_result = g_task_propagate_int (G_TASK (result), &error);
g_assert_error (error, result_inout->expected_domain, result_inout->expected_code);
g_assert_cmpstr (error->message, ==, result_inout->expected_message);
g_error_free (error);
g_assert (g_task_had_error (G_TASK (result)));
@ -159,7 +167,7 @@ error_return (gpointer user_data)
g_task_return_new_error (task,
G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed");
"Failed %p", task);
g_object_unref (task);
return FALSE;
@ -177,12 +185,17 @@ static void
test_error (void)
{
GTask *task;
gssize result;
TaskErrorResult result;
gboolean first_task_data_destroyed = FALSE;
gboolean second_task_data_destroyed = FALSE;
gboolean notification_emitted = FALSE;
task = g_task_new (NULL, NULL, error_callback, &result);
result = (TaskErrorResult){
.expected_domain = G_IO_ERROR,
.expected_code = G_IO_ERROR_FAILED,
.expected_message = g_strdup_printf ("Failed %p", task),
};
g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
g_signal_connect (task, "notify::completed",
(GCallback) completed_cb, &notification_emitted);
@ -199,10 +212,11 @@ test_error (void)
g_idle_add (error_return, task);
g_main_loop_run (loop);
g_assert_cmpint (result, ==, -1);
g_assert_cmpint (result.int_result, ==, -1);
g_assert (second_task_data_destroyed == TRUE);
g_assert_true (notification_emitted);
g_assert (task == NULL);
g_free (result.expected_message);
}
/* test_return_from_same_iteration: calling g_task_return_* from the