xref: /netbsd-src/external/bsd/atf/dist/atf-c++/tests_test.cpp (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008, 2009, 2010 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 //    notice, this list of conditions and the following disclaimer in the
14 //    documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 extern "C" {
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 
34 #include <fcntl.h>
35 #include <unistd.h>
36 }
37 
38 #include <fstream>
39 #include <sstream>
40 
41 #include <atf-c++.hpp>
42 
43 #include "atf-c++/parser.hpp"
44 #include "atf-c++/user.hpp"
45 
46 #include "test_helpers.hpp"
47 
48 // ------------------------------------------------------------------------
49 // Tests for the "atf_tp_writer" class.
50 // ------------------------------------------------------------------------
51 
52 static
53 void
54 print_indented(const std::string& str)
55 {
56     std::vector< std::string > ws = atf::text::split(str, "\n");
57     for (std::vector< std::string >::const_iterator iter = ws.begin();
58          iter != ws.end(); iter++)
59         std::cout << ">>" << *iter << "<<" << std::endl;
60 }
61 
62 // XXX Should this string handling and verbosity level be part of the
63 // ATF_CHECK_EQUAL macro?  It may be hard to predict sometimes that a
64 // string can have newlines in it, and so the error message generated
65 // at the moment will be bogus if there are some.
66 static
67 void
68 check_equal(const std::string& str, const std::string& exp)
69 {
70     if (str != exp) {
71         std::cout << "String equality check failed." << std::endl
72                   << "Adding >> and << to delimit the string boundaries "
73                      "below." << std::endl;
74         std::cout << "GOT:" << std::endl;
75         print_indented(str);
76         std::cout << "EXPECTED:" << std::endl;
77         print_indented(exp);
78         atf_tc_fail("Constructed string differs from the expected one");
79     }
80 }
81 
82 ATF_TEST_CASE(atf_tp_writer);
83 ATF_TEST_CASE_HEAD(atf_tp_writer)
84 {
85     set_md_var("descr", "Verifies the application/X-atf-tp writer");
86 }
87 ATF_TEST_CASE_BODY(atf_tp_writer)
88 {
89     std::ostringstream expss;
90     std::ostringstream ss;
91 
92 #define RESET \
93     expss.str(""); \
94     ss.str("")
95 
96 #define CHECK \
97     check_equal(ss.str(), expss.str())
98 
99     {
100         RESET;
101 
102         atf::tests::detail::atf_tp_writer w(ss);
103         expss << "Content-Type: application/X-atf-tp; version=\"1\""
104               << std::endl << std::endl;
105         CHECK;
106     }
107 
108     {
109         RESET;
110 
111         atf::tests::detail::atf_tp_writer w(ss);
112         expss << "Content-Type: application/X-atf-tp; version=\"1\""
113               << std::endl << std::endl;
114         CHECK;
115 
116         w.start_tc("test1");
117         expss << "ident: test1" << std::endl;
118         CHECK;
119 
120         w.end_tc();
121         CHECK;
122     }
123 
124     {
125         RESET;
126 
127         atf::tests::detail::atf_tp_writer w(ss);
128         expss << "Content-Type: application/X-atf-tp; version=\"1\""
129               << std::endl << std::endl;
130         CHECK;
131 
132         w.start_tc("test1");
133         expss << "ident: test1" << std::endl;
134         CHECK;
135 
136         w.end_tc();
137         CHECK;
138 
139         w.start_tc("test2");
140         expss << std::endl << "ident: test2" << std::endl;
141         CHECK;
142 
143         w.end_tc();
144         CHECK;
145     }
146 
147     {
148         RESET;
149 
150         atf::tests::detail::atf_tp_writer w(ss);
151         expss << "Content-Type: application/X-atf-tp; version=\"1\""
152               << std::endl << std::endl;
153         CHECK;
154 
155         w.start_tc("test1");
156         expss << "ident: test1" << std::endl;
157         CHECK;
158 
159         w.tc_meta_data("descr", "the description");
160         expss << "descr: the description" << std::endl;
161         CHECK;
162 
163         w.end_tc();
164         CHECK;
165 
166         w.start_tc("test2");
167         expss << std::endl << "ident: test2" << std::endl;
168         CHECK;
169 
170         w.tc_meta_data("descr", "second test case");
171         expss << "descr: second test case" << std::endl;
172         CHECK;
173 
174         w.tc_meta_data("require.progs", "/bin/cp");
175         expss << "require.progs: /bin/cp" << std::endl;
176         CHECK;
177 
178         w.tc_meta_data("X-custom", "foo bar baz");
179         expss << "X-custom: foo bar baz" << std::endl;
180         CHECK;
181 
182         w.end_tc();
183         CHECK;
184     }
185 
186 #undef CHECK
187 #undef RESET
188 }
189 
190 // ------------------------------------------------------------------------
191 // Tests cases for the header file.
192 // ------------------------------------------------------------------------
193 
194 HEADER_TC(include, "atf-c++/tests.hpp");
195 
196 // ------------------------------------------------------------------------
197 // Main.
198 // ------------------------------------------------------------------------
199 
200 ATF_INIT_TEST_CASES(tcs)
201 {
202     // Add tests for the "atf_tp_writer" class.
203     ATF_ADD_TEST_CASE(tcs, atf_tp_writer);
204 
205     // Add the test cases for the header file.
206     ATF_ADD_TEST_CASE(tcs, include);
207 }
208