From 82009d685f48e95d58e0eaaf16f2bfc260e172ab Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 22 Dec 2021 00:56:08 -0600 Subject: add lily_assert_equal macro --- lily-test.h | 21 ++++++++++++++++----- tests/assertions.c | 25 ++++++++++++++++++++++++- tests/main.c | 3 ++- tests/tests.h | 2 +- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lily-test.h b/lily-test.h index 1894127..df50afb 100644 --- a/lily-test.h +++ b/lily-test.h @@ -18,16 +18,27 @@ 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 } +#define LILY_SUCCESS (struct lily_test_result_t){ 0, NULL, NULL, NULL } // assertions -#define lily_assert_msg(statement, message) do { \ - if(!(statement)) { \ - return (struct lily_test_result_t){1, (message), LILY_LOCATION}; \ - } \ +#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) + #endif diff --git a/tests/assertions.c b/tests/assertions.c index 0c443b5..d95f52a 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -9,7 +9,7 @@ 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); + lily_assert_msg(statement, 1, message, NULL); return LILY_SUCCESS; } @@ -29,3 +29,26 @@ const char* basic_assertion() 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!"; + + return 0; +} diff --git a/tests/main.c b/tests/main.c index ccc7310..69bf65e 100644 --- a/tests/main.c +++ b/tests/main.c @@ -8,7 +8,8 @@ int main() { - run_test("check basic assert_msg()", basic_assertion); + run_test("check lily_assert_msg()", basic_assertion); + run_test("check lily_assert_equal()", assert_equal); printf("all tests finished.\n"); return 0; diff --git a/tests/tests.h b/tests/tests.h index 7b3dc48..e1d7e04 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -7,6 +7,6 @@ int validate_message(const char* received, const char* expected, // test cases const char* basic_assertion(); - +const char* assert_equal(); #endif -- cgit v1.2.1