aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkatherine <ageha@airen-no-jikken.icu>2019-06-15 18:51:10 -0700
committerkatherine <ageha@airen-no-jikken.icu>2019-06-15 18:51:10 -0700
commit8b1b6b7f1b8ab79e5fd3d4d16e7de7ec681130b7 (patch)
treef70f7a1b6e49a23b3699ad617dee2436d9134e92
parentcae280b3246a51cd485cb01c8ec73a205ceac7e8 (diff)
downloadsimple-test-8b1b6b7f1b8ab79e5fd3d4d16e7de7ec681130b7.tar.gz
replace USE/REGISTER_TEARDOWN with TEARDOWNHEADmaster
simpler and more flexible
-rw-r--r--README.md45
-rw-r--r--doc/example.c24
-rw-r--r--simple-test.h21
3 files changed, 38 insertions, 52 deletions
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
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
diff --git a/simple-test.h b/simple-test.h
index c6abebf..34cb4d7 100644
--- a/simple-test.h
+++ b/simple-test.h
@@ -9,6 +9,7 @@
#include <stdarg.h>
#include <string.h>
+
/**************************
* CONFIGURATION MACROS *
**************************/
@@ -394,10 +395,8 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1,
* BASIC MACROS *
******************/
-#define REGISTER_TEARDOWN(f) \
+#define TEARDOWN(f) \
do { \
- if (simple_test_teardown != NULL) \
- SIMPLE_TEST_ERR("teardown function already defined"); \
simple_test_teardown = _Generic((f), \
void(*)(void): (f), default: NULL); \
if (simple_test_teardown == NULL) { \
@@ -406,13 +405,6 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1,
} \
} while (0)
-#define USE_TEARDOWN \
- do { \
- if (simple_test_teardown == NULL) \
- SIMPLE_TEST_ERR("teardown function undefined"); \
- simple_test_do_teardown = true; \
- } while (0)
-
/* must appear before all tests */
#define BEGIN_TEST \
int main(int argc, char **argv) \
@@ -425,7 +417,6 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1,
int simple_test_test_current = 1; \
int simple_test_test_current_at; \
int simple_test_pass_number = 0; \
- bool simple_test_do_teardown = false; \
void (*simple_test_teardown)(void) = NULL; \
do { \
simple_test_test_current_at = 0; \
@@ -447,9 +438,9 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1,
#define TEST(description) \
} \
- if (simple_test_do_teardown) { \
- simple_test_do_teardown = false; \
+ if (simple_test_teardown != NULL) { \
simple_test_teardown(); \
+ simple_test_teardown = NULL; \
} \
simple_test_test_current_at++; \
if (simple_test_pass_number == 0) { \
@@ -471,9 +462,9 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1,
/* must appear after all tests */
#define END_TEST \
} \
- if (simple_test_do_teardown) { \
- simple_test_do_teardown = false; \
+ if (simple_test_teardown != NULL) { \
simple_test_teardown(); \
+ simple_test_teardown = NULL; \
} \
if (simple_test_test_count == 0) { \
SIMPLE_TEST_ERR("no tests defined"); \