tests_test.cpp revision 262855
1235783Skib//
2235783Skib// Automated Testing Framework (atf)
3235783Skib//
4235783Skib// Copyright (c) 2007 The NetBSD Foundation, Inc.
5235783Skib// All rights reserved.
6235783Skib//
7235783Skib// Redistribution and use in source and binary forms, with or without
8235783Skib// modification, are permitted provided that the following conditions
9235783Skib// are met:
10235783Skib// 1. Redistributions of source code must retain the above copyright
11235783Skib//    notice, this list of conditions and the following disclaimer.
12235783Skib// 2. Redistributions in binary form must reproduce the above copyright
13235783Skib//    notice, this list of conditions and the following disclaimer in the
14235783Skib//    documentation and/or other materials provided with the distribution.
15235783Skib//
16235783Skib// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17235783Skib// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18235783Skib// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19235783Skib// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20235783Skib// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21235783Skib// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22235783Skib// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23235783Skib// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24235783Skib// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25235783Skib// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26235783Skib// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27235783Skib// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28235783Skib//
29235783Skib
30235783Skibextern "C" {
31235783Skib#include <sys/types.h>
32235783Skib#include <sys/stat.h>
33235783Skib
34235783Skib#include <fcntl.h>
35235783Skib#include <unistd.h>
36235783Skib}
37235783Skib
38235783Skib#include <fstream>
39235783Skib#include <sstream>
40235783Skib
41235783Skib#include "macros.hpp"
42235783Skib
43235783Skib#include "detail/test_helpers.hpp"
44235783Skib#include "detail/text.hpp"
45235783Skib
46235783Skib// ------------------------------------------------------------------------
47280369Skib// Tests for the "atf_tp_writer" class.
48235783Skib// ------------------------------------------------------------------------
49235783Skib
50235783Skibstatic
51235783Skibvoid
52235783Skibprint_indented(const std::string& str)
53235783Skib{
54235783Skib    std::vector< std::string > ws = atf::text::split(str, "\n");
55235783Skib    for (std::vector< std::string >::const_iterator iter = ws.begin();
56235783Skib         iter != ws.end(); iter++)
57235783Skib        std::cout << ">>" << *iter << "<<\n";
58235783Skib}
59235783Skib
60235783Skib// XXX Should this string handling and verbosity level be part of the
61235783Skib// ATF_REQUIRE_EQ macro?  It may be hard to predict sometimes that a
62235783Skib// string can have newlines in it, and so the error message generated
63235783Skib// at the moment will be bogus if there are some.
64235783Skibstatic
65235783Skibvoid
66235783Skibcheck_equal(const atf::tests::tc& tc, const std::string& str,
67235783Skib            const std::string& exp)
68235783Skib{
69235783Skib    if (str != exp) {
70235783Skib        std::cout << "String equality check failed.\n"
71235783Skib            "Adding >> and << to delimit the string boundaries below.\n";
72235783Skib        std::cout << "GOT:\n";
73235783Skib        print_indented(str);
74235783Skib        std::cout << "EXPECTED:\n";
75235783Skib        print_indented(exp);
76235783Skib        tc.fail("Constructed string differs from the expected one");
77235783Skib    }
78235783Skib}
79235783Skib
80280369SkibATF_TEST_CASE(atf_tp_writer);
81235783SkibATF_TEST_CASE_HEAD(atf_tp_writer)
82235783Skib{
83235783Skib    set_md_var("descr", "Verifies the application/X-atf-tp writer");
84235783Skib}
85235783SkibATF_TEST_CASE_BODY(atf_tp_writer)
86235783Skib{
87235783Skib    std::ostringstream expss;
88235783Skib    std::ostringstream ss;
89235783Skib
90235783Skib#define RESET \
91235783Skib    expss.str(""); \
92235783Skib    ss.str("")
93235783Skib
94235783Skib#define CHECK \
95235783Skib    check_equal(*this, ss.str(), expss.str())
96235783Skib
97235783Skib    {
98235783Skib        RESET;
99235783Skib
100235783Skib        atf::tests::detail::atf_tp_writer w(ss);
101235783Skib        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
102235783Skib        CHECK;
103235783Skib    }
104235783Skib
105235783Skib    {
106235783Skib        RESET;
107235783Skib
108235783Skib        atf::tests::detail::atf_tp_writer w(ss);
109235783Skib        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
110235783Skib        CHECK;
111235783Skib
112235783Skib        w.start_tc("test1");
113235783Skib        expss << "ident: test1\n";
114235783Skib        CHECK;
115235783Skib
116235783Skib        w.end_tc();
117235783Skib        CHECK;
118235783Skib    }
119235783Skib
120280369Skib    {
121280369Skib        RESET;
122280369Skib
123235783Skib        atf::tests::detail::atf_tp_writer w(ss);
124235783Skib        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
125235783Skib        CHECK;
126235783Skib
127235783Skib        w.start_tc("test1");
128235783Skib        expss << "ident: test1\n";
129235783Skib        CHECK;
130235783Skib
131235783Skib        w.end_tc();
132235783Skib        CHECK;
133235783Skib
134235783Skib        w.start_tc("test2");
135235783Skib        expss << "\nident: test2\n";
136235783Skib        CHECK;
137235783Skib
138235783Skib        w.end_tc();
139235783Skib        CHECK;
140235783Skib    }
141235783Skib
142235783Skib    {
143235783Skib        RESET;
144235783Skib
145235783Skib        atf::tests::detail::atf_tp_writer w(ss);
146235783Skib        expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
147235783Skib        CHECK;
148235783Skib
149235783Skib        w.start_tc("test1");
150235783Skib        expss << "ident: test1\n";
151235783Skib        CHECK;
152235783Skib
153235783Skib        w.tc_meta_data("descr", "the description");
154235783Skib        expss << "descr: the description\n";
155235783Skib        CHECK;
156235783Skib
157235783Skib        w.end_tc();
158235783Skib        CHECK;
159235783Skib
160235783Skib        w.start_tc("test2");
161235783Skib        expss << "\nident: test2\n";
162235783Skib        CHECK;
163235783Skib
164235783Skib        w.tc_meta_data("descr", "second test case");
165235783Skib        expss << "descr: second test case\n";
166235783Skib        CHECK;
167235783Skib
168235783Skib        w.tc_meta_data("require.progs", "/bin/cp");
169235783Skib        expss << "require.progs: /bin/cp\n";
170235783Skib        CHECK;
171235783Skib
172235783Skib        w.tc_meta_data("X-custom", "foo bar baz");
173235783Skib        expss << "X-custom: foo bar baz\n";
174235783Skib        CHECK;
175235783Skib
176235783Skib        w.end_tc();
177235783Skib        CHECK;
178235783Skib    }
179235783Skib
180235783Skib#undef CHECK
181235783Skib#undef RESET
182235783Skib}
183235783Skib
184235783Skib// ------------------------------------------------------------------------
185235783Skib// Tests cases for the header file.
186235783Skib// ------------------------------------------------------------------------
187235783Skib
188235783SkibHEADER_TC(include, "atf-c++/tests.hpp");
189235783Skib
190235783Skib// ------------------------------------------------------------------------
191235783Skib// Main.
192235783Skib// ------------------------------------------------------------------------
193235783Skib
194235783SkibATF_INIT_TEST_CASES(tcs)
195235783Skib{
196235783Skib    // Add tests for the "atf_tp_writer" class.
197235783Skib    ATF_ADD_TEST_CASE(tcs, atf_tp_writer);
198235783Skib
199235783Skib    // Add the test cases for the header file.
200235783Skib    ATF_ADD_TEST_CASE(tcs, include);
201235783Skib}
202235783Skib