Skip to content

Commit

Permalink
lone/test: define default test suite event handler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matheusmoreira committed Jul 14, 2024
1 parent 268a7e1 commit c838475
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
7 changes: 5 additions & 2 deletions include/lone/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions source/lone/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
27 changes: 0 additions & 27 deletions source/tests/lone/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c838475

Please sign in to comment.