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.
24 #include "subunit/child.h"
27 * Helper function to capture stdout, run some call, and check what
29 * @expected the expected stdout content
30 * @function the function to call.
33 test_stdout_function(char const * expected,
34 void (*function)(void))
36 /* test that the start function emits a correct test: line. */
41 /* we need a socketpair to capture stdout in */
42 fail_if(pipe(new_stdout), "Failed to create a socketpair.");
43 /* backup stdout so we can replace it */
45 if (old_stdout == -1) {
48 fail("Failed to backup stdout before replacing.");
50 /* redirect stdout so we can analyse it */
51 if (dup2(new_stdout[1], 1) != 1) {
55 fail("Failed to redirect stdout");
57 /* yes this can block. Its a test case with < 100 bytes of output.
61 /* flush writes on FILE object to file descriptor */
63 /* restore stdout now */
64 if (dup2(old_stdout, 1) != 1) {
68 fail("Failed to restore stdout");
70 /* and we dont need the write side any more */
71 if (close(new_stdout[1])) {
73 fail("Failed to close write side of socketpair.");
76 bytecount = read(new_stdout[0], buffer, 100);
79 fail("Failed to read captured output.");
81 buffer[bytecount]='\0';
82 /* and we dont need the read side any more */
83 fail_if(close(new_stdout[0]), "Failed to close write side of socketpair.");
84 /* compare with expected outcome */
85 fail_if(strcmp(expected, buffer), "Did not get expected output [%s], got [%s]", expected, buffer);
92 subunit_test_start("test case");
96 START_TEST (test_start)
98 test_stdout_function("test: test case\n", call_test_start);
106 subunit_test_pass("test case");
110 START_TEST (test_pass)
112 test_stdout_function("success: test case\n", call_test_pass);
120 subunit_test_fail("test case", "Multiple lines\n of error\n");
124 START_TEST (test_fail)
126 test_stdout_function("failure: test case [\n"
136 call_test_error(void)
138 subunit_test_error("test case", "Multiple lines\n of output\n");
142 START_TEST (test_error)
144 test_stdout_function("error: test case [\n"
156 subunit_test_skip("test case", "Multiple lines\n of output\n");
160 START_TEST (test_skip)
162 test_stdout_function("skip: test case [\n"
172 call_test_progress_pop(void)
174 subunit_progress(SUBUNIT_PROGRESS_POP, 0);
178 call_test_progress_set(void)
180 subunit_progress(SUBUNIT_PROGRESS_SET, 5);
184 call_test_progress_push(void)
186 subunit_progress(SUBUNIT_PROGRESS_PUSH, 0);
190 call_test_progress_cur(void)
192 subunit_progress(SUBUNIT_PROGRESS_CUR, -6);
195 START_TEST (test_progress)
197 test_stdout_function("progress: pop\n",
198 call_test_progress_pop);
199 test_stdout_function("progress: push\n",
200 call_test_progress_push);
201 test_stdout_function("progress: 5\n",
202 call_test_progress_set);
203 test_stdout_function("progress: -6\n",
204 call_test_progress_cur);
211 Suite *s = suite_create("subunit_child");
212 TCase *tc_core = tcase_create("Core");
213 suite_add_tcase (s, tc_core);
214 tcase_add_test (tc_core, test_start);
215 tcase_add_test (tc_core, test_pass);
216 tcase_add_test (tc_core, test_fail);
217 tcase_add_test (tc_core, test_error);
218 tcase_add_test (tc_core, test_skip);
219 tcase_add_test (tc_core, test_progress);
228 Suite *s = child_suite();
229 SRunner *sr = srunner_create(s);
230 srunner_run_all(sr, CK_NORMAL);
231 nf = srunner_ntests_failed(sr);
233 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;