-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
add_executable(cutest_test | ||
tools/__init__.c | ||
tools/help.c | ||
main.c | ||
test.c | ||
) | ||
target_include_directories(cutest_test | ||
PUBLIC | ||
$<INSTALL_INTERFACE:include> | ||
PRIVATE | ||
${CMAKE_CURRENT_SOURCE_DIR} | ||
) | ||
target_link_libraries(cutest_test | ||
PRIVATE | ||
cutest | ||
) | ||
cutest_setup_target_wall(cutest_test) | ||
|
||
add_test(NAME cutest_test COMMAND $<TARGET_FILE:cutest_test>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "test.h" | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
return cutest_run_tests(argc, argv, stdout, &test_hook); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "tools/__init__.h" | ||
#include "test.h" | ||
|
||
static const char* s_tool_help = | ||
"missing argument after `--`, use `-- help` (pay attention to the space) to see\n" | ||
"what tools builtin.\n"; | ||
|
||
static void _before_all_test(int argc, char* argv[]) | ||
{ | ||
int i; | ||
for (i = 0; i < argc; i++) | ||
{ | ||
if (strcmp(argv[i], "--") == 0) | ||
{ | ||
if (i == argc - 1) | ||
{ | ||
fprintf(stderr, s_tool_help); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
exit(test_tool_exec(argc - i - 1, argv + i + 1)); | ||
} | ||
} | ||
} | ||
|
||
const cutest_hook_t test_hook = { | ||
_before_all_test, /* .before_all_test */ | ||
NULL, /* .after_all_test */ | ||
NULL, /* .before_setup */ | ||
NULL, /* .after_setup */ | ||
NULL, /* .before_teardown */ | ||
NULL, /* .after_teardown */ | ||
NULL, /* .before_test */ | ||
NULL, /* .after_test */ | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef __CUTEST_TEST_H__ | ||
#define __CUTEST_TEST_H__ | ||
|
||
#include "cutest.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Global test hook. | ||
*/ | ||
extern const cutest_hook_t test_hook; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "__init__.h" | ||
#include "test.h" | ||
|
||
extern const test_tool_t test_tool_help; | ||
|
||
static const test_tool_t* const g_command_table[] = { | ||
&test_tool_help, | ||
}; | ||
|
||
int test_tool_exec(int argc, char* argv[]) | ||
{ | ||
size_t i; | ||
for (i = 0; i < TEST_ARRAY_SIZE(g_command_table); i++) | ||
{ | ||
if (strcmp(argv[0], g_command_table[i]->cmd) != 0) | ||
{ | ||
continue; | ||
} | ||
|
||
return g_command_table[i]->proc(argc, argv); | ||
} | ||
|
||
fprintf(stderr, "%s: command not found\n", argv[0]); | ||
fflush(NULL); | ||
|
||
return EXIT_FAILURE; | ||
} | ||
|
||
void test_tool_foreach(int (*cb)(const test_tool_t*, void*), void* arg) | ||
{ | ||
size_t i; | ||
for (i = 0; i < TEST_ARRAY_SIZE(g_command_table); i++) | ||
{ | ||
if (cb(g_command_table[i], arg)) | ||
{ | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __CUTEST_TEST_TOOL_INIT_H__ | ||
#define __CUTEST_TEST_TOOL_INIT_H__ | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef struct test_tool | ||
{ | ||
const char* cmd; | ||
int (*proc)(int argc, char* argv[]); | ||
const char* help; | ||
} test_tool_t; | ||
|
||
int test_tool_exec(int argc, char* argv[]); | ||
void test_tool_foreach(int (*cb)(const test_tool_t*, void*), void* arg); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "__init__.h" | ||
|
||
static const char* s_help_str = | ||
"This program contains tests written using cutest. You can use the\n" | ||
"following command line flags to control its behavior:\n" | ||
"\n" | ||
"Test Selection:\n" | ||
" --help\n" | ||
" Show what features cutest support.\n" | ||
"\n" | ||
"Tool Selection:\n" | ||
" --\n" | ||
" By using `-- [CMD]` (pay attention to the space) you are able to launch\n" | ||
" builtin tools. Anything followed by `--` will be treat as command\n" | ||
" arguments to builtin tools. After builtin tools finished, the program\n" | ||
" will exit.\n" | ||
; | ||
|
||
static void _print_help(const char* help, const char* prefix) | ||
{ | ||
size_t i; | ||
int flag_need_prefix = 1; | ||
|
||
for (i = 0; help[i] != '\0'; i++) | ||
{ | ||
if (flag_need_prefix) | ||
{ | ||
flag_need_prefix = 0; | ||
printf("%s", prefix); | ||
} | ||
|
||
printf("%c", help[i]); | ||
|
||
if (help[i] == '\n') | ||
{ | ||
flag_need_prefix = 1; | ||
} | ||
} | ||
|
||
printf("\n"); | ||
} | ||
|
||
static int _print_tools(const test_tool_t* info, void* arg) | ||
{ | ||
(void)arg; | ||
|
||
printf(" -- %s\n", info->cmd); | ||
if (info->help != NULL) | ||
{ | ||
_print_help(info->help, " "); | ||
} | ||
return 0; | ||
} | ||
|
||
static int _tool_help(int argc, char* argv[]) | ||
{ | ||
(void)argc; (void)argv; | ||
|
||
printf("%s", s_help_str); | ||
test_tool_foreach(_print_tools, NULL); | ||
|
||
fflush(NULL); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
const test_tool_t test_tool_help = { | ||
"help", _tool_help, | ||
"Show this help and exit." | ||
}; |