]>
git.ipfire.org Git - thirdparty/git.git/blob - t/unit-tests/test-lib.c
15 unsigned lazy_plan
:1;
21 .result
= RESULT_NONE
,
25 #define make_relative(location) location
28 * Visual C interpolates the absolute Windows path for `__FILE__`,
29 * but we want to see relative paths, as verified by t0080.
33 static const char *make_relative(const char *location
)
35 static char prefix
[] = __FILE__
, buf
[PATH_MAX
], *p
;
36 static size_t prefix_len
;
39 size_t len
= strlen(prefix
);
40 const char *needle
= "\\t\\unit-tests\\test-lib.c";
41 size_t needle_len
= strlen(needle
);
43 if (len
< needle_len
|| strcmp(needle
, prefix
+ len
- needle_len
))
44 die("unexpected suffix of '%s'", prefix
);
46 /* let it end in a directory separator */
47 prefix_len
= len
- needle_len
+ 1;
50 /* Does it not start with the expected prefix? */
51 if (fspathncmp(location
, prefix
, prefix_len
))
54 strlcpy(buf
, location
+ prefix_len
, sizeof(buf
));
55 /* convert backslashes to forward slashes */
56 for (p
= buf
; *p
; p
++)
64 static void msg_with_prefix(const char *prefix
, const char *format
, va_list ap
)
68 fprintf(stdout
, "%s", prefix
);
69 vprintf(format
, ap
); /* TODO: handle newlines */
74 void test_msg(const char *format
, ...)
79 msg_with_prefix("# ", format
, ap
);
83 void test_plan(int count
)
88 printf("1..%d\n", count
);
103 void test_skip(const char *format
, ...)
109 ctx
.result
= RESULT_SKIP
;
110 va_start(ap
, format
);
112 msg_with_prefix("# skipping test - ", format
, ap
);
116 void test_skip_all(const char *format
, ...)
121 if (!ctx
.count
&& ctx
.lazy_plan
) {
122 /* We have not printed a test plan yet */
123 prefix
= "1..0 # SKIP ";
126 /* We have already printed a test plan */
127 prefix
= "Bail out! # ";
131 ctx
.result
= RESULT_SKIP
;
132 va_start(ap
, format
);
133 msg_with_prefix(prefix
, format
, ap
);
137 int test__run_begin(void)
139 assert(!ctx
.running
);
142 ctx
.result
= RESULT_NONE
;
148 static void print_description(const char *format
, va_list ap
)
151 fputs(" - ", stdout
);
156 int test__run_end(int was_run UNUSED
, const char *location
, const char *format
, ...)
164 va_start(ap
, format
);
166 switch (ctx
.result
) {
168 printf("ok %d", ctx
.count
);
169 print_description(format
, ap
);
173 printf("not ok %d", ctx
.count
);
174 print_description(format
, ap
);
178 printf("not ok %d", ctx
.count
);
179 print_description(format
, ap
);
184 printf("ok %d", ctx
.count
);
185 print_description(format
, ap
);
190 test_msg("BUG: test has no checks at %s",
191 make_relative(location
));
192 printf("not ok %d", ctx
.count
);
193 print_description(format
, ap
);
194 ctx
.result
= RESULT_FAILURE
;
204 ctx
.failed
|= ctx
.result
== RESULT_FAILURE
;
206 return ctx
.result
!= RESULT_FAILURE
;
209 static void test_fail(void)
211 assert(ctx
.result
!= RESULT_SKIP
);
213 ctx
.result
= RESULT_FAILURE
;
216 static void test_pass(void)
218 assert(ctx
.result
!= RESULT_SKIP
);
220 if (ctx
.result
== RESULT_NONE
)
221 ctx
.result
= RESULT_SUCCESS
;
224 static void test_todo(void)
226 assert(ctx
.result
!= RESULT_SKIP
);
228 if (ctx
.result
!= RESULT_FAILURE
)
229 ctx
.result
= RESULT_TODO
;
232 int test_assert(const char *location
, const char *check
, int ok
)
236 if (ctx
.result
== RESULT_SKIP
) {
237 test_msg("skipping check '%s' at %s", check
,
238 make_relative(location
));
245 test_msg("check \"%s\" failed at %s", check
,
246 make_relative(location
));
254 void test__todo_begin(void)
262 int test__todo_end(const char *location
, const char *check
, int res
)
268 if (ctx
.result
== RESULT_SKIP
)
271 test_msg("todo check '%s' succeeded at %s", check
,
272 make_relative(location
));
281 int check_bool_loc(const char *loc
, const char *check
, int ok
)
283 return test_assert(loc
, check
, ok
);
286 union test__tmp test__tmp
[2];
288 int check_int_loc(const char *loc
, const char *check
, int ok
,
289 intmax_t a
, intmax_t b
)
291 int ret
= test_assert(loc
, check
, ok
);
294 test_msg(" left: %"PRIdMAX
, a
);
295 test_msg(" right: %"PRIdMAX
, b
);
301 int check_uint_loc(const char *loc
, const char *check
, int ok
,
302 uintmax_t a
, uintmax_t b
)
304 int ret
= test_assert(loc
, check
, ok
);
307 test_msg(" left: %"PRIuMAX
, a
);
308 test_msg(" right: %"PRIuMAX
, b
);
314 static void print_one_char(char ch
, char quote
)
316 if ((unsigned char)ch
< 0x20u
|| ch
== 0x7f) {
317 /* TODO: improve handling of \a, \b, \f ... */
318 printf("\\%03o", (unsigned char)ch
);
320 if (ch
== '\\' || ch
== quote
)
326 static void print_char(const char *prefix
, char ch
)
328 printf("# %s: '", prefix
);
329 print_one_char(ch
, '\'');
330 fputs("'\n", stdout
);
333 int check_char_loc(const char *loc
, const char *check
, int ok
, char a
, char b
)
335 int ret
= test_assert(loc
, check
, ok
);
339 print_char(" left", a
);
340 print_char(" right", b
);
347 static void print_str(const char *prefix
, const char *str
)
349 printf("# %s: ", prefix
);
351 fputs("NULL\n", stdout
);
355 print_one_char(*str
++, '"');
356 fputs("\"\n", stdout
);
360 int check_str_loc(const char *loc
, const char *check
,
361 const char *a
, const char *b
)
363 int ok
= (!a
&& !b
) || (a
&& b
&& !strcmp(a
, b
));
364 int ret
= test_assert(loc
, check
, ok
);
368 print_str(" left", a
);
369 print_str(" right", b
);