From c838475e26fb555bde08d4c1ee8ddaae1e9d2694 Mon Sep 17 00:00:00 2001 From: Matheus Afonso Martins Moreira Date: Sun, 14 Jul 2024 02:19:08 -0300 Subject: [PATCH] lone/test: define default test suite event handler Define some sensible default event handlers for the test suite. They print the test name when it begins and the result when it ends. This allows adding more output in-between later, such as an assertions trace as well as expected/got comparisons when they fail. --- include/lone/test.h | 7 +++++-- source/lone/test.c | 36 ++++++++++++++++++++++++++++++++++++ source/tests/lone/types.c | 27 --------------------------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/include/lone/test.h b/include/lone/test.h index a6c80c51..6a0c8887 100644 --- a/include/lone/test.h +++ b/include/lone/test.h @@ -33,6 +33,9 @@ typedef void (*lone_test_function)(struct lone_test_suite *suite, typedef void (*lone_test_event)(struct lone_test_suite *suite, struct lone_test_case *test); +void lone_test_suite_default_test_started_handler(struct lone_test_suite *suite, struct lone_test_case *test); +void lone_test_suite_default_test_finished_handler(struct lone_test_suite *suite, struct lone_test_case *test); + struct lone_test_suite_events { void *context; @@ -95,8 +98,8 @@ void __name(struct lone_test_suite *suite, struct lone_test_case *test) { \ .tests = (__cases), \ .events.context = 0, \ - .events.on.test.started = 0, \ - .events.on.test.finished = 0, \ + .events.on.test.started = lone_test_suite_default_test_started_handler, \ + .events.on.test.finished = lone_test_suite_default_test_finished_handler, \ } enum lone_test_result lone_test_suite_run(struct lone_test_suite *suite); diff --git a/source/lone/test.c b/source/lone/test.c index 9ab278e4..90a73edb 100644 --- a/source/lone/test.c +++ b/source/lone/test.c @@ -251,3 +251,39 @@ LONE_TEST_ASSERTION_FUNCTION(u64, lone_u64, U64, u64) #undef LONE_TEST_ASSERTION_FUNCTION #undef LONE_TEST_ASSERTION_FUNCTION_5 + +#define LINUX_WRITE_LITERAL(fd, c_string_literal) \ + linux_write(fd, c_string_literal, sizeof(c_string_literal) - 1) + +void lone_test_suite_default_test_started_handler(struct lone_test_suite *suite, struct lone_test_case *test) +{ + LINUX_WRITE_LITERAL(1, "TEST "); + linux_write(1, test->name.pointer, test->name.count); + LINUX_WRITE_LITERAL(1, "\n"); +} + +void lone_test_suite_default_test_finished_handler(struct lone_test_suite *suite, struct lone_test_case *test) +{ + struct lone_bytes result; + + switch (test->result) { + case LONE_TEST_RESULT_PASS: + result = (struct lone_bytes) LONE_BYTES_FROM_LITERAL("PASS"); + break; + case LONE_TEST_RESULT_FAIL: + result = (struct lone_bytes) LONE_BYTES_FROM_LITERAL("FAIL"); + break; + case LONE_TEST_RESULT_SKIP: + result = (struct lone_bytes) LONE_BYTES_FROM_LITERAL("SKIP"); + break; + case LONE_TEST_RESULT_ERROR: + case LONE_TEST_RESULT_PENDING: + default: + result = (struct lone_bytes) LONE_BYTES_FROM_LITERAL("ERROR"); + break; + } + + LINUX_WRITE_LITERAL(1, "\tRESULT "); + linux_write(1, result.pointer, result.count); + LINUX_WRITE_LITERAL(1, "\n"); +} diff --git a/source/tests/lone/types.c b/source/tests/lone/types.c index 02c41f76..523d9143 100644 --- a/source/tests/lone/types.c +++ b/source/tests/lone/types.c @@ -415,31 +415,6 @@ LONE_TYPES_ENDIAN_TEST_WRITE(s, 64, be, unaligned, 0x0102030405060708, 1, 0x01, #undef LONE_TYPES_ENDIAN_TEST_WRITE #undef LONE_TYPES_ENDIAN_TEST_READ -static void test_finished(struct lone_test_suite *suite, struct lone_test_case *test) -{ - struct lone_bytes result; - - switch (test->result) { - case LONE_TEST_RESULT_PASS: - result = (struct lone_bytes) LONE_BYTES_FROM_LITERAL("PASS"); - break; - case LONE_TEST_RESULT_FAIL: - result = (struct lone_bytes) LONE_BYTES_FROM_LITERAL("FAIL"); - break; - case LONE_TEST_RESULT_SKIP: - result = (struct lone_bytes) LONE_BYTES_FROM_LITERAL("SKIP"); - break; - case LONE_TEST_RESULT_PENDING: - default: - linux_exit(-1); - } - - linux_write(1, result.pointer, result.count); - linux_write(1, " ", 1); - linux_write(1, test->name.pointer, test->name.count); - linux_write(1, "\n", 1); -} - long lone(int argc, char **argv, char **envp, struct lone_auxiliary_vector *auxv) { @@ -550,8 +525,6 @@ long lone(int argc, char **argv, char **envp, struct lone_auxiliary_vector *auxv struct lone_test_suite suite = LONE_TEST_SUITE(cases); enum lone_test_result result; - suite.events.on.test.finished = test_finished; - result = lone_test_suite_run(&suite); switch (result) {