From 1e3ca08048542a29d04ecc2c1dbc0e5ba8421c27 Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 21 Dec 2021 20:58:51 -0600 Subject: strip things down in preparation for refactor --- .gitignore | 1 + CMakeLists.txt | 5 ++- lily-test.h | 28 +------------- tests/helpers.c | 15 ++++++++ tests/main.c | 13 +++++++ tests/test.c | 116 -------------------------------------------------------- tests/tests.h | 7 ++++ 7 files changed, 41 insertions(+), 144 deletions(-) create mode 100644 tests/helpers.c create mode 100644 tests/main.c delete mode 100644 tests/test.c create mode 100644 tests/tests.h diff --git a/.gitignore b/.gitignore index bdc5af0..7077787 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ build +img diff --git a/CMakeLists.txt b/CMakeLists.txt index c856cb4..f7acc8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,10 @@ project( DESCRIPTION "A super-simple single-header C test framework") include_directories(${CMAKE_SOURCE_DIR}) -add_executable(lily-metatest ${CMAKE_SOURCE_DIR}/tests/test.c) +set (TEST_SRC ${CMAKE_SOURCE_DIR}/tests) +add_executable(lily-metatest + ${TEST_SRC}/main.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.h b/lily-test.h index 9f166b1..a1e63e5 100644 --- a/lily-test.h +++ b/lily-test.h @@ -1,32 +1,6 @@ #ifndef LILY_TEST_H #define LILY_TEST_H -#define lily_test const char * - -struct lily_test_data_t { - int tests_run; - int tests_failed; -}; - -#define LILY_INIT() \ - struct lily_test_data_t lily_test_data = { 0, 0 }; - - -// helper macros to turn numerical constants into strings -#define STR_IMPL(x) #x -#define STR(X) STR_IMPL(x) - - -// assertion macros -#define lily_indent " " -#define lily_assert_msg(statement, message) \ - do { \ - if (!(statement)) \ - return "" message \ - "\n" lily_indent " [" __FILE__ ":" STR(__LINE__) "]"; \ - } while(0) - -#define lily_assert_equal(a, b) lily_assert_msg((a) == (b), "'" #a "' is not equal to '" #b "'") - +#define LILY_FILENAME __FILE__ #endif diff --git a/tests/helpers.c b/tests/helpers.c new file mode 100644 index 0000000..42a0724 --- /dev/null +++ b/tests/helpers.c @@ -0,0 +1,15 @@ +#include + +#include "lily-test.h" +#include "tests.h" + +void run_test(const char *name, const char* (*test)()) +{ + printf("%s... ", name); + const char *result = test(); + if (result != 0) { + printf("FAILED (%s)\n", result); + } + else + printf("OK\n"); +} diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..a3de945 --- /dev/null +++ b/tests/main.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include + +#include "lily-test.h" +#include "tests.h" + +int main() +{ + printf("all tests finished.\n"); + return 0; +} diff --git a/tests/test.c b/tests/test.c deleted file mode 100644 index 85c1ad7..0000000 --- a/tests/test.c +++ /dev/null @@ -1,116 +0,0 @@ -#include -#include -#include -#include - -#include "lily-test.h" - -LILY_INIT(); - -void run_test(const char *name, lily_test (*fp)()) -{ - printf("%s... ", name); - const char *result = fp(); - if (result != 0) { - printf("FAILED (%s)\n", result); - } - else - printf("OK\n"); -} - - -lily_test check_init() -{ - // should fail to compile if lily_test_data is undefined - if (lily_test_data.tests_run != 0) - return "tests_run is not equal to zero!"; - if (lily_test_data.tests_failed != 0) - return "tests_failed is not equal to zero!"; - return 0; -} - - -int get_message(char **destination, const char *source) -{ - const char *s = source; - size_t size = 0; - while (*s != '\n') { - if (*s == 0) - return false; - s++; - size++; - } - - *destination = malloc((size+1) * sizeof(char)); - strncpy(*destination, source, size); - (*destination)[size] = 0; - return true; -} - -#define assert_msg "message" - -lily_test wrap_assert(bool statement) -{ - lily_assert_msg(statement, assert_msg); - return 0; -} - -lily_test check_assert() -{ - if (wrap_assert(true) != 0) - return "true assertion did not return 0!"; - - const char *result = wrap_assert(false); - - if (result == 0) - return "false assertion returned zero!"; - - char *message; - if (!get_message(&message, result)) - return "false assertion contained malformed message!"; - - if (strcmp(message, assert_msg) != 0) - return "false assertion message was not '" assert_msg "'!"; - free(message); - - return 0; -} - - -const char *assert_eq_true() -{ - lily_assert_equal(15, 15); - return 0; -} -const char *assert_eq_false() -{ - lily_assert_equal(14, 15); - return 0; -} - -lily_test check_other_asserts() -{ - if (assert_eq_true() != 0) - return "equality assertion returned non-zero when true!"; - const char *result = assert_eq_false(); - if (result == 0) - return "equality assertion returned zero when false!"; - char *message; - if (!get_message(&message, result)) - return "false equality assertion returned malformed message!"; - if (strcmp(message, "'14' is not equal to '15'") != 0) - return "false equality assertion returned incorrect message!"; - - return 0; -} - - -int main() -{ - run_test("check LILY_INIT()", check_init); - run_test("check basic assertion", check_assert); - run_test("check other asserts", check_other_asserts); - - printf("all tests finished.\n"); - return 0; -} diff --git a/tests/tests.h b/tests/tests.h new file mode 100644 index 0000000..cbe0f02 --- /dev/null +++ b/tests/tests.h @@ -0,0 +1,7 @@ +#ifndef LILY_META_TESTS_H +#define LILY_META_TESTS_H + +void run_test(const char *name, const char* (*fp)()); +int validate_message(const char* received, const char* expected, + const char* FILE, unsigned int LINE); +#endif -- cgit v1.2.1