8000 Merge pull request #7029 from libgit2/ethomson/clar-update · timonvo/libgit2@bad4a07 · GitHub
[go: up one dir, main page]

Skip to content

Commit bad4a07

Browse files
authored
Merge pull request libgit2#7029 from libgit2/ethomson/clar-update
clar: update to latest version
2 parents d34d905 + 72d49bb commit bad4a07

File tree

10 files changed

+396
-176
lines changed

10 files changed

+396
-176
lines changed

tests/clar/clar.c

Lines changed: 147 additions & 78 deletions
Large diffs are not rendered by default.

tests/clar/clar.h

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88
#define __CLAR_TEST_H__
99

1010
#include <stdlib.h>
11+
#include <limits.h>
12+
13+
#if defined(_WIN32) && defined(CLAR_WIN32_LONGPATHS)
14+
# define CLAR_MAX_PATH 4096
15+
#elif defined(_WIN32)
16+
# define CLAR_MAX_PATH MAX_PATH
17+
#else
18+
# define CLAR_MAX_PATH PATH_MAX
19+
#endif
20+
21+
#ifndef CLAR_SELFTEST
22+
# define CLAR_CURRENT_FILE __FILE__
23+
# define CLAR_CURRENT_LINE __LINE__
24+
# define CLAR_CURRENT_FUNC __func__
25+
#else
26+
# define CLAR_CURRENT_FILE "file"
27+
# define CLAR_CURRENT_LINE 42
28+
# define CLAR_CURRENT_FUNC "func"
29+
#endif
1130

1231
enum cl_test_status {
1332
CL_TEST_OK,
@@ -30,6 +49,7 @@ void clar_test_shutdown(void);
3049
int clar_test(int argc, char *argv[]);
3150

3251
const char *clar_sandbox_path(void);
52+
const char *clar_tempdir_path(void);
3353

3454
void cl_set_cleanup(void (*cleanup)(void *), void *opaque);
3555
void cl_fs_cleanup(void);
@@ -83,19 +103,33 @@ void cl_fixture_cleanup(const char *fixture_name);
83103
const char *cl_fixture_basename(const char *fixture_name);
84104
#endif
85105

106+
/**
107+
* Invoke a helper function, which itself will use `cl_assert`
108+
* constructs. This will preserve the stack information of the
109+
* current call point, so that function name and line number
110+
* information is shown from the line of the test, instead of
111+
* the helper function.
112+
*/
113+
#define cl_invoke(expr) \
114+
do { \
115+
clar__set_invokepoint(CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE); \
116+
expr; \
117+
clar__clear_invokepoint(); \
118+
} while(0)
119+
86120
/**
87121
* Assertion macros with explicit error message
88122
*/
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)
123+
#define cl_must_pass_(expr, desc) clar__assert((expr) >= 0, CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Function call failed: " #expr, desc, 1)
124+
#define cl_must_fail_(expr, desc) clar__assert((expr) < 0, CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Expected function call to fail: " #expr, desc, 1)
125+
#define cl_assert_(expr, desc) clar__assert((expr) != 0, CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Expression is not true: " #expr, desc, 1)
92126

93127
/**
94128
* Check macros with explicit error message
95129
*/
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)
130+
#define cl_check_pass_(expr, desc) clar__assert((expr) >= 0, CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Function call failed: " #expr, desc, 0)
131+
#define cl_check_fail_(expr, desc) clar__assert((expr) < 0, CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Expected function call to fail: " #expr, desc, 0)
132+
#define cl_check_(expr, desc) clar__assert((expr) != 0, CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Expression is not true: " #expr, desc, 0)
99133

100134
/**
101135
* Assertion macros with no error message
@@ -114,33 +148,33 @@ const char *cl_fixture_basename(const char *fixture_name);
114148
/**
115149
* Forced failure/warning
116150
*/
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)
151+
#define cl_fail(desc) clar__fail(CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Test failed.", desc, 1)
152+
#define cl_warning(desc) clar__fail(CLAR_CURRENT_FILE, CLAR_CURRENT_FUNC, CLAR_CURRENT_LINE, "Warning during test execution:", desc, 0)
119153

120154
#define cl_skip() clar__skip()
121155

122156
/**
123157
* Typed assertion macros
124158
*/
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))
159+
#define cl_assert_equal_s(s1,s2) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #s1 " != " #s2, 1, "%s", (s1), (s2))
160+
#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%s", (s1), (s2))
127161

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))
162+
#define cl_assert_equal_wcs(wcs1,wcs2) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #wcs1 " != " #wcs2, 1, "%ls", (wcs1), (wcs2))
163+
#define cl_assert_equal_wcs_(wcs1,wcs2,note) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%ls", (wcs1), (wcs2))
130164

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))
165+
#define cl_assert_equal_strn(s1,s2,len) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #s1 " != " #s2, 1, "%.*s", (s1), (s2), (int)(len))
166+
#define cl_assert_equal_strn_(s1,s2,len,note) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%.*s", (s1), (s2), (int)(len))
133167

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))
168+
#define cl_assert_equal_wcsn(wcs1,wcs2,len) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #wcs1 " != " #wcs2, 1, "%.*ls", (wcs1), (wcs2), (int)(len))
169+
#define cl_assert_equal_wcsn_(wcs1,wcs2,len,note) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%.*ls", (wcs1), (wcs2), (int)(len))
136170

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))
171+
#define cl_assert_equal_i(i1,i2) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
172+
#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,#i1 " != " #i2 " (" #note ")", 1, "%d", (i1), (i2))
173+
#define cl_assert_equal_i_fmt(i1,i2,fmt) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,#i1 " != " #i2, 1, (fmt), (int)(i1), (int)(i2))
140174

