From edb8c6114a20b806c56dd0225dd39e333ffb3ac3 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 22 Dec 2021 00:27:43 -0600 Subject: re-implement basic lily_assert_msg() macro --- CMakeLists.txt | 1 + lily-test.h | 25 +++++++++++++++++++++++-- tests/assertions.c | 31 +++++++++++++++++++++++++++++++ tests/helpers.c | 2 +- tests/main.c | 2 ++ tests/run_test.c | 2 ++ tests/tests.h | 7 ++++++- 7 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 tests/assertions.c create mode 100644 tests/run_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e4dcb5..baa2701 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories(${CMAKE_SOURCE_DIR}) set (TEST_SRC ${CMAKE_SOURCE_DIR}/tests) add_executable(lily-metatest ${TEST_SRC}/main.c + ${TEST_SRC}/assertions.c ${TEST_SRC}/helpers.c) set_target_properties(lily-metatest PROPERTIES C_STANDARD 99 diff --git a/lily-test.h b/lily-test.h index a94f292..1894127 100644 --- a/lily-test.h +++ b/lily-test.h @@ -1,12 +1,33 @@ #ifndef LILY_TEST_H #define LILY_TEST_H +#include + +/* gives a string like 'some/filename:xxx' */ +#define STR_IMP(x) #x +#define STR(x) STR_IMP(x) /* define SOURCE_PATH_SIZE to strip away the leading parts of the full compilation path */ #ifndef SOURCE_PATH_SIZE -#define LILY_FILENAME __FILE__ +#define LILY_LOCATION "" __FILE__ ":" STR(__LINE__) #else -#define LILY_FILENAME (__FILE__ + SOURCE_PATH_SIZE) +#define LILY_LOCATION (("" __FILE__ ":" STR(__LINE__)) + SOURCE_PATH_SIZE) #endif +struct lily_test_result_t { + int error; + const char *message; + const char *location; +}; + +typedef struct lily_test_result_t lily_test; +#define LILY_SUCCESS (struct lily_test_result_t){ 0, NULL, NULL } + +// assertions +#define lily_assert_msg(statement, message) do { \ + if(!(statement)) { \ + return (struct lily_test_result_t){1, (message), LILY_LOCATION}; \ + } \ + } while(0) + #endif diff --git a/tests/assertions.c b/tests/assertions.c new file mode 100644 index 0000000..0c443b5 --- /dev/null +++ b/tests/assertions.c @@ -0,0 +1,31 @@ +#include +#include + +#include "lily-test.h" +#include "tests.h" + +// basic assertion +lily_test wrap_assert_msg(int statement, const char *message) +{ + // if you move the location of this line, BE SURE TO UPDATE THE + // TEST BELOW so that it expects the correct line! + lily_assert_msg(statement, message); + return LILY_SUCCESS; +} + +const char* basic_assertion() +{ + lily_test result = wrap_assert_msg(1, "should succeed"); + if (result.error != 0) + return "assert_msg() incorrectly returned non-zero error on true statement!"; + + result = wrap_assert_msg(0, "should fail"); + if (result.error == 0) + return "assert_msg() incorrectly returned zero error on false statement!"; + if (strcmp(result.message, "should fail") != 0) + return "assert_msg() returned with incorrect message!"; + if (strcmp(result.location, "tests/assertions.c:12") != 0) + return "assert_msg() returned with incorrect location!"; + + return 0; +} diff --git a/tests/helpers.c b/tests/helpers.c index 42a0724..cdf72b4 100644 --- a/tests/helpers.c +++ b/tests/helpers.c @@ -8,7 +8,7 @@ void run_test(const char *name, const char* (*test)()) printf("%s... ", name); const char *result = test(); if (result != 0) { - printf("FAILED (%s)\n", result); + printf("FAILED: %s\n", result); } else printf("OK\n"); diff --git a/tests/main.c b/tests/main.c index a3de945..ccc7310 100644 --- a/tests/main.c +++ b/tests/main.c @@ -8,6 +8,8 @@ int main() { + run_test("check basic assert_msg()", basic_assertion); + printf("all tests finished.\n"); return 0; } diff --git a/tests/run_test.c b/tests/run_test.c new file mode 100644 index 0000000..3dd0c4b --- /dev/null +++ b/tests/run_test.c @@ -0,0 +1,2 @@ +#include "lily-test.h" + diff --git a/tests/tests.h b/tests/tests.h index cbe0f02..7b3dc48 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -1,7 +1,12 @@ #ifndef LILY_META_TESTS_H #define LILY_META_TESTS_H -void run_test(const char *name, const char* (*fp)()); +void run_test(const char *name, const char* (*test)()); int validate_message(const char* received, const char* expected, const char* FILE, unsigned int LINE); + +// test cases +const char* basic_assertion(); + + #endif -- cgit v1.2.1