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(void)
171 subunit_progress(SUBUNIT_PROGRESS_POP, 0);
172 subunit_progress(SUBUNIT_PROGRESS_SET, 5);
173 subunit_progress(SUBUNIT_PROGRESS_PUSH, 0);
177 START_TEST (test_progress)
179 test_stdout_function("progress: pop\n"
189 Suite *s = suite_create("subunit_child");
190 TCase *tc_core = tcase_create("Core");
191 suite_add_tcase (s, tc_core);
192 tcase_add_test (tc_core, test_start);
193 tcase_add_test (tc_core, test_pass);
194 tcase_add_test (tc_core, test_fail);
195 tcase_add_test (tc_core, test_error);
196 tcase_add_test (tc_core, test_skip);
197 tcase_add_test (tc_core, test_progress);
206 Suite *s = child_suite();
207 SRunner *sr = srunner_create(s);
208 srunner_run_all(sr, CK_NORMAL);
209 nf = srunner_ntests_failed(sr);
211 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;