2 * Copyright (c) Vicent Marti. All rights reserved.
4 * This file is part of clar, distributed under the ISC license.
5 * For full terms see the included COPYING file.
7 #ifndef __CLAR_TEST_H__
8 #define __CLAR_TEST_H__
19 /** Setup clar environment */
20 void clar_test_init(int argc
, char *argv
[]);
21 int clar_test_run(void);
22 void clar_test_shutdown(void);
24 /** One shot setup & run */
25 int clar_test(int argc
, char *argv
[]);
27 const char *clar_sandbox_path(void);
29 void cl_set_cleanup(void (*cleanup
)(void *), void *opaque
);
30 void cl_fs_cleanup(void);
33 * cl_trace_* is a hook to provide a simple global tracing
36 * The goal here is to let main() provide clar-proper
37 * with a callback to optionally write log info for
38 * test operations into the same stream used by their
39 * actual tests. This would let them print test names
40 * and maybe performance data as they choose.
42 * The goal is NOT to alter the flow of control or to
43 * override test selection/skipping. (So the callback
44 * does not return a value.)
46 * The goal is NOT to duplicate the existing
47 * pass/fail/skip reporting. (So the callback
48 * does not accept a status/errorcode argument.)
51 typedef enum cl_trace_event
{
52 CL_TRACE__SUITE_BEGIN
,
54 CL_TRACE__TEST__BEGIN
,
56 CL_TRACE__TEST__RUN_BEGIN
,
57 CL_TRACE__TEST__RUN_END
,
58 CL_TRACE__TEST__LONGJMP
,
61 typedef void (cl_trace_cb
)(
63 const char *suite_name
,
64 const char *test_name
,
68 * Register a callback into CLAR to send global trace events.
69 * Pass NULL to disable.
71 void cl_trace_register(cl_trace_cb
*cb
, void *payload
);
74 #ifdef CLAR_FIXTURE_PATH
75 const char *cl_fixture(const char *fixture_name
);
76 void cl_fixture_sandbox(const char *fixture_name
);
77 void cl_fixture_cleanup(const char *fixture_name
);
78 const char *cl_fixture_basename(const char *fixture_name
);
82 * Assertion macros with explicit error message
84 #define cl_must_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __LINE__, "Function call failed: " #expr, desc, 1)
85 #define cl_must_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __LINE__, "Expected function call to fail: " #expr, desc, 1)
86 #define cl_assert_(expr, desc) clar__assert((expr) != 0, __FILE__, __LINE__, "Expression is not true: " #expr, desc, 1)
89 * Check macros with explicit error message
91 #define cl_check_pass_(expr, desc) clar__assert((expr) >= 0, __FILE__, __LINE__, "Function call failed: " #expr, desc, 0)
92 #define cl_check_fail_(expr, desc) clar__assert((expr) < 0, __FILE__, __LINE__, "Expected function call to fail: " #expr, desc, 0)
93 #define cl_check_(expr, desc) clar__assert((expr) != 0, __FILE__, __LINE__, "Expression is not true: " #expr, desc, 0)
96 * Assertion macros with no error message
98 #define cl_must_pass(expr) cl_must_pass_(expr, NULL)
99 #define cl_must_fail(expr) cl_must_fail_(expr, NULL)
100 #define cl_assert(expr) cl_assert_(expr, NULL)
103 * Check macros with no error message
105 #define cl_check_pass(expr) cl_check_pass_(expr, NULL)
106 #define cl_check_fail(expr) cl_check_fail_(expr, NULL)
107 #define cl_check(expr) cl_check_(expr, NULL)
110 * Forced failure/warning
112 #define cl_fail(desc) clar__fail(__FILE__, __LINE__, "Test failed.", desc, 1)
113 #define cl_warning(desc) clar__fail(__FILE__, __LINE__, "Warning during test execution:", desc, 0)
115 #define cl_skip() clar__skip()
118 * Typed assertion macros
120 #define cl_assert_equal_s(s1,s2) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%s", (s1), (s2))
121 #define cl_assert_equal_s_(s1,s2,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%s", (s1), (s2))
123 #define cl_assert_equal_wcs(wcs1,wcs2) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2, 1, "%ls", (wcs1), (wcs2))
124 #define cl_assert_equal_wcs_(wcs1,wcs2,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%ls", (wcs1), (wcs2))
126 #define cl_assert_equal_strn(s1,s2,len) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%.*s", (s1), (s2), (int)(len))
127 #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))
129 #define cl_assert_equal_wcsn(wcs1,wcs2,len) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2, 1, "%.*ls", (wcs1), (wcs2), (int)(len))
130 #define cl_assert_equal_wcsn_(wcs1,wcs2,len,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%.*ls", (wcs1), (wcs2), (int)(len))
132 #define cl_assert_equal_i(i1,i2) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
133 #define cl_assert_equal_i_(i1,i2,note) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2 " (" #note ")", 1, "%d", (i1), (i2))
134 #define cl_assert_equal_i_fmt(i1,i2,fmt) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, (fmt), (int)(i1), (int)(i2))
136 #define cl_assert_equal_b(b1,b2) clar__assert_equal(__FILE__,__LINE__,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
138 #define cl_assert_equal_p(p1,p2) clar__assert_equal(__FILE__,__LINE__,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
140 void clar__skip(void);
146 const char *description
,
154 const char *description
,
157 void clar__assert_equal(