Skip to content

Commit

Permalink
Add feature that allows for parameterizing test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Feb 25, 2024
1 parent 72e9851 commit 234c81e
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 53 deletions.
12 changes: 12 additions & 0 deletions drivers/test/include/bake_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ typedef struct bake_test_case {
void (*function)(void);
} bake_test_case;

typedef struct bake_test_param {
const char *name;
char **values;
int32_t value_count;
int32_t value_cur;
} bake_test_param;

typedef struct bake_test_suite {
const char *id;
void (*setup)(void);
void (*teardown)(void);
uint32_t testcase_count;
bake_test_case *testcases;
uint32_t param_count;
bake_test_param *params;
uint32_t assert_count;
} bake_test_suite;

Expand Down Expand Up @@ -122,6 +131,9 @@ void test_expect_abort(void);
BAKE_TEST_API
void test_abort(void);

BAKE_TEST_API
const char* test_param(const char *name);

#define test_assert(cond) _test_assert(cond, #cond, __FILE__, __LINE__)
#define test_bool(v1, v2) _test_bool(v1, v2, #v1, #v2, __FILE__, __LINE__)
#define test_true(v) _test_bool(v, true, #v, "true", __FILE__, __LINE__)
Expand Down
69 changes: 67 additions & 2 deletions drivers/test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,59 @@ int generate_suite_testcases(
return 0;
}

static
int generate_suite_params(
ut_code *src,
JSON_Array *suites)
{
size_t i, count = json_array_get_count(suites);

/* The JSON structure has already been validated, so no need to do error
* checking again. */

for (i = 0; i < count; i ++) {
JSON_Object *suite = json_array_get_object(suites, i);
const char *id = json_object_get_string(suite, "id");

JSON_Object *params = json_object_get_object(suite, "params");
if (!params) {
continue;
}

ut_code_write(src, "bake_test_param %s_params[] = {\n", id);
ut_code_indent(src);

for (size_t p = 0; p < json_object_get_count(params); p ++) {
if (p) {
ut_code_write(src, ",\n");
}

const char *name = json_object_get_name(params, p);
ut_code_write(src, "{\"%s\", (char*[]){", name);

JSON_Array *param_values = json_object_get_array(params, name);
for (size_t v = 0; v < json_array_get_count(param_values); v ++) {
const char *value = json_array_get_string(param_values, v);
if (v) {
ut_code_write(src, ", ");
}
ut_code_write(src, "\"%s\"", value);
}

ut_code_write(src, "}, %d}", json_array_get_count(param_values));
}

ut_code_write(src, "\n");
ut_code_dedent(src);
ut_code_write(src, "};\n");
}

ut_code_write(src, "\n");

return 0;
}


static
int generate_suite_data(
ut_code *src,
Expand Down Expand Up @@ -159,7 +212,17 @@ int generate_suite_data(
size_t t_count = json_array_get_count(testcases);

ut_code_write(src, "%d,\n", t_count);
ut_code_write(src, "%s_testcases\n", id);
ut_code_write(src, "%s_testcases", id);

JSON_Object *params = json_object_get_object(suite, "params");
if (params) {
size_t p_count = json_object_get_count(params);
ut_code_write(src, ",\n");
ut_code_write(src, "%d,\n", p_count);
ut_code_write(src, "%s_params\n", id);
} else {
ut_code_write(src, "\n");
}

ut_code_dedent(src);
ut_code_write(src, "}");
Expand All @@ -175,6 +238,7 @@ int generate_testmain(
bake_driver_api *driver,
bake_config *config,
bake_project *project,
JSON_Object *jo,
JSON_Array *suites)
{
(void)driver;
Expand Down Expand Up @@ -207,6 +271,7 @@ int generate_testmain(

generate_testcase_fwd_decls(src, suites);
generate_suite_testcases(src, suites);
generate_suite_params(src, suites);

ut_code_write(src, "static bake_test_suite suites[] = {\n");
ut_code_indent(src);
Expand Down Expand Up @@ -349,7 +414,7 @@ void generate(
}
}

generate_testmain(driver, config, project, suites);
generate_testmain(driver, config, project, jo, suites);
} else {
fprintf(stderr, "no 'testsuites' array in test configuration\n");
project->error = true;
Expand Down
Loading

0 comments on commit 234c81e

Please sign in to comment.