Skip to content

Commit

Permalink
Add setup & teardown functions to test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Mar 26, 2019
1 parent 5b3b761 commit f18153d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/test/include/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ typedef struct bake_test_case {

typedef struct bake_test_suite {
const char *id;
void (*setup)(void);
void (*teardown)(void);
uint32_t testcase_count;
uint32_t assert_count;
bake_test_case *testcases;
Expand Down
31 changes: 31 additions & 0 deletions drivers/test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ int generate_testcase_fwd_decls(

ut_code_write(src, "// Testsuite '%s'\n", id);

int has_setup = json_object_get_boolean(suite, "setup");
if (has_setup && has_setup != -1) {
ut_code_write(src, "void %s_setup(void);\n", id);
}

int has_teardown = json_object_get_boolean(suite, "teardown");
if (has_teardown && has_teardown != -1) {
ut_code_write(src, "void %s_teardown(void);\n", id);
}

JSON_Array *testcases = json_object_get_array(suite, "testcases");
uint32_t t, count = json_array_get_count(testcases);

Expand Down Expand Up @@ -89,6 +99,17 @@ int generate_suite_data(
ut_code_indent(src);
ut_code_write(src, ".id = \"%s\",\n", id);
ut_code_write(src, ".testcase_count = %d,\n", count);

int has_setup = json_object_get_boolean(suite, "setup");
if (has_setup && has_setup != -1) {
ut_code_write(src, ".setup = %s_setup,\n", id);
}

int has_teardown = json_object_get_boolean(suite, "teardown");
if (has_teardown && has_teardown != -1) {
ut_code_write(src, ".teardown = %s_teardown,\n", id);
}

ut_code_write(src, ".testcases = (bake_test_case[]){");
ut_code_indent(src);

Expand Down Expand Up @@ -242,6 +263,16 @@ int generate_suite(

cdiff_file_write(suite_file, "#include <include/%s.h>\n", project->id_base);

int has_setup = json_object_get_boolean(suite, "setup");
if (has_setup && has_setup != -1) {
generate_testcase(driver, config, project, id, "setup", suite_file);
}

int has_teardown = json_object_get_boolean(suite, "teardown");
if (has_teardown && has_teardown != -1) {
generate_testcase(driver, config, project, id, "teardown", suite_file);
}

if (cases) {
uint32_t i, count = json_array_get_count(cases);
for (i = 0; i < count; i ++) {
Expand Down
8 changes: 8 additions & 0 deletions drivers/test/src/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ int bake_test_run_single_test(
bake_test_case *test = &suite->testcases[t];

if (!strcmp(test->id, case_id)) {
if (suite->setup) {
suite->setup();
}

current_testsuite = suite;
current_testcase = test;
suite->assert_count = 0;
Expand All @@ -65,6 +69,10 @@ int bake_test_run_single_test(
test_empty();
}
found = true;

if (suite->teardown) {
suite->teardown();
}
}
}
}
Expand Down

0 comments on commit f18153d

Please sign in to comment.