Skip to content

Commit

Permalink
rewrite test
Browse files Browse the repository at this point in the history
  • Loading branch information
qgymib committed Apr 25, 2024
1 parent a806b99 commit d8dde0a
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,5 @@ endif()
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
add_subdirectory(example)
add_subdirectory(test)
add_subdirectory(test2)
endif()
19 changes: 19 additions & 0 deletions test2/CMakeLists.txt
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>)
6 changes: 6 additions & 0 deletions test2/main.c
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);
}
37 changes: 37 additions & 0 deletions test2/test.c
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 */
};
18 changes: 18 additions & 0 deletions test2/test.h
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
41 changes: 41 additions & 0 deletions test2/tools/__init__.c
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;
}
}
}
20 changes: 20 additions & 0 deletions test2/tools/__init__.h
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
72 changes: 72 additions & 0 deletions test2/tools/help.c
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."
};

0 comments on commit d8dde0a

Please sign in to comment.