From 8b1b6b7f1b8ab79e5fd3d4d16e7de7ec681130b7 Mon Sep 17 00:00:00 2001 From: katherine Date: Sat, 15 Jun 2019 18:51:10 -0700 Subject: replace USE/REGISTER_TEARDOWN with TEARDOWN simpler and more flexible --- doc/example.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'doc') diff --git a/doc/example.c b/doc/example.c index 0229d2a..c048465 100644 --- a/doc/example.c +++ b/doc/example.c @@ -10,14 +10,15 @@ /* global variables, functions, and includes must come before BEGIN_TEST */ char *global_s; -/* a teardown function is called by TESTs which include USE_TEARDOWN. the call - * occurs either after an ASSERT fails or after the TEST is finished. +/* a teardown function must be of type `void function(void)` and can be called + * by TESTs which include them using the TEARDOWN macro. the call occurs either + * after an ASSERT fails or after the TEST is finished. + * + * the macro may be called multiple times, but only the most recently set + * teardown function will be called. * - * defining one is optional, but, if used, REGISTER_TEARDOWN must appear - * between BEGIN_TEST and the first TEST statement. - * * here i'm using it to free memory that's alloced inside tests below */ -void teardown(void) +void teardown_fun(void) { free(global_s); } @@ -25,9 +26,6 @@ void teardown(void) /* must appear before an (optional) REGISTER_TEARDOWN and all TESTs */ BEGIN_TEST -/* if used, must appear before first TEST and after BEGIN_TEST */ -REGISTER_TEARDOWN(teardown); - /* run a test. provided description must be a string literal */ TEST("basic assertion") { @@ -86,9 +84,9 @@ TEST("string comparison") char *s = "test"; global_s = strdup("test"); - /* USE_TEARDOWN; tells this test to call the previously defined teardown - * function on exiting (successfully or otherwise) */ - USE_TEARDOWN; + /* this tells this test to call the previously defined teardown function on + * exiting (successfully or otherwise) */ + TEARDOWN(teardown_fun); /* strings are compared by content, so this assertion succeeds */ ASSERT_EQ(s, global_s); @@ -98,7 +96,7 @@ TEST("pointer comparison") { char *s = "test"; global_s = strdup("test"); - USE_TEARDOWN; + TEARDOWN(teardown_fun); /* you can cast parameters in order to use a different type of comparison. * here i'm casting the 'char *' to 'void *' so assertion performs a -- cgit v1.2.3