tests_test.cpp revision 262855
1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2007 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
30extern "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 "macros.hpp"
42
43#include "detail/test_helpers.hpp"
44#include "detail/text.hpp"
45
46// ------------------------------------------------------------------------
47// Tests for the "atf_tp_writer" class.
48// ------------------------------------------------------------------------
49
50static
51void
52print_indented(const std::string& str)
53{
54    std::vector< std::string > ws = atf::text::split(str, "\n");
55    for (std::vector< std::string >::const_iterator iter = ws.begin();
56         iter != ws.end(); iter++)
57        std::cout << ">>" << *iter << "<<\n";
58}
59
60// XXX Should this string handling and verbosity level be part of the
61// ATF_REQUIRE_EQ macro?  It may be hard to predict sometimes that a
62// string can have newlines in it, and so the error message generated
63// at the moment will be bogus if there are some.
64static
65void
66check_equal(const atf::tests::tc& tc, const std::string& str,
67            const std::string& exp)
68{
69    if (str != exp) {
70        std::cout << "String equality check failed.\n"
71            "Adding >> and << to delimit the string boundaries below.\n";
72        std::cout << "GOT:\n";
73        print_indented(str);
74        std::cout << "EXPECTED:\n";
75        print_indented(exp);
76        tc.fail("Constructed string differs from the expected one");
77    }
78}
79
80ATF_TEST_CASE(atf_tp_writer);
81ATF_TEST_CASE_HEAD(atf_tp_writer)
82{
83    set_md_var("descr", "Verifies the application/X-atf-tp writer");
84}
85ATF_TEST_CASE_BODY(atf_tp_writer)
86{
87    std::ostringstream expss;
88    std::ostringstream ss;
89
90#define RESET \
91    expss.str(""); \
92    ss.str("")
93
94#define CHECK \
95    check_equal(*this, ss.str(), expss.str())
96
97    {
98        RESET;
99
100        atf::tests::detail::atf_tp_writer w(ss);
101        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
102        CHECK;
103    }
104
105    {
106        RESET;
107
108        atf::tests::detail::atf_tp_writer w(ss);
109        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
110        CHECK;
111
112        w.start_tc("test1");
113        expss << "ident: test1\n";
114        CHECK;
115
116        w.end_tc();
117        CHECK;
118    }
119
120    {
121        RESET;
122
123        atf::tests::detail::atf_tp_writer w(ss);
124        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
125        CHECK;
126
127        w.start_tc("test1");
128        expss << "ident: test1\n";
129        CHECK;
130
131        w.end_tc();
132        CHECK;
133
134        w.start_tc("test2");
135        expss << "\nident: test2\n";
136        CHECK;
137
138        w.end_tc();
139        CHECK;
140    }
141
142    {
143        RESET;
144
145        atf::tests::detail::atf_tp_writer w(ss);
146        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
147        CHECK;
148
149        w.start_tc("test1");
150        expss << "ident: test1\n";
151        CHECK;
152
153        w.tc_meta_data("descr", "the description");
154        expss << "descr: the description\n";
155        CHECK;
156
157        w.end_tc();
158        CHECK;
159
160        w.start_tc("test2");
161        expss << "\nident: test2\n";
162        CHECK;
163
164        w.tc_meta_data("descr", "second test case");
165        expss << "descr: second test case\n";
166        CHECK;
167
168        w.tc_meta_data("require.progs", "/bin/cp");
169        expss << "require.progs: /bin/cp\n";
170        CHECK;
171
172        w.tc_meta_data("X-custom", "foo bar baz");
173        expss << "X-custom: foo bar baz\n";
174        CHECK;
175
176        w.end_tc();
177        CHECK;
178    }
179
180#undef CHECK
181#undef RESET
182}
183
184// ------------------------------------------------------------------------
185// Tests cases for the header file.
186// ------------------------------------------------------------------------
187
188HEADER_TC(include, "atf-c++/tests.hpp");
189
190// ------------------------------------------------------------------------
191// Main.
192// ------------------------------------------------------------------------
193
194ATF_INIT_TEST_CASES(tcs)
195{
196    // Add tests for the "atf_tp_writer" class.
197    ATF_ADD_TEST_CASE(tcs, atf_tp_writer);
198
199    // Add the test cases for the header file.
200    ATF_ADD_TEST_CASE(tcs, include);
201}
202