]> git.proxmox.com Git - libgit2.git/blob - tests/clar.h
Update upstream source from tag 'upstream/1.0.0+dfsg.1'
[libgit2.git] / tests / clar.h
1 /*
2 * Copyright (c) Vicent Marti. All rights reserved.
3 *
4 * This file is part of clar, distributed under the ISC license.
5 * For full terms see the included COPYING file.
6 */
7 #ifndef __CLAR_TEST_H__
8 #define __CLAR_TEST_H__
9
10 #include <stdlib.h>
11
12 enum cl_test_status {
13 CL_TEST_OK,
14 CL_TEST_FAILURE,
15 CL_TEST_SKIP,
16 CL_TEST_NOTRUN,
17 };
18
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);
23
24 /** One shot setup & run */
25 int clar_test(int argc, char *argv[]);
26
27 const char *clar_sandbox_path(void);
28
29 void cl_set_cleanup(void (*cleanup)(void *), void *opaque);
30 void cl_fs_cleanup(void);
31
32 /**
33 * cl_trace_* is a hook to provide a simple global tracing
34 * mechanism.
35 *
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.
41 *
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.)
45 *
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.)
49 *
50 */
51 typedef enum cl_trace_event {
52 CL_TRACE__SUITE_BEGIN,
53 CL_TRACE__SUITE_END,
54 CL_TRACE__TEST__BEGIN,
55 CL_TRACE__TEST__END,
56 CL_TRACE__TEST__RUN_BEGIN,
57 CL_TRACE__TEST__RUN_END,
58 CL_TRACE__TEST__LONGJMP,
59 } cl_trace_event;
60
61 typedef void (cl_trace_cb)(
62 cl_trace_event ev,
63 const char *suite_name,
64 const char *test_name,
65 void *payload);
66
67 /**
68 * Register a callback into CLAR to send global trace events.
69 * Pass NULL to disable.
70 */
71 void cl_trace_register(cl_trace_cb *cb, void *payload);
72
73
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);
79 #endif
80
81 /**
82 * Assertion macros with explicit error message
83 */
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)
87
88 /**
89 * Check macros with explicit error message
90 */
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)
94
95 /**
96 * Assertion macros with no error message
97 */
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)
101
102 /**
103 * Check macros with no error message
104 */
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)
108
109 /**
110 * Forced failure/warning
111 */
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)
114
115 #define cl_skip() clar__skip()
116
117 /**
118 * Typed assertion macros
119 */
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))
122
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))
125
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))
128
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))
131
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))
135
136 #define cl_assert_equal_b(b1,b2) clar__assert_equal(__FILE__,__LINE__,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
137
138 #define cl_assert_equal_p(p1,p2) clar__assert_equal(__FILE__,__LINE__,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
139
140 void clar__skip(void);
141
142 void clar__fail(
143 const char *file,
144 size_t line,
145 const char *error,
146 const char *description,
147 int should_abort);
148
149 void clar__assert(
150 int condition,
151 const char *file,
152 size_t line,
153 const char *error,
154 const char *description,
155 int should_abort);
156
157 void clar__assert_equal(
158 const char *file,
159 size_t line,
160 const char *err,
161 int should_abort,
162 const char *fmt,
163 ...);
164
165 #endif