-
Notifications
You must be signed in to change notification settings - Fork 0
/
show-result.c
175 lines (151 loc) · 5.77 KB
/
show-result.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* JCUnit - a very simple unit testing framework for C
*
* Copyright (C) 2021-2022 Denis Korchagin <denis.korchagin.1995@gmail.com>
*
* This file is part of JCUnit
*
* For the full license information, please view the LICENSE
* file that was distributed with this source code.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation of version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include "headers/show-result.h"
#include "headers/child-process.h"
#include "headers/test-iterator.h"
#include "headers/string.h"
#include "headers/errors.h"
static const char * stringify_test_status(struct abstract_test_result * test_result, bool use_short_version);
static void print_error(struct abstract_test_result * test_result, FILE * output);
static void print_program_runner_error(struct program_runner_test_result * test_result, FILE * output);
void show_test_result_in_detail_mode(struct abstract_test_result * test_result, FILE * output)
{
assert(test_result != NULL);
const char * test_status = stringify_test_status(test_result, false);
if (test_status == NULL) {
jcunit_fatal_error("Can't resolve the status of test result!");
}
fprintf(output, " %10s %s\n", test_status, test_result->name->value);
if (
test_result->status == TEST_RESULT_STATUS_INCOMPLETE
|| test_result->status == TEST_RESULT_STATUS_PASS
|| test_result->status == TEST_RESULT_STATUS_SKIPPED
) {
return;
}
if (test_result->status == TEST_RESULT_STATUS_ERROR) {
print_error(test_result, output);
return;
}
if (test_result->status == TEST_RESULT_STATUS_FAILURE) {
fprintf(output, "--- Expected\n%s$\n", test_result->expected == NULL ? "" : test_result->expected->value);
fprintf(output, "+++ Actual\n%s$\n", test_result->actual->value);
return;
}
fprintf(output, "The unknown test status!\n");
}
void show_test_result_in_passthrough_mode(struct abstract_test_result * test_result, FILE * output)
{
assert(test_result != NULL);
const char * test_status = stringify_test_status(test_result, true);
if (test_status == NULL) {
jcunit_fatal_error("Can't resolve the status of test result!");
}
fprintf(output, "%s", test_status);
}
static const char * stringify_test_status(struct abstract_test_result * test_result, bool use_short_version)
{
switch (test_result->status) {
case TEST_RESULT_STATUS_PASS: return use_short_version ? "." : "PASS";
case TEST_RESULT_STATUS_FAILURE: return use_short_version ? "F" : "FAIL";
case TEST_RESULT_STATUS_INCOMPLETE: return use_short_version ? "I" : "INCOMPLETE";
case TEST_RESULT_STATUS_ERROR: return use_short_version ? "E" : "ERROR";
case TEST_RESULT_STATUS_SKIPPED: return use_short_version ? "S" : "SKIPPED";
}
return NULL;
}
void print_error(struct abstract_test_result * test_result, FILE * output)
{
switch (test_result->kind) {
case TEST_RESULT_KIND_PROGRAM_RUNNER:
print_program_runner_error((struct program_runner_test_result *)test_result, output);
break;
}
}
void print_program_runner_error(struct program_runner_test_result * test_result, FILE * output)
{
switch (test_result->error_code) {
case ERROR_CODE_NONE:
break;
case ERROR_CODE_FILE_NOT_FOUND:
fprintf(
output,
"The file \"%s\" was not found!\n",
test_result->executable->value
);
break;
case ERROR_CODE_NOT_EXECUTABLE:
fprintf(
output,
"The file \"%s\" is not executable!\n",
test_result->executable->value
);
break;
case ERROR_CODE_READ_CHILD_DATA:
fprintf(output, "Can't read the program data!\n");
break;
}
}
struct abstract_test_result * test_runner(
struct abstract_test * test,
struct tests_results * tests_results,
unsigned int current_index
) {
struct abstract_test_result * test_result = test_run(test);
add_test_result_to_test_suite_result(tests_results, test_result, current_index);
return test_result;
}
void show_error_test_result(FILE * output, struct abstract_test_result * test_result, unsigned int error_number)
{
if (test_result->kind != TEST_RESULT_KIND_PROGRAM_RUNNER) {
jcunit_fatal_error("An unknown test result!");
}
fprintf(
output,
"%u) %s : %s\n",
error_number,
test_result->test->test_suite->name,
test_result->name->value
);
print_error(test_result, output);
}
void show_failure_test_result(FILE * output, struct abstract_test_result * test_result, unsigned int failure_number)
{
if (test_result->kind != TEST_RESULT_KIND_PROGRAM_RUNNER) {
jcunit_fatal_error("An unknown test result!");
}
fprintf(
output,
"%u) %s : %s\n",
failure_number,
test_result->test->test_suite->name,
test_result->name->value
);
fprintf(output, "--- Expected\n%s$\n", test_result->expected == NULL ? "" : test_result->expected->value);
fprintf(output, "+++ Actual\n%s$\n", test_result->actual->value);
}