summaryrefslogtreecommitdiff
path: root/include/cmocka.h
diff options
context:
space:
mode:
authorPawel Szewczyk <p.szewczyk@samsung.com>2015-09-14 12:52:12 +0200
committerAndreas Schneider <asn@cryptomilk.org>2015-09-23 16:42:11 +0200
commitda22fa6c2435c8421817292e5ca1269c7f4e4959 (patch)
tree68620f0e0c6b517212c8942dad4b8abca994d4b8 /include/cmocka.h
parentcc3386b4ce502e532686a6ec0c56af362cb33e86 (diff)
downloadcmocka-da22fa6c2435c8421817292e5ca1269c7f4e4959.tar.gz
cmocka-da22fa6c2435c8421817292e5ca1269c7f4e4959.tar.bz2
cmocka-da22fa6c2435c8421817292e5ca1269c7f4e4959.zip
cmocka: Allow to pass initial data to test cases
Sometimes multiple test cases share the same test function, running it on different data. To pass this data to test functions we must define setup function for each data set. It's not very convienient when there are many states to test. This commit introduce more elegant way to pass data to tests. The initial_state field of CMUnitTest structure can be defined by user. It will be either passed to setup function, which makes any preparation needed, or it will be passed directly to test function when setup func is NULL. Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Diffstat (limited to 'include/cmocka.h')
-rw-r--r--include/cmocka.h25
1 files changed, 21 insertions, 4 deletions
diff --git a/include/cmocka.h b/include/cmocka.h
index 797fbe2..e4aa5b2 100644
--- a/include/cmocka.h
+++ b/include/cmocka.h
@@ -1489,19 +1489,35 @@ static inline void _unit_test_dummy(void **state) {
/** Initializes a CMUnitTest structure. */
-#define cmocka_unit_test(f) { #f, f, NULL, NULL }
+#define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
/** Initializes a CMUnitTest structure with a setup function. */
-#define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL }
+#define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
/** Initializes a CMUnitTest structure with a teardown function. */
-#define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown }
+#define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
/**
* Initialize an array of CMUnitTest structures with a setup function for a test
* and a teardown function. Either setup or teardown can be NULL.
*/
-#define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown }
+#define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
+
+/**
+ * Initialize a CMUnitTest structure with given initial state. It will be passed to test
+ * function as an argument later. It can be used when test state does not need special initialization
+ * or was initialized already.
+ * If group state was initialized for this unit test, it won't be overrided by initial state defined here.
+ */
+#define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state }
+
+/**
+ * Initialize a CMUnitTest structure with given initial state, setup and teardown function. Any of
+ * these values can be NULL. Initial state is passed later to setup function, or directly to test if
+ * none was given.
+ * If group state was initialized for this unit test, it won't be overrided by initial state defined here.
+ */
+#define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state }
#define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
#define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
@@ -1894,6 +1910,7 @@ struct CMUnitTest {
CMUnitTestFunction test_func;
CMFixtureFunction setup_func;
CMFixtureFunction teardown_func;
+ void *initial_state;
};
/* Location within some source code. */