Skip to content

Commit c3bb1b0

Browse files
committed
tests: update to latest clar
1 parent 8d92da0 commit c3bb1b0

File tree

8 files changed

+1050
-224
lines changed

8 files changed

+1050
-224
lines changed

tests/clar.c

Lines changed: 313 additions & 83 deletions
Large diffs are not rendered by default.

tests/clar.h

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,93 @@
99

1010
#include <stdlib.h>
1111

12+
enum cl_test_status {
13+
CL_TEST_OK,
14+
CL_TEST_FAILURE,
15+
CL_TEST_SKIP,
16+
CL_TEST_NOTRUN,
17+
};
18+
19+
enum cl_output_format {
20+
CL_OUTPUT_CLAP,
21+
CL_OUTPUT_TAP,
22+
};
23+
24+
/** Setup clar environment */
1225
void clar_test_init(int argc, char *argv[]);
1326
int clar_test_run(void);
1427
void clar_test_shutdown(void);
1528

29+
/** One shot setup & run */
1630
int clar_test(int argc, char *argv[]);
1731

1832
const char *clar_sandbox_path(void);
1933

2034
void cl_set_cleanup(void (*cleanup)(void *), void *opaque);
2135
void cl_fs_cleanup(void);
2236

37+
/**
38+
* cl_trace_* is a hook to provide a simple global tracing
39+
* mechanism.
40+
*
41+
* The goal here is to let main() provide clar-proper
42+
* with a callback to optionally write log info for
43+
* test operations into the same stream used by their
44+
* actual tests. This would let them print test names
45+
* and maybe performance data as they choose.
46+
*
47+
* The goal is NOT to alter the flow of control or to
48+
* override test selection/skipping. (So the callback
49+
* does not return a value.)
50+
*
51+
* The goal is NOT to duplicate the existing
52+
* pass/fail/skip reporting. (So the callback
53+
* does not accept a status/errorcode argument.)
54+
*
55+
*/
56+
typedef enum cl_trace_event {
57+
CL_TRACE__SUITE_BEGIN,
58+
CL_TRACE__SUITE_END,
59+
CL_TRACE__TEST__BEGIN,
60+
CL_TRACE__TEST__END,
61+
CL_TRACE__TEST__RUN_BEGIN,
62+
CL_TRACE__TEST__RUN_END,
63+
CL_TRACE__TEST__LONGJMP,
64+
} cl_trace_event;
65+
66+
typedef void (cl_trace_cb)(
67+
cl_trace_event ev,
68+
const char *suite_name,
69+
const char *test_name,
70+
void *payload);
71+
72+
/**
73+
* Register a callback into CLAR to send global trace events.
74+
* Pass NULL to disable.
75+
*/
76+
void cl_trace_register(cl_trace_cb *cb, void *payload);
77+
78+
2379
#ifdef CLAR_FIXTURE_PATH
2480
const char *cl_fixture(const char *fixture_name);
2581
void cl_fixture_sandbox(const char *fixture_name);
2682
void cl_fixture_cleanup(const char *fixture_name);
83+
const char *cl_fixture_basename(const char *fixture_name);
2784
#endif
2885

2986
/**
3087
* Assertion macros with explicit error message
3188
*/
32-
#define cl_must_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __LINE__, "Function call failed: " #expr, desc, 1)
33-
#define cl_must_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __LINE__, "Expected function call to fail: " #expr, desc, 1)
34-
#define cl_assert_(expr, desc) clar__assert((expr) != 0, __FILE__, __LINE__, "Expression is not true: " #expr, desc, 1)
89+
#define cl_must_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __func__, __LINE__, "Function call failed: " #expr, desc, 1)
90+
#define cl_must_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __func__, __LINE__, "Expected function call to fail: " #expr, desc, 1)
91+
#define cl_assert_(expr, desc) clar__assert((expr) != 0, __FILE__, __func__, __LINE__, "Expression is not true: " #expr, desc, 1)
3592

3693
/**
3794
* Check macros with explicit error message
3895
*/
39-
#define cl_check_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __LINE__, "Function call failed: " #expr, desc, 0)
40-
#define cl_check_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __LINE__, "Expected function call to fail: " #expr, desc, 0)
41-
#define cl_check_(expr, desc) clar__assert((expr) != 0, __FILE__, __LINE__, "Expression is not true: " #expr, desc, 0)
96+
#define cl_check_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __func__, __LINE__, "Function call failed: " #expr, desc, 0)
97+
#define cl_check_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __func__, __LINE__, "Expected function call to fail: " #expr, desc, 0)
98+
#define cl_check_(expr, desc) clar__assert((expr) != 0, __FILE__, __func__, __LINE__, "Expression is not true: " #expr, desc, 0)
4299