141-
#define cl_assert_equal_b(b1,b2) clar__assert_equal(__FILE__,__func__,__LINE__,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
175+
#define cl_assert_equal_b(b1,b2) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
142176

143-
#define cl_assert_equal_p(p1,p2) clar__assert_equal(__FILE__,__func__,__LINE__,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
177+
#define cl_assert_equal_p(p1,p2) clar__assert_equal(CLAR_CURRENT_FILE,CLAR_CURRENT_FUNC,CLAR_CURRENT_LINE,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
144178

145179
void clar__skip(void);
146180

@@ -170,4 +204,11 @@ void clar__assert_equal(
170204
const char *fmt,
171205
...);
172206

207+
void clar__set_invokepoint(
208+
const char *file,
209+
const char *func,
210+
size_t line);
211+
212+
void clar__clear_invokepoint(void);
213+
173214
#endif

tests/clar/clar/fixtures.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
static const char *
33
fixture_path(const char *base, const char *fixture_name)
44
{
5-
static char _path[4096];
5+
static char _path[CLAR_MAX_PATH];
66
size_t root_len;
77

88
root_len = strlen(base);
@@ -28,7 +28,7 @@ const char *cl_fixture(const char *fixture_name)
2828

2929
void cl_fixture_sandbox(const char *fixture_name)
3030
{
31-
fs_copy(cl_fixture(fixture_name), _clar_path);
31+
fs_copy(cl_fixture(fixture_name), clar_sandbox_path());
3232
}
3333

3434
const char *cl_fixture_basename(const char *fixture_name)
@@ -45,6 +45,6 @@ const char *cl_fixture_basename(const char *fixture_name)
4545

4646
void cl_fixture_cleanup(const char *fixture_name)
4747
{
48-
fs_rm(fixture_path(_clar_path, cl_fixture_basename(fixture_name)));
48+
fs_rm(fixture_path(clar_sandbox_path(), cl_fixture_basename(fixture_name)));
4949
}
5050
#endif

tests/clar/clar/fs.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88

99
#ifdef _WIN32
1010

11-
#ifdef CLAR_WIN32_LONGPATHS
12-
# define CLAR_MAX_PATH 4096
13-
#else
14-
# define CLAR_MAX_PATH MAX_PATH
15-
#endif
16-
1711
#define RM_RETRY_COUNT 5
1812
#define RM_RETRY_DELAY 10
1913

@@ -146,7 +140,7 @@ fs_rm_wait(WCHAR *_wpath)
146140
ERROR_PATH_NOT_FOUND == last_error)
147141
return 0;
148142

149-
Sleep(RM_RETRY_DELAY * retries * retries);
143+
Sleep(RM_RETRY_DELAY * retries * retries);
150144
}
151145
while (retries++ <= RM_RETRY_COUNT);
152146

@@ -296,7 +290,9 @@ void
296290
cl_fs_cleanup(void)
297291
{
298292
#ifdef CLAR_FIXTURE_PATH
299-
fs_rm(fixture_path(_clar_path, "*"));
293+
fs_rm(fixture_path(clar_tempdir_path(), "*"));
294+
#else
295+
((void)fs_copy); /* unused */
300296
#endif
301297
}
302298

@@ -516,7 +512,7 @@ fs_rm(const char *path)
516512
void
517513
cl_fs_cleanup(void)
518514
{
519-
clar_unsandbox();
520-
clar_sandbox();
515+
clar_tempdir_shutdown();
516+
clar_tempdir_init();
521517
}
522518
#endif

tests/clar/clar/print.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void clar_print_clap_error(int num, const struct clar_report *report, con
2121
{
2222
printf(" %d) Failure:\n", num);
2323

24-
printf("%s::%s [%s:%"PRIuZ"]\n",
24+
printf("%s::%s [%s:%"PRIuMAX"]\n",
2525
report->suite,
2626
report->test,
2727
error->file,
@@ -47,8 +47,8 @@ static void clar_print_clap_ontest(const char *suite_name, const char *test_name
4747
switch (status) {
4848
case CL_TEST_OK: printf("ok\n"); break;
4949
case CL_TEST_FAILURE: printf("fail\n"); break;
50-
case CL_TEST_SKIP: printf("skipped"); break;
51-
case CL_TEST_NOTRUN: printf("notrun"); break;
50+
case CL_TEST_SKIP: printf("skipped\n"); break;
51+
case CL_TEST_NOTRUN: printf("notrun\n"); break;
5252
}
5353
} else {
5454
switch (status) {
@@ -136,7 +136,7 @@ static void clar_print_tap_ontest(const char *suite_name, const char *test_name,
136136

137137
printf(" at:\n");
138138
printf(" file: '"); print_escaped(error->file); printf("'\n");
139-
printf(" line: %" PRIuZ "\n", error->line_number);
139+
printf(" line: %" PRIuMAX "\n", error->line_number);
140140
printf(" function: '%s'\n", error->function);
141141
printf(" ---\n");
142142

@@ -202,10 +202,15 @@ static void clar_print_onsuite(const char *suite_name, int suite_index)
202202
PRINT(onsuite, suite_name, suite_index);
203203
}
204204

205+
static void clar_print_onabortv(const char *msg, va_list argp)
206+
{
207+
PRINT(onabort, msg, argp);
208+
}
209+
205210
static void clar_print_onabort(const char *msg, ...)
206211
{
207212
va_list argp;
208213
va_start(argp, msg);
209-
PRINT(onabort, msg, argp);
214+
clar_print_onabortv(msg, argp);
210215
va_end(argp);
211216
}

0 commit comments

Comments
 (0)
0