4 * Copyright (C) 2006 Robert Collins <robertc@robertcollins.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "subunit/child.h"
29 * Helper function to capture stdout, run some call, and check what
31 * @expected the expected stdout content
32 * @function the function to call.
35 test_stdout_function(char const * expected,
36 void (*function)(void))
38 /* test that the start function emits a correct test: line. */
43 /* we need a socketpair to capture stdout in */
44 fail_if(pipe(new_stdout), "Failed to create a socketpair.");
45 /* backup stdout so we can replace it */
47 if (old_stdout == -1) {
50 fail("Failed to backup stdout before replacing.");
52 /* redirect stdout so we can analyse it */
53 if (dup2(new_stdout[1], 1) != 1) {
57 fail("Failed to redirect stdout");
59 /* yes this can block. Its a test case with < 100 bytes of output.
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"
155 Suite *s = suite_create("subunit_child");
156 TCase *tc_core = tcase_create("Core");
157 suite_add_tcase (s, tc_core);
158 tcase_add_test (tc_core, test_start);
159 tcase_add_test (tc_core, test_pass);
160 tcase_add_test (tc_core, test_fail);
161 tcase_add_test (tc_core, test_error);
170 Suite *s = child_suite();
171 SRunner *sr = srunner_create(s);
172 srunner_run_all(sr, CK_NORMAL);
173 nf = srunner_ntests_failed(sr);
175 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;