Skip to content

Commit 34713f5

Browse files
committed
rewrite test
1 parent 45d93ed commit 34713f5

File tree

8 files changed

+91
-5
lines changed

8 files changed

+91
-5
lines changed

src/cutest.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,14 +905,31 @@ static int _cutest_porting_color_vfprintf(FILE* stream, cutest_porting_color_t c
905905

906906
#endif
907907

908+
/**
909+
* @brief Protocol type of #cutest_porting_cvfprintf().
910+
*/
911+
typedef int (*cutest_porting_cvfprintf_fn)(FILE* stream, int color,
912+
const char* fmt, va_list ap);
913+
908914
/**
909915
* @brief Print data to \p stream.
910916
*/
911917
int cutest_porting_cvfprintf(FILE* stream, int color, const char* fmt, va_list ap)
912918
{
913-
int ret;
914-
CUTEST_PORTING_ASSERT(stream != NULL);
919+
/* Internal behavior for capture output. */
920+
static cutest_porting_cvfprintf_fn s_proxy = NULL;
921+
if (stream == NULL && color == -1)
922+
{
923+
s_proxy = (cutest_porting_cvfprintf_fn)fmt;
924+
return 0;
925+
}
926+
if (s_proxy != NULL)
927+
{
928+
return s_proxy(stream, color, fmt, ap);
929+
}
915930

931+
/* Standard behavior. */
932+
int ret;
916933
int stream_fd = fileno(stream);
917934
if (!_cutest_should_use_color(isatty(stream_fd)) || (color == CUTEST_COLOR_DEFAULT))
918935
{

test2/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
add_executable(cutest_test
2+
cases/__init__.c
3+
cases/cmd_also_run_disabled_tests.c
24
tools/__init__.c
35
tools/eolcheck.c
46
tools/help.c

test2/cases/__init__.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdlib.h>
2+
#include "__init__.h"
3+
4+
extern const test_case_t cmd_also_run_disabled_tests;
5+
6+
static const test_case_t* s_test_cases[] = {
7+
&cmd_also_run_disabled_tests,
8+
};
9+
10+
int test_run_all_tests(void)
11+
{
12+
size_t i;
13+
for (i = 0; i < TEST_ARRAY_SIZE(s_test_cases); i++)
14+
{
15+
const test_case_t* tc = s_test_cases[i];
16+
17+
fprintf(stdout, "[ RUN ] %s\n", tc->name);
18+
tc->proc();
19+
fprintf(stdout, "[ OK ] %s\n", tc->name);
20+
}
21+
22+
return 0;
23+
}

test2/cases/__init__.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef __CUTEST_TEST_CASE_INIT_H__
2+
#define __CUTEST_TEST_CASE_INIT_H__
3+
4+
#include "cutest.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
typedef struct test_case
11+
{
12+
/**
13+
* @brief Test case name.
14+
*/
15+
const char* name;
16+
17+
/**
18+
* @brief Test case body.
19+
*/
20+
void (*proc)(void);
21+
} test_case_t;
22+
23+
int test_run_all_tests(void);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "__init__.h"
2+
3+
4+
5+
static void _cmd_also_run_disabled_tests(void)
6+
{
7+
8+
}
9+
10+
const test_case_t cmd_also_run_disabled_tests = {
11+
"cmd.also_run_disabled_tests", _cmd_also_run_disabled_tests,
12+
};

test2/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#include "cases/__init__.h"
12
#include "test.h"
23

34
int main(int argc, char* argv[])
45
{
5-
return cutest_run_tests(argc, argv, stdout, &test_hook);
6+
cutest_test_run_tools(argc, argv);
7+
return test_run_all_tests();
68
}

test2/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static const char* s_tool_help =
88
"what tools builtin.\n"
99
;
1010

11-
static void _before_all_test(int argc, char* argv[])
11+
void cutest_test_run_tools(int argc, char* argv[])
1212
{
1313
int i;
1414
for (i = 0; i < argc; i++)
@@ -27,7 +27,7 @@ static void _before_all_test(int argc, char* argv[])
2727
}
2828

2929
const cutest_hook_t test_hook = {
30-
_before_all_test, /* .before_all_test */
30+
NULL, /* .before_all_test */
3131
NULL, /* .after_all_test */
3232
NULL, /* .before_setup */
3333
NULL, /* .after_setup */

test2/test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern "C" {
1212
*/
1313
extern const cutest_hook_t test_hook;
1414

15+
void cutest_test_run_tools(int argc, char* argv[]);
16+
1517
#ifdef __cplusplus
1618
}
1719
#endif

0 commit comments

Comments
 (0)