From 9c4bd59002109c850dda3ab79febc17f7f0367e9 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 25 Dec 2021 23:12:46 -0600 Subject: begin refactor to two-file library --- CMakeLists.txt | 5 +++-- lily-test.c | 2 ++ lily-test.h | 35 ++++++----------------------------- tests/assertions.c | 50 ++++++-------------------------------------------- tests/helpers.c | 2 +- tests/main.c | 4 +--- tests/tests.h | 6 +++--- 7 files changed, 22 insertions(+), 82 deletions(-) create mode 100644 lily-test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index baa2701..e209e55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.2) project( lily-test VERSION 0.1.0 - DESCRIPTION "A super-simple single-header C test framework") + DESCRIPTION "A super-simple C test framework") string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE) add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}") @@ -11,8 +11,9 @@ include_directories(${CMAKE_SOURCE_DIR}) set (TEST_SRC ${CMAKE_SOURCE_DIR}/tests) add_executable(lily-metatest ${TEST_SRC}/main.c + ${TEST_SRC}/helpers.c ${TEST_SRC}/assertions.c - ${TEST_SRC}/helpers.c) + ) set_target_properties(lily-metatest PROPERTIES C_STANDARD 99 CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") diff --git a/lily-test.c b/lily-test.c new file mode 100644 index 0000000..3dd0c4b --- /dev/null +++ b/lily-test.c @@ -0,0 +1,2 @@ +#include "lily-test.h" + diff --git a/lily-test.h b/lily-test.h index df50afb..c2f1b8b 100644 --- a/lily-test.h +++ b/lily-test.h @@ -1,44 +1,21 @@ #ifndef LILY_TEST_H #define LILY_TEST_H +#include #include +#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_LOCATION "" __FILE__ ":" STR(__LINE__) +#define LILY_LOCATION (__FILE__ ":" STR(__LINE__)) #else -#define LILY_LOCATION (("" __FILE__ ":" STR(__LINE__)) + 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; - void *data; -}; - -typedef struct lily_test_result_t lily_test; -#define LILY_SUCCESS (struct lily_test_result_t){ 0, NULL, NULL, NULL } - -// assertions -#define LILY_NO_ERROR 0 -#define LILY_NULL_DATA 1 -#define LILY_INT_DATA 2 -#define LILY_STRING_DATA 3 - -#define lily_assert_msg(statement, code, message, data) do { \ - if(!(statement)) { \ - return (struct lily_test_result_t){ \ - (code), (message), LILY_LOCATION, (data) \ - }; \ - } \ - } while(0) - -#define lily_assert_equal(a, b) \ - lily_assert_msg(a == b, LILY_NULL_DATA, #a " is not equal to " #b, NULL) +void lily_assert_printf(bool statement, const char *location, + const char *format_string, ...); #endif diff --git a/tests/assertions.c b/tests/assertions.c index d95f52a..5fb5e8d 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -4,51 +4,13 @@ #include "lily-test.h" #include "tests.h" -// basic assertion -lily_test wrap_assert_msg(int statement, const char *message) +const char *test_LILY_LOCATION() { - // 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, 1, message, NULL); - 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; -} - - -// assert equal -lily_test wrap_assert_equal(int x, int y) -{ - lily_assert_equal(x, y); - return LILY_SUCCESS; -} - -const char* assert_equal() -{ - lily_test result = wrap_assert_equal(15, 15); - if (result.error != LILY_NO_ERROR) - return "lily_assert_equal incorrectly returned an error on equal inputs!"; - - result = wrap_assert_equal(14, 15); - if (result.error != LILY_NULL_DATA) - return "lily_assert_equal returned an incorrect error on non-equal inputs!"; - if (strcmp(result.message, "x is not equal to y") != 0) - return "lily_assert_equal returned incorrect message for non-equal inputs!"; + // if you move this line, you MUST update the expected string! + const char *location = LILY_LOCATION; + int diff = strcmp(location, "tests/assertions.c:10"); + if (diff != 0) + return "LILY_LOCATION did not resolve correctly!"; return 0; } diff --git a/tests/helpers.c b/tests/helpers.c index cdf72b4..37afcae 100644 --- a/tests/helpers.c +++ b/tests/helpers.c @@ -3,7 +3,7 @@ #include "lily-test.h" #include "tests.h" -void run_test(const char *name, const char* (*test)()) +void _run_test(const char *name, const char* (*test)()) { printf("%s... ", name); const char *result = test(); diff --git a/tests/main.c b/tests/main.c index 69bf65e..0659713 100644 --- a/tests/main.c +++ b/tests/main.c @@ -8,9 +8,7 @@ int main() { - run_test("check lily_assert_msg()", basic_assertion); - run_test("check lily_assert_equal()", assert_equal); + run_test(test_LILY_LOCATION); - printf("all tests finished.\n"); return 0; } diff --git a/tests/tests.h b/tests/tests.h index e1d7e04..eab0115 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -1,12 +1,12 @@ #ifndef LILY_META_TESTS_H #define LILY_META_TESTS_H -void run_test(const char *name, const char* (*test)()); +#define run_test(test) _run_test(#test, test) +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(); -const char* assert_equal(); +const char* test_LILY_LOCATION(); #endif -- cgit v1.2.1