]> git.mdlowis.com Git - projs/atf.git/commitdiff
added mocked out exit function for testing of exit codes without terminating the...
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 21 Feb 2017 22:20:20 +0000 (17:20 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 21 Feb 2017 22:20:20 +0000 (17:20 -0500)
Makefile
atf.h
tests.c

index 619de45dd1caf10907415bcb5349729fa7722486..febe4e28c3a1209621dab68eeb817fef3090204b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,6 +5,9 @@ DISTGZ    = ${DISTTAR}.gz
 DISTFILES = atf.h LICENSE.md Makefile README.md tests.c
 
 tests: tests.c
+       @echo "============================================"
+       @echo "NOTE: It is expected that 3 tests will fail."
+       @echo "============================================"
        $(CC) -I. -o $@ $^
        -./$@
 
diff --git a/atf.h b/atf.h
index b6b5fbbc4a976448cc9b99e460dbf44e1f41d243..a486e792e0bed0ca0c1e3e98dd2cdec10d7f9365 100755 (executable)
--- a/atf.h
+++ b/atf.h
@@ -22,6 +22,8 @@
 
 #include <stddef.h>
 #include <stdbool.h>
+#include <setjmp.h>
+#include <assert.h>
 
 extern char* Curr_Test;
 void atf_init(int argc, char** argv);
@@ -35,6 +37,9 @@ int atf_print_results(void);
 
 #define CHECK(expr) \
     if(atf_test_assert((expr), #expr, __FILE__, __LINE__)) break
+    
+#define CHECK_EXITCODE(code) \
+    CHECK(ExitCode == code)
 
 #define TEST_SUITE(name) \
     void name(void)
@@ -50,6 +55,9 @@ int atf_print_results(void);
 
 #define PRINT_TEST_RESULTS \
     atf_print_results
+    
+#define EXPECT_EXIT \
+    if ((ExitExpected = true, 0 == setjmp(ExitPad)))
 
 /* Function Definitions
  *****************************************************************************/
@@ -65,6 +73,9 @@ char* Curr_File = NULL;
 unsigned int Curr_Line = 0;
 static unsigned int Total = 0;
 static unsigned int Failed = 0;
+bool ExitExpected;
+int ExitCode;
+jmp_buf ExitPad;
 
 #ifndef NO_SIGNALS
 static void handle_signal(int sig) {
@@ -86,7 +97,7 @@ static void handle_signal(int sig) {
     fprintf(stderr,"%s:%d:0:%s:CRASH (signal: %d - %s)\n", Curr_File, Curr_Line, Curr_Test, sig, sig_name);
     Failed++;
     (void)atf_print_results();
-    exit(1);
+    _Exit(1);
 }
 #endif
 
@@ -136,6 +147,16 @@ int atf_print_results(void) {
     return Failed;
 }
 
+void exit(int code) {
+    if (ExitExpected) {
+        ExitCode = code;
+        ExitExpected = false;
+        longjmp(ExitPad, 1);
+    } else {
+        assert(!"Unexpected exit. Something went wrong");
+    }
+}
+
 #undef INCLUDE_DEFS
 #endif
 
diff --git a/tests.c b/tests.c
index ce0efab4a465654e3a6799ef740bb5e1eedeb33d..dc18c50970cbc009f059bc2760f81a74ffa6fd89 100644 (file)
--- a/tests.c
+++ b/tests.c
@@ -24,6 +24,16 @@ int main(int argc, char** argv) {
 }
 
 TEST_SUITE(External_Suite) {
+    TEST(CHECK_EXITCODE should pass if exit called with correct code) {
+        EXPECT_EXIT { exit(42); }
+        CHECK_EXITCODE(42);
+    }
+    
+    TEST(CHECK_EXITCODE should fail if exit called with incorrect code) {
+        EXPECT_EXIT { exit(42); }
+        CHECK_EXITCODE(41);
+    }
+    
     TEST(Should_handle_SIGABRT) {
         raise(SIGABRT);
     }