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
25
#include "subunit/child.h"
27
START_TEST (test_start)
29
/* test that the start function emits a correct test: line. */
34
/* we need a socketpair to capture stdout in */
35
fail_if(pipe(new_stdout), "Failed to create a socketpair.");
36
/* backup stdout so we can replace it */
38
if (old_stdout == -1) {
41
fail("Failed to backup stdout before replacing.");
43
/* redirect stdout so we can analyse it */
44
if (dup2(new_stdout[1], 1) != 1) {
48
fail("Failed to redirect stdout");
50
/* yes this can block. Its a test case with < 100 bytes of output.
53
subunit_test_start("test case");
54
/* restore stdout now */
55
if (dup2(old_stdout, 1) != 1) {
59
fail("Failed to restore stdout");
61
/* and we dont need the write side any more */
62
if (close(new_stdout[1])) {
64
fail("Failed to close write side of socketpair.");
67
bytecount = read(new_stdout[0], buffer, 100);
70
fail("Failed to read captured output.");
72
buffer[bytecount]='\0';
73
/* and we dont need the read side any more */
74
fail_if(close(new_stdout[0]), "Failed to close write side of socketpair.");
75
/* compare with expected outcome */
76
#define EXPECTED "test: test case\n"
77
fail_if(strcmp(EXPECTED, buffer), "Did not get expected output [%s], got [%s]", EXPECTED, buffer);
83
Suite *child_suite(void)
85
Suite *s = suite_create("subunit_child");
86
TCase *tc_core = tcase_create("Core");
87
suite_add_tcase (s, tc_core);
88
tcase_add_test (tc_core, test_start);
96
Suite *s = child_suite();
97
SRunner *sr = srunner_create(s);
98
srunner_run_all(sr, CK_NORMAL);
99
nf = srunner_ntests_failed(sr);
101
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;