]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/atf/atf-c++/detail/test_helpers.hpp
Update atf to 0.18 and remove the code of the deprecated tools.
[FreeBSD/stable/10.git] / contrib / atf / atf-c++ / detail / test_helpers.hpp
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2009 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 #if defined(TESTS_ATF_ATF_CXX_TEST_HELPERS_H)
31 #   error "Cannot include test_helpers.hpp more than once."
32 #else
33 #   define TESTS_ATF_ATF_CXX_TEST_HELPERS_H
34 #endif
35
36 #include <cstdlib>
37 #include <iostream>
38 #include <sstream>
39 #include <utility>
40
41 #include "../macros.hpp"
42 #include "../tests.hpp"
43 #include "parser.hpp"
44 #include "process.hpp"
45 #include "text.hpp"
46
47 #define HEADER_TC(name, hdrname) \
48     ATF_TEST_CASE(name); \
49     ATF_TEST_CASE_HEAD(name) \
50     { \
51         set_md_var("descr", "Tests that the " hdrname " file can be " \
52             "included on its own, without any prerequisites"); \
53     } \
54     ATF_TEST_CASE_BODY(name) \
55     { \
56         header_check(hdrname); \
57     }
58
59 #define BUILD_TC(name, sfile, descr, failmsg) \
60     ATF_TEST_CASE(name); \
61     ATF_TEST_CASE_HEAD(name) \
62     { \
63         set_md_var("descr", descr); \
64     } \
65     ATF_TEST_CASE_BODY(name) \
66     { \
67         build_check_cxx_o(*this, sfile, failmsg, true);      \
68     }
69
70 #define BUILD_TC_FAIL(name, sfile, descr, failmsg) \
71     ATF_TEST_CASE(name); \
72     ATF_TEST_CASE_HEAD(name) \
73     { \
74         set_md_var("descr", descr); \
75     } \
76     ATF_TEST_CASE_BODY(name) \
77     { \
78         build_check_cxx_o(*this, sfile, failmsg, false);      \
79     }
80
81 namespace atf {
82 namespace tests {
83 class tc;
84 }
85 }
86
87 void header_check(const char*);
88 void build_check_cxx_o(const atf::tests::tc&, const char*, const char*, bool);
89 atf::fs::path get_process_helpers_path(const atf::tests::tc&, bool);
90
91 struct run_h_tc_data {
92     const atf::tests::vars_map& m_config;
93
94     run_h_tc_data(const atf::tests::vars_map& config) :
95         m_config(config) {}
96 };
97
98 template< class TestCase >
99 void
100 run_h_tc_child(void* v)
101 {
102     run_h_tc_data* data = static_cast< run_h_tc_data* >(v);
103
104     TestCase tc;
105     tc.init(data->m_config);
106     tc.run("result");
107     std::exit(EXIT_SUCCESS);
108 }
109
110 template< class TestCase >
111 void
112 run_h_tc(atf::tests::vars_map config = atf::tests::vars_map())
113 {
114     run_h_tc_data data(config);
115     atf::process::child c = atf::process::fork(
116         run_h_tc_child< TestCase >,
117         atf::process::stream_redirect_path(atf::fs::path("stdout")),
118         atf::process::stream_redirect_path(atf::fs::path("stderr")),
119         &data);
120     const atf::process::status s = c.wait();
121     ATF_REQUIRE(s.exited());
122 }
123
124 namespace test_helpers_detail {
125
126 typedef std::vector< std::string > string_vector;
127
128 template< class Reader >
129 std::pair< string_vector, string_vector >
130 do_read(const char* input)
131 {
132     string_vector errors;
133
134     std::istringstream is(input);
135     Reader reader(is);
136     try {
137         reader.read();
138     } catch (const atf::parser::parse_errors& pes) {
139         for (std::vector< atf::parser::parse_error >::const_iterator iter =
140              pes.begin(); iter != pes.end(); iter++)
141             errors.push_back(*iter);
142     } catch (const atf::parser::parse_error& pe) {
143         ATF_FAIL("Raised a lonely parse error: " +
144                  atf::text::to_string(pe.first) + ": " + pe.second);
145     }
146
147     return std::make_pair(reader.m_calls, errors);
148 }
149
150 void check_equal(const char*[], const string_vector&);
151
152 } // namespace test_helpers_detail
153
154 template< class Reader >
155 void
156 do_parser_test(const char* input, const char* exp_calls[],
157                const char* exp_errors[])
158 {
159     const std::pair< test_helpers_detail::string_vector,
160                      test_helpers_detail::string_vector >
161         actual = test_helpers_detail::do_read< Reader >(input);
162     test_helpers_detail::check_equal(exp_calls, actual.first);
163     test_helpers_detail::check_equal(exp_errors, actual.second);
164 }