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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/**
*
* subunit C bindings.
* Copyright (C) 2006 Robert Collins <robertc@robertcollins.net>
*
* Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
* license at the users choice. A copy of both licenses are available in the
* project source as Apache-2.0 and BSD. You may not use this file except in
* compliance with one of these two licences.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under these licenses is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license you chose for the specific language governing permissions
* and limitations under that license.
**/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <check.h>
#include "subunit/child.h"
/**
* Helper function to capture stdout, run some call, and check what
* was written.
* @expected the expected stdout content
* @function the function to call.
**/
static void
test_stdout_function(char const * expected,
void (*function)(void))
{
/* test that the start function emits a correct test: line. */
int bytecount;
int old_stdout;
int new_stdout[2];
char buffer[100];
/* we need a socketpair to capture stdout in */
fail_if(pipe(new_stdout), "Failed to create a socketpair.");
/* backup stdout so we can replace it */
old_stdout = dup(1);
if (old_stdout == -1) {
close(new_stdout[0]);
close(new_stdout[1]);
fail("Failed to backup stdout before replacing.");
}
/* redirect stdout so we can analyse it */
if (dup2(new_stdout[1], 1) != 1) {
close(old_stdout);
close(new_stdout[0]);
close(new_stdout[1]);
fail("Failed to redirect stdout");
}
/* yes this can block. Its a test case with < 100 bytes of output.
* DEAL.
*/
function();
/* restore stdout now */
if (dup2(old_stdout, 1) != 1) {
close(old_stdout);
close(new_stdout[0]);
close(new_stdout[1]);
fail("Failed to restore stdout");
}
/* and we dont need the write side any more */
if (close(new_stdout[1])) {
close(new_stdout[0]);
fail("Failed to close write side of socketpair.");
}
/* get the output */
bytecount = read(new_stdout[0], buffer, 100);
if (0 > bytecount) {
close(new_stdout[0]);
fail("Failed to read captured output.");
}
buffer[bytecount]='\0';
/* and we dont need the read side any more */
fail_if(close(new_stdout[0]), "Failed to close write side of socketpair.");
/* compare with expected outcome */
fail_if(strcmp(expected, buffer), "Did not get expected output [%s], got [%s]", expected, buffer);
}
static void
call_test_start(void)
{
subunit_test_start("test case");
}
START_TEST (test_start)
{
test_stdout_function("test: test case\n", call_test_start);
}
END_TEST
static void
call_test_pass(void)
{
subunit_test_pass("test case");
}
START_TEST (test_pass)
{
test_stdout_function("success: test case\n", call_test_pass);
}
END_TEST
static void
call_test_fail(void)
{
subunit_test_fail("test case", "Multiple lines\n of error\n");
}
START_TEST (test_fail)
{
test_stdout_function("failure: test case [\n"
"Multiple lines\n"
" of error\n"
"]\n",
call_test_fail);
}
END_TEST
static void
call_test_error(void)
{
subunit_test_error("test case", "Multiple lines\n of output\n");
}
START_TEST (test_error)
{
test_stdout_function("error: test case [\n"
"Multiple lines\n"
" of output\n"
"]\n",
call_test_error);
}
END_TEST
static void
call_test_skip(void)
{
subunit_test_skip("test case", "Multiple lines\n of output\n");
}
START_TEST (test_skip)
{
test_stdout_function("skip: test case [\n"
"Multiple lines\n"
" of output\n"
"]\n",
call_test_skip);
}
END_TEST
static void
call_test_progress_pop(void)
{
subunit_progress(SUBUNIT_PROGRESS_POP, 0);
}
static void
call_test_progress_set(void)
{
subunit_progress(SUBUNIT_PROGRESS_SET, 5);
}
static void
call_test_progress_push(void)
{
subunit_progress(SUBUNIT_PROGRESS_PUSH, 0);
}
static void
call_test_progress_cur(void)
{
subunit_progress(SUBUNIT_PROGRESS_CUR, -6);
}
START_TEST (test_progress)
{
test_stdout_function("progress: pop\n",
call_test_progress_pop);
test_stdout_function("progress: push\n",
call_test_progress_push);
test_stdout_function("progress: 5\n",
call_test_progress_set);
test_stdout_function("progress: -6\n",
call_test_progress_cur);
}
END_TEST
static Suite *
child_suite(void)
{
Suite *s = suite_create("subunit_child");
TCase *tc_core = tcase_create("Core");
suite_add_tcase (s, tc_core);
tcase_add_test (tc_core, test_start);
tcase_add_test (tc_core, test_pass);
tcase_add_test (tc_core, test_fail);
tcase_add_test (tc_core, test_error);
tcase_add_test (tc_core, test_skip);
tcase_add_test (tc_core, test_progress);
return s;
}
int
main(void)
{
int nf;
Suite *s = child_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|