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