4
* Copyright (C) 2006 Robert Collins <robertc@robertcollins.net>
6
* Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
7
* license at the users choice. A copy of both licenses are available in the
8
* project source as Apache-2.0 and BSD. You may not use this file except in
9
* compliance with one of these two licences.
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under these licenses is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the license you chose for the specific language governing permissions
15
* and limitations under that license.
23
#include "subunit/child.h"
26
* Helper function to capture stdout, run some call, and check what
28
* @expected the expected stdout content
29
* @function the function to call.
32
test_stdout_function(char const * expected,
33
void (*function)(void))
35
/* test that the start function emits a correct test: line. */
40
/* we need a socketpair to capture stdout in */
41
fail_if(pipe(new_stdout), "Failed to create a socketpair.");
42
/* backup stdout so we can replace it */
44
if (old_stdout == -1) {
47
fail("Failed to backup stdout before replacing.");
49
/* redirect stdout so we can analyse it */
50
if (dup2(new_stdout[1], 1) != 1) {
54
fail("Failed to redirect stdout");
56
/* yes this can block. Its a test case with < 100 bytes of output.
60
/* restore stdout now */
61
if (dup2(old_stdout, 1) != 1) {
65
fail("Failed to restore stdout");
67
/* and we dont need the write side any more */
68
if (close(new_stdout[1])) {
70
fail("Failed to close write side of socketpair.");
73
bytecount = read(new_stdout[0], buffer, 100);
76
fail("Failed to read captured output.");
78
buffer[bytecount]='\0';
79
/* and we dont need the read side any more */
80
fail_if(close(new_stdout[0]), "Failed to close write side of socketpair.");
81
/* compare with expected outcome */
82
fail_if(strcmp(expected, buffer), "Did not get expected output [%s], got [%s]", expected, buffer);
89
subunit_test_start("test case");
93
START_TEST (test_start)
95
test_stdout_function("test: test case\n", call_test_start);
103
subunit_test_pass("test case");
107
START_TEST (test_pass)
109
test_stdout_function("success: test case\n", call_test_pass);
117
subunit_test_fail("test case", "Multiple lines\n of error\n");
121
START_TEST (test_fail)
123
test_stdout_function("failure: test case [\n"
133
call_test_error(void)
135
subunit_test_error("test case", "Multiple lines\n of output\n");
139
START_TEST (test_error)
141
test_stdout_function("error: test case [\n"
153
subunit_test_skip("test case", "Multiple lines\n of output\n");
157
START_TEST (test_skip)
159
test_stdout_function("skip: test case [\n"
169
call_test_progress_pop(void)
171
subunit_progress(SUBUNIT_PROGRESS_POP, 0);
175
call_test_progress_set(void)
177
subunit_progress(SUBUNIT_PROGRESS_SET, 5);
181
call_test_progress_push(void)
183
subunit_progress(SUBUNIT_PROGRESS_PUSH, 0);
187
call_test_progress_cur(void)
189
subunit_progress(SUBUNIT_PROGRESS_CUR, -6);
192
START_TEST (test_progress)
194
test_stdout_function("progress: pop\n",
195
call_test_progress_pop);
196
test_stdout_function("progress: push\n",
197
call_test_progress_push);
198
test_stdout_function("progress: 5\n",
199
call_test_progress_set);
200
test_stdout_function("progress: -6\n",
201
call_test_progress_cur);
208
Suite *s = suite_create("subunit_child");
209
TCase *tc_core = tcase_create("Core");
210
suite_add_tcase (s, tc_core);
211
tcase_add_test (tc_core, test_start);
212
tcase_add_test (tc_core, test_pass);
213
tcase_add_test (tc_core, test_fail);
214
tcase_add_test (tc_core, test_error);
215
tcase_add_test (tc_core, test_skip);
216
tcase_add_test (tc_core, test_progress);
225
Suite *s = child_suite();
226
SRunner *sr = srunner_create(s);
227
srunner_run_all(sr, CK_NORMAL);
228
nf = srunner_ntests_failed(sr);
230
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;