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 --- README.md | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 0fffdc3..9a6e8c9 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,15 @@ you can compile and play with yourself. /* 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); } @@ -57,9 +58,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") { @@ -118,9 +116,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); @@ -130,7 +128,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 @@ -185,9 +183,8 @@ simple-test's interface consists of a series of preprocessor macros: | **BEGIN_TEST** | must be included once, after includes and global declarations and before the first `TEST` statement | | **END_TEST** | must be included once, after the final `TEST` statement | | **TEST(description)**{} | declare a test, described by `description`, which consists of statements between the {} | -| **REGISTER_TEARDOWN(func)** | optionally included after `BEGIN_TEST` and before the first `TEST` statement. registers function `func`, which must be of type `void func(void)`, as a teardown function to be called after subscribing tests exit | -| **USE_TEARDOWN;** | if included within a `TEST` body (before any `ASSERT` statements), the designated `TEST` will call the function registered with `REGISTER_TEARDOWN` on terminating | -| **ECHO(...)** | print a formatted description of the state within the current test | +| **TEARDOWN(fun);** | if included within a `TEST` body, the designated `TEST` will call the function argument after any following failed assertions or when terminating. may be redefined | +| **ECHO(...);** | print a formatted description of the state within the current test | assertions are used to check that the behaviour of code executed inside the body of a `TEST` statement is correct. values are passed and the assertion @@ -195,14 +192,14 @@ fails if those values do not reflect expectations. valid assertions are: | **NAME** | **DESCRIPTION** | |---------:|:----------------| -| **ASSERT(a)** | assert that the value of `a` is not a "not" value, i.e. NULL, 0, or '\0' | -| **ASSERT_NOT(a)** | assert that the value of `a` is a "not" value | -| **ASSERT_EQ(a, b)** | fail if the values of `a` and `b` are not equal | -| **ASSERT_NEQ(a, b)** | fail if the values of `a` and `b` are equal | -| **ASSERT_GT(a, b)** | fail if the value of `a` is not greater the value of `b` | -| **ASSERT_GEQ(a, b)** | fail if the value of `a` is not greater than or equal to the value of `b` | -| **ASSERT_LT(a, b)** | fail if the value of `a` is not less than the value of `b` | -| **ASSERT_LEQ(a, b)** | fail if the value of `a` is not less than or equal to the value of `b` | +| **ASSERT(a);** | assert that the value of `a` is not a "not" value, i.e. NULL, 0, or '\0' | +| **ASSERT_NOT(a);** | assert that the value of `a` is a "not" value | +| **ASSERT_EQ(a, b);** | fail if the values of `a` and `b` are not equal | +| **ASSERT_NEQ(a, b);** | fail if the values of `a` and `b` are equal | +| **ASSERT_GT(a, b);** | fail if the value of `a` is not greater the value of `b` | +| **ASSERT_GEQ(a, b);** | fail if the value of `a` is not greater than or equal to the value of `b` | +| **ASSERT_LT(a, b);** | fail if the value of `a` is not less than the value of `b` | +| **ASSERT_LEQ(a, b);** | fail if the value of `a` is not less than or equal to the value of `b` | note -- cgit v1.2.3