Skip to content

Commit bf9fc12

Browse files
tiennouethomson
authored andcommitted
Isolate test reports
This makes it possible to keep track of every test status (even successful ones), and their errors, if any.
1 parent 90753a9 commit bf9fc12

File tree

3 files changed

+82
-36
lines changed

3 files changed

+82
-36
lines changed

tests/clar.c

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ static const char *
9595
fixture_path(const char *base, const char *fixture_name);
9696

9797
struct clar_error {
98-
const char *test;
99-
int test_number;
100-
const char *suite;
10198
const char *file;
10299
int line_number;
103100
const char *error_msg;
@@ -113,8 +110,22 @@ struct clar_explicit {
113110
struct clar_explicit *next;
114111
};
115112

113+
struct clar_report {
114+
const char *test;
115+
int test_number;
116+
const char *suite;
117+
118+
enum cl_test_status status;
119+
120+
struct clar_error *errors;
121+
struct clar_error *last_error;
122+
123+
struct clar_report *next;
124+
};
125+
116126
static struct {
117127
enum cl_test_status test_status;
128+
118129
const char *active_test;
119130
const char *active_suite;
120131

@@ -131,8 +142,8 @@ static struct {
131142
struct clar_explicit *explicit;
132143
struct clar_explicit *last_explicit;
133144

134-
struct clar_error *errors;
135-
struct clar_error *last_error;
145+
struct clar_report *reports;
146+
struct clar_report *last_report;
136147

137148
void (*local_cleanup)(void *);
138149
void *local_cleanup_payload;
@@ -162,7 +173,7 @@ struct clar_suite {
162173
/* From clar_print_*.c */
163174
static void clar_print_init(int test_count, int suite_count, const char *suite_names);
164175
static void clar_print_shutdown(int test_count, int suite_count, int error_count);
165-
static void clar_print_error(int num, const struct clar_error *error);
176+
static void clar_print_error(int num, const struct clar_report *report, const struct clar_error *error);
166177
static void clar_print_ontest(const char *test_name, int test_number, enum cl_test_status failed);
167178
static void clar_print_onsuite(const char *suite_name, int suite_index);
168179
static void clar_print_onabort(const char *msg, ...);
@@ -193,21 +204,33 @@ void cl_trace_register(cl_trace_cb *cb, void *payload)
193204

194205
/* Core test functions */
195206
static void
196-
clar_report_errors(void)
207+
clar_report(int *i, struct clar_error *error)
197208
{
198-
int i = 1;
199-
struct clar_error *error, *next;
200-
201-
error = _clar.errors;
202209
while (error != NULL) {
203-
next = error->next;
204-
clar_print_error(i++, error);
205-
free(error->description);
206-
free(error);
207-
error = next;
210+
clar_print_error((*i)++, _clar.last_report, error);
211+
error = error->next;
208212
}
213+
}
209214

210-
_clar.errors = _clar.last_error = NULL;
215+
static void
216+
clar_report_errors(struct clar_error *error)
217+
{
218+
int i = 1;
219+
clar_report(&i, error);
220+
}
221+
222+
static void
223+
clar_report_all(void)
224+
{
225+
int i = 1;
226+
struct clar_report *report;
227+
228+
report = _clar.reports;
229+
while (report != NULL) {
230+
if (report->status == CL_TEST_FAILURE)
231+
clar_report(&i, report->errors);
232+
report = report->next;
233+
}
211234
}
212235

213236
static void
@@ -216,7 +239,6 @@ clar_run_test(
216239
const struct clar_func *initialize,
217240
const struct clar_func *cleanup)
218241
{
219-
_clar.test_status = CL_TEST_OK;
220242
_clar.trampoline_enabled = 1;
221243

222244
CL_TRACE(CL_TRACE__TEST__BEGIN);
@@ -232,6 +254,9 @@ clar_run_test(
232254

233255
_clar.trampoline_enabled = 0;
234256

257+
if (_clar.last_report->status == CL_TEST_NOTRUN)
258+
_clar.last_report->status = CL_TEST_OK;
259+
235260
if (_clar.local_cleanup != NULL)
236261
_clar.local_cleanup(_clar.local_cleanup_payload);
237262

@@ -247,9 +272,9 @@ clar_run_test(
247272
_clar.local_cleanup_payload = NULL;
248273

249274
if (_clar.report_errors_only) {
250-
clar_report_errors();
275+
clar_report_errors(_clar.last_report->errors);
251276
} else {
252-
clar_print_ontest(test->name, _clar.tests_ran, _clar.test_status);
277+
clar_print_ontest(test->name, _clar.tests_ran, _clar.last_report->status);
253278
}
254279
}
255280

@@ -258,6 +283,7 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
258283
{
259284
const struct clar_func *test = suite->tests;
260285
size_t i, matchlen;
286+
struct clar_report *report;
261287

262288
if (!suite->enabled)
263289
return;
@@ -290,6 +316,21 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
290316
continue;
291317

292318
_clar.active_test = test[i].name;
319+
320+
report = calloc(1, sizeof(struct clar_report));
321+
report->suite = _clar.active_suite;
322+
report->test = _clar.active_test;
323+
report->test_number = _clar.tests_ran;
324+
report->status = CL_TEST_NOTRUN;
325+
326+
if (_clar.reports == NULL)
327+
_clar.reports = report;
328+
329+
if (_clar.last_report != NULL)
330+
_clar.last_report->next = report;
331+
332+
_clar.last_report = report;
333+
293334
clar_run_test(&test[i], &suite->initialize, &suite->cleanup);
294335

295336
if (_clar.exit_on_error && _clar.total_errors)
@@ -466,6 +507,7 @@ void
466507
clar_test_shutdown(void)
467508
{
468509
struct clar_explicit *explicit, *explicit_next;
510+
struct clar_report *report, *report_next;
469511

470512
clar_print_shutdown(
471513
_clar.tests_ran,
@@ -479,6 +521,11 @@ clar_test_shutdown(void)
479521
explicit_next = explicit->next;
480522
free(explicit);
481523
}
524+
525+
for (report = _clar.reports; report; report = report_next) {
526+
report_next = report->next;
527+
free(report);
528+
}
482529
}
483530

484531
int
@@ -498,7 +545,7 @@ static void abort_test(void)
498545
if (!_clar.trampoline_enabled) {
499546
clar_print_onabort(
500547
"Fatal error: a cleanup method raised an exception.");
501-
clar_report_errors();
548+
clar_report_errors(_clar.last_report->errors);
502549
exit(-1);
503550
}
504551

@@ -508,7 +555,7 @@ static void abort_test(void)
508555

509556
void clar__skip(void)
510557
{
511-
_clar.test_status = CL_TEST_SKIP;
558+
_clar.last_report->status = CL_TEST_SKIP;
512559
_clar.total_skipped++;
513560
abort_test();
514561
}
@@ -522,17 +569,14 @@ void clar__fail(
522569
{
523570
struct clar_error *error = calloc(1, sizeof(struct clar_error));
524571

525-
if (_clar.errors == NULL)
526-
_clar.errors = error;
572+
if (_clar.last_report->errors == NULL)
573+
_clar.last_report->errors = error;
527574

528-
if (_clar.last_error != NULL)
529-
_clar.last_error->next = error;
575+
if (_clar.last_report->last_error != NULL)
576+
_clar.last_report->last_error->next = error;
530577

531-
_clar.last_error = error;
578+
_clar.last_report->last_error = error;
532579

533-
error->test = _clar.active_test;
534-
error->test_number = _clar.tests_ran;
535-
error->suite = _clar.active_suite;
536580
error->file = file;
537581
error->line_number = line;
538582
error->error_msg = error_msg;
@@ -541,7 +585,7 @@ void clar__fail(
541585
error->description = strdup(description);
542586

543587
_clar.total_errors++;
544-
_clar.test_status = CL_TEST_FAILURE;
588+
_clar.last_report->status = CL_TEST_FAILURE;
545589

546590
if (should_abort)
547591
abort_test();

tests/clar.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
enum cl_test_status {
1313
CL_TEST_OK,
1414
CL_TEST_FAILURE,
15-
CL_TEST_SKIP
15+
CL_TEST_SKIP,
16+
CL_TEST_NOTRUN,
1617
};
1718

1819
void clar_test_init(int argc, char *argv[]);

tests/clar/print.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ static void clar_print_shutdown(int test_count, int suite_count, int error_count
1313
(void)error_count;
1414

1515
printf("\n\n");
16-
clar_report_errors();
16+
clar_report_all();
1717
}
1818

19-
static void clar_print_error(int num, const struct clar_error *error)
19+
static void clar_print_error(int num, const struct clar_report *report, const struct clar_error *error)
2020
{
2121
printf(" %d) Failure:\n", num);
2222

2323
printf("%s::%s [%s:%d]\n",
24-
error->suite,
25-
error->test,
24+
report->suite,
25+
report->test,
2626
error->file,
2727
error->line_number);
2828

@@ -44,6 +44,7 @@ static void clar_print_ontest(const char *test_name, int test_number, enum cl_te
4444
case CL_TEST_OK: printf("."); break;
4545
case CL_TEST_FAILURE: printf("F"); break;
4646
case CL_TEST_SKIP: printf("S"); break;
47+
case CL_TEST_NOTRUN: printf("N"); break;
4748
}
4849

4950
fflush(stdout);

0 commit comments

Comments
 (0)