]>
git.ipfire.org Git - thirdparty/git.git/blob - t/unit-tests/test-lib.c
15 unsigned lazy_plan
:1;
21 .result
= RESULT_NONE
,
25 * Visual C interpolates the absolute Windows path for `__FILE__`,
26 * but we want to see relative paths, as verified by t0080.
27 * There are other compilers that do the same, and are not for
32 static const char *make_relative(const char *location
)
34 static char prefix
[] = __FILE__
, buf
[PATH_MAX
], *p
;
35 static size_t prefix_len
;
36 static int need_bs_to_fs
= -1;
38 /* one-time preparation */
39 if (need_bs_to_fs
< 0) {
40 size_t len
= strlen(prefix
);
41 char needle
[] = "t\\unit-tests\\test-lib.c";
42 size_t needle_len
= strlen(needle
);
45 die("unexpected prefix '%s'", prefix
);
48 * The path could be relative (t/unit-tests/test-lib.c)
49 * or full (/home/user/git/t/unit-tests/test-lib.c).
50 * Check the slash between "t" and "unit-tests".
52 prefix_len
= len
- needle_len
;
53 if (prefix
[prefix_len
+ 1] == '/') {
54 /* Oh, we're not Windows */
55 for (size_t i
= 0; i
< needle_len
; i
++)
56 if (needle
[i
] == '\\')
64 * prefix_len == 0 if the compiler gives paths relative
65 * to the root of the working tree. Otherwise, we want
66 * to see that we did find the needle[] at a directory
67 * boundary. Again we rely on that needle[] begins with
68 * "t" followed by the directory separator.
70 if (fspathcmp(needle
, prefix
+ prefix_len
) ||
71 (prefix_len
&& prefix
[prefix_len
- 1] != needle
[1]))
72 die("unexpected suffix of '%s'", prefix
);
76 * Does it not start with the expected prefix?
77 * Return it as-is without making it worse.
79 if (prefix_len
&& fspathncmp(location
, prefix
, prefix_len
))
83 * If we do not need to munge directory separator, we can return
84 * the substring at the tail of the location.
87 return location
+ prefix_len
;
89 /* convert backslashes to forward slashes */
90 strlcpy(buf
, location
+ prefix_len
, sizeof(buf
));
91 for (p
= buf
; *p
; p
++)
97 static void msg_with_prefix(const char *prefix
, const char *format
, va_list ap
)
101 fprintf(stdout
, "%s", prefix
);
102 vprintf(format
, ap
); /* TODO: handle newlines */
107 void test_msg(const char *format
, ...)
111 va_start(ap
, format
);
112 msg_with_prefix("# ", format
, ap
);
116 void test_plan(int count
)
118 assert(!ctx
.running
);
121 printf("1..%d\n", count
);
128 assert(!ctx
.running
);
131 test_plan(ctx
.count
);
136 void test_skip(const char *format
, ...)
142 ctx
.result
= RESULT_SKIP
;
143 va_start(ap
, format
);
145 msg_with_prefix("# skipping test - ", format
, ap
);
149 void test_skip_all(const char *format
, ...)
154 if (!ctx
.count
&& ctx
.lazy_plan
) {
155 /* We have not printed a test plan yet */
156 prefix
= "1..0 # SKIP ";
159 /* We have already printed a test plan */
160 prefix
= "Bail out! # ";
164 ctx
.result
= RESULT_SKIP
;
165 va_start(ap
, format
);
166 msg_with_prefix(prefix
, format
, ap
);
170 int test__run_begin(void)
172 assert(!ctx
.running
);
175 ctx
.result
= RESULT_NONE
;
181 static void print_description(const char *format
, va_list ap
)
184 fputs(" - ", stdout
);
189 int test__run_end(int was_run UNUSED
, const char *location
, const char *format
, ...)
197 va_start(ap
, format
);
199 switch (ctx
.result
) {
201 printf("ok %d", ctx
.count
);
202 print_description(format
, ap
);
206 printf("not ok %d", ctx
.count
);
207 print_description(format
, ap
);
211 printf("not ok %d", ctx
.count
);
212 print_description(format
, ap
);
217 printf("ok %d", ctx
.count
);
218 print_description(format
, ap
);
223 test_msg("BUG: test has no checks at %s",
224 make_relative(location
));
225 printf("not ok %d", ctx
.count
);
226 print_description(format
, ap
);
227 ctx
.result
= RESULT_FAILURE
;
237 ctx
.failed
|= ctx
.result
== RESULT_FAILURE
;
239 return ctx
.result
!= RESULT_FAILURE
;
242 static void test_fail(void)
244 assert(ctx
.result
!= RESULT_SKIP
);
246 ctx
.result
= RESULT_FAILURE
;
249 static void test_pass(void)
251 assert(ctx
.result
!= RESULT_SKIP
);
253 if (ctx
.result
== RESULT_NONE
)
254 ctx
.result
= RESULT_SUCCESS
;
257 static void test_todo(void)
259 assert(ctx
.result
!= RESULT_SKIP
);
261 if (ctx
.result
!= RESULT_FAILURE
)
262 ctx
.result
= RESULT_TODO
;
265 int test_assert(const char *location
, const char *check
, int ok
)
269 if (ctx
.result
== RESULT_SKIP
) {
270 test_msg("skipping check '%s' at %s", check
,
271 make_relative(location
));
278 test_msg("check \"%s\" failed at %s", check
,
279 make_relative(location
));
287 void test__todo_begin(void)
295 int test__todo_end(const char *location
, const char *check
, int res
)
301 if (ctx
.result
== RESULT_SKIP
)
304 test_msg("todo check '%s' succeeded at %s", check
,
305 make_relative(location
));
314 int check_bool_loc(const char *loc
, const char *check
, int ok
)
316 return test_assert(loc
, check
, ok
);
319 union test__tmp test__tmp
[2];
321 int check_int_loc(const char *loc
, const char *check
, int ok
,
322 intmax_t a
, intmax_t b
)
324 int ret
= test_assert(loc
, check
, ok
);
327 test_msg(" left: %"PRIdMAX
, a
);
328 test_msg(" right: %"PRIdMAX
, b
);
334 int check_uint_loc(const char *loc
, const char *check
, int ok
,
335 uintmax_t a
, uintmax_t b
)
337 int ret
= test_assert(loc
, check
, ok
);
340 test_msg(" left: %"PRIuMAX
, a
);
341 test_msg(" right: %"PRIuMAX
, b
);
347 static void print_one_char(char ch
, char quote
)
349 if ((unsigned char)ch
< 0x20u
|| ch
== 0x7f) {
350 /* TODO: improve handling of \a, \b, \f ... */
351 printf("\\%03o", (unsigned char)ch
);
353 if (ch
== '\\' || ch
== quote
)
359 static void print_char(const char *prefix
, char ch
)
361 printf("# %s: '", prefix
);
362 print_one_char(ch
, '\'');
363 fputs("'\n", stdout
);
366 int check_char_loc(const char *loc
, const char *check
, int ok
, char a
, char b
)
368 int ret
= test_assert(loc
, check
, ok
);
372 print_char(" left", a
);
373 print_char(" right", b
);
380 static void print_str(const char *prefix
, const char *str
)
382 printf("# %s: ", prefix
);
384 fputs("NULL\n", stdout
);
388 print_one_char(*str
++, '"');
389 fputs("\"\n", stdout
);
393 int check_str_loc(const char *loc
, const char *check
,
394 const char *a
, const char *b
)
396 int ok
= (!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
));
397 int ret
= test_assert(loc
, check
, ok
);
401 print_str(" left", a
);
402 print_str(" right", b
);