43100
/**
44101
* Assertion macros with no error message
@@ -57,45 +114,57 @@ void cl_fixture_cleanup(const char *fixture_name);
57114
/**
58115
* Forced failure/warning
59116
*/
60-
#define cl_fail(desc) clar__fail(__FILE__, __LINE__, "Test failed.", desc, 1)
61-
#define cl_warning(desc) clar__fail(__FILE__, __LINE__, "Warning during test execution:", desc, 0)
117+
#define cl_fail(desc) clar__fail(__FILE__, __func__, __LINE__, "Test failed.", desc, 1)
118+
#define cl_warning(desc) clar__fail(__FILE__, __func__, __LINE__, "Warning during test execution:", desc, 0)
119+
120+
#define cl_skip() clar__skip()
62121

63122
/**
64123
* Typed assertion macros
65124
*/
66-
#define cl_assert_equal_s(s1,s2) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%s", (s1), (s2))
67-
#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%s", (s1), (s2))
125+
#define cl_assert_equal_s(s1,s2) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%s", (s1), (s2))
126+
#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%s", (s1), (s2))
127+
128+
#define cl_assert_equal_wcs(wcs1,wcs2) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2, 1, "%ls", (wcs1), (wcs2))
129+
#define cl_assert_equal_wcs_(wcs1,wcs2,note) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%ls", (wcs1), (wcs2))
130+
131+
#define cl_assert_equal_strn(s1,s2,len) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%.*s", (s1), (s2), (int)(len))
132+
#define cl_assert_equal_strn_(s1,s2,len,note) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%.*s", (s1), (s2), (int)(len))
68133

69-
#define cl_assert_equal_strn(s1,s2,len) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%.*s", (s1), (s2), (int)(len))
70-
#define cl_assert_equal_strn_(s1,s2,len,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%.*s", (s1), (s2), (int)(len))
134+
#define cl_assert_equal_wcsn(wcs1,wcs2,len) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2, 1, "%.*ls", (wcs1), (wcs2), (int)(len))
135+
#define cl_assert_equal_wcsn_(wcs1,wcs2,len,note) clar__assert_equal(__FILE__,__func__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%.*ls", (wcs1), (wcs2), (int)(len))
71136

72-
#define cl_assert_equal_i(i1,i2) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
73-
#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2 " (" #note ")", 1, "%d", (i1), (i2))
74-
#define cl_assert_equal_i_fmt(i1,i2,fmt) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, (fmt), (int)(i1), (int)(i2))
137+
#define cl_assert_equal_i(i1,i2) clar__assert_equal(__FILE__,__func__,__LINE__,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
138+
#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal(__FILE__,__func__,__LINE__,#i1 " != " #i2 " (" #note ")", 1, "%d", (i1), (i2))
139+
#define cl_assert_equal_i_fmt(i1,i2,fmt) clar__assert_equal(__FILE__,__func__,__LINE__,#i1 " != " #i2, 1, (fmt), (int)(i1), (int)(i2))
75140

76-
#define cl_assert_equal_b(b1,b2) clar__assert_equal(__FILE__,__LINE__,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
141+
#define cl_assert_equal_b(b1,b2) clar__assert_equal(__FILE__,__func__,__LINE__,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
77142

78-
#define cl_assert_equal_p(p1,p2) clar__assert_equal(__FILE__,__LINE__,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
143+
#define cl_assert_equal_p(p1,p2) clar__assert_equal(__FILE__,__func__,__LINE__,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
79144

145+
void clar__skip(void);
80146

81147
void clar__fail(
82148
const char *file,
83-
int line,
149+
const char *func,
150+
size_t line,
84151
const char *error,
85152
const char *description,
86153
int should_abort);
87154

88155
void clar__assert(
89156
int condition,
90157
const char *file,
91-
int line,
158+
const char *func,
159+
size_t line,
92160
const char *error,
93161
const char *description,
94162
int should_abort);
95163

96164
void clar__assert_equal(
97165
const char *file,
98-
int line,
166+
const char *func,
167+
size_t line,
99168
const char *err,
100169
int should_abort,
101170
const char *fmt,

tests/clar/fixtures.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef CLAR_FIXTURE_PATH
12
static const char *
23
fixture_path(const char *base, const char *fixture_name)
34
{
@@ -20,7 +21,6 @@ fixture_path(const char *base, const char *fixture_name)
2021
return _path;
2122
}
2223

23-
#ifdef CLAR_FIXTURE_PATH
2424
const char *cl_fixture(const char *fixture_name)
2525
{
2626
return fixture_path(CLAR_FIXTURE_PATH, fixture_name);
@@ -31,8 +31,20 @@ void cl_fixture_sandbox(const char *fixture_name)
3131
fs_copy(cl_fixture(fixture_name), _clar_path);
3232
}
3333

34+
const char *cl_fixture_basename(const char *fixture_name)
35+
{
36+
const char *p;
37+
38+
for (p = fixture_name; *p; p++) {
39+
if (p[0] == '/' && p[1] && p[1] != '/')
40+
fixture_name = p+1;
41+
}
42+
43+
return fixture_name;
44+
}
45+
3446
void cl_fixture_cleanup(const char *fixture_name)
3547
{
36-
fs_rm(fixture_path(_clar_path, fixture_name));
48+
fs_rm(fixture_path(_clar_path, cl_fixture_basename(fixture_name)));
3749
}
3850
#endif

0 commit comments

Comments
 (0)