summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip.withnall@collabora.co.uk>2015-06-17 17:18:21 +0100
committerPhilip Withnall <philip.withnall@collabora.co.uk>2015-06-17 17:19:25 +0100
commit7612d6720bab91b7a397036e6a22203b2f7093e6 (patch)
tree6fb30027ab91ac8af98ae2021c424cf3d3f54398
parent70327ca30815038f5736e10f8d731fdc0fa171d7 (diff)
downloadfolks-7612d6720bab91b7a397036e6a22203b2f7093e6.tar.gz
tests: Allow data to be stored in unit test closures
Previously, unit test closures had to be entirely pure — now they can store data. This will be useful for adding unit tests dynamically.
-rw-r--r--tests/lib/test-case-helper.c25
-rw-r--r--tests/lib/test-case.vala7
2 files changed, 24 insertions, 8 deletions
diff --git a/tests/lib/test-case-helper.c b/tests/lib/test-case-helper.c
index bd5c0ee0..a4bdb7cf 100644
--- a/tests/lib/test-case-helper.c
+++ b/tests/lib/test-case-helper.c
@@ -1,6 +1,7 @@
/* test-case-helper.c
*
* Copyright © 2013 Intel Corporation
+ * Copyright © 2015 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -29,6 +30,8 @@
typedef struct {
gpointer self;
FolksTestCaseTestMethod test;
+ gpointer test_data;
+ GDestroyNotify test_data_free;
} FolksTestCaseWeakMethod;
static void
@@ -52,7 +55,7 @@ folks_test_case_weak_method_test (gpointer fixture G_GNUC_UNUSED,
g_return_if_fail (wm->self != NULL);
g_return_if_fail (FOLKS_IS_TEST_CASE (wm->self));
- wm->test (wm->self);
+ wm->test (wm->test_data);
}
static void
@@ -67,21 +70,33 @@ folks_test_case_weak_method_teardown (gpointer fixture G_GNUC_UNUSED,
folks_test_case_tear_down (wm->self);
}
+static void
+test_case_destroyed_cb (gpointer data,
+ GObject *test_case_location)
+{
+ FolksTestCaseWeakMethod *wm = data;
+
+ if (wm->test_data != NULL && wm->test_data_free != NULL)
+ wm->test_data_free (wm->test_data);
+}
+
GTestCase *
folks_test_case_add_test_helper (FolksTestCase *self,
const gchar *name,
FolksTestCaseTestMethod test,
- void *test_target)
+ void *test_target,
+ GDestroyNotify test_target_destroy_notify)
{
FolksTestCaseWeakMethod *wm;
- g_return_val_if_fail (self == (FolksTestCase *) test_target, NULL);
-
/* This will never be freed, so make sure not to hold references. */
wm = g_new0 (FolksTestCaseWeakMethod, 1);
wm->self = self;
wm->test = test;
- g_object_add_weak_pointer (G_OBJECT (self), &wm->self);
+ wm->test_data = test_target;
+ wm->test_data_free = test_target_destroy_notify;
+
+ g_object_weak_ref (G_OBJECT (self), test_case_destroyed_cb, wm);
return g_test_create_case (name,
0,
diff --git a/tests/lib/test-case.vala b/tests/lib/test-case.vala
index 604b3a56..bdf79303 100644
--- a/tests/lib/test-case.vala
+++ b/tests/lib/test-case.vala
@@ -376,13 +376,14 @@ public abstract class Folks.TestCase : Object
TestSuite.get_root ().add_suite (this._suite);
}
- public void add_test (string name, TestMethod test)
+ public void add_test (string name, owned TestMethod test)
{
- this._suite.add (add_test_helper (name, test));
+ this._suite.add (add_test_helper (name, (owned) test));
}
/* implemented in test-case-helper.c */
- internal extern GLib.TestCase add_test_helper (string name, TestMethod test);
+ internal extern GLib.TestCase add_test_helper (string name,
+ owned TestMethod test);
/**
* Set up for one test. If you have more than one test, this will