2 // Automated Testing Framework (atf)
4 // Copyright (c) 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
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.
30 #if defined(TESTS_ATF_ATF_CXX_TEST_HELPERS_H)
31 # error "Cannot include test_helpers.hpp more than once."
33 # define TESTS_ATF_ATF_CXX_TEST_HELPERS_H
41 #include "../macros.hpp"
42 #include "../tests.hpp"
44 #include "process.hpp"
47 #define HEADER_TC(name, hdrname) \
48 ATF_TEST_CASE(name); \
49 ATF_TEST_CASE_HEAD(name) \
51 set_md_var("descr", "Tests that the " hdrname " file can be " \
52 "included on its own, without any prerequisites"); \
54 ATF_TEST_CASE_BODY(name) \
56 header_check(hdrname); \
59 #define BUILD_TC(name, sfile, descr, failmsg) \
60 ATF_TEST_CASE(name); \
61 ATF_TEST_CASE_HEAD(name) \
63 set_md_var("descr", descr); \
65 ATF_TEST_CASE_BODY(name) \
67 build_check_cxx_o(*this, sfile, failmsg, true); \
70 #define BUILD_TC_FAIL(name, sfile, descr, failmsg) \
71 ATF_TEST_CASE(name); \
72 ATF_TEST_CASE_HEAD(name) \
74 set_md_var("descr", descr); \
76 ATF_TEST_CASE_BODY(name) \
78 build_check_cxx_o(*this, sfile, failmsg, false); \
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 bool grep_file(const char*, const char*);
91 bool grep_string(const std::string&, const char*);
93 struct run_h_tc_data {
94 const atf::tests::vars_map& m_config;
96 run_h_tc_data(const atf::tests::vars_map& config) :
100 template< class TestCase >
102 run_h_tc_child(void* v)
104 run_h_tc_data* data = static_cast< run_h_tc_data* >(v);
107 tc.init(data->m_config);
109 std::exit(EXIT_SUCCESS);
112 template< class TestCase >
114 run_h_tc(atf::tests::vars_map config = atf::tests::vars_map())
116 run_h_tc_data data(config);
117 atf::process::child c = atf::process::fork(
118 run_h_tc_child< TestCase >,
119 atf::process::stream_redirect_path(atf::fs::path("stdout")),
120 atf::process::stream_redirect_path(atf::fs::path("stderr")),
122 const atf::process::status s = c.wait();
123 ATF_REQUIRE(s.exited());
126 namespace test_helpers_detail {
128 typedef std::vector< std::string > string_vector;
130 template< class Reader >
131 std::pair< string_vector, string_vector >
132 do_read(const char* input)
134 string_vector errors;
136 std::istringstream is(input);
140 } catch (const atf::parser::parse_errors& pes) {
141 for (std::vector< atf::parser::parse_error >::const_iterator iter =
142 pes.begin(); iter != pes.end(); iter++)
143 errors.push_back(*iter);
144 } catch (const atf::parser::parse_error& pe) {
145 ATF_FAIL("Raised a lonely parse error: " +
146 atf::text::to_string(pe.first) + ": " + pe.second);
149 return std::make_pair(reader.m_calls, errors);
152 void check_equal(const char*[], const string_vector&);
154 } // namespace test_helpers_detail
156 template< class Reader >
158 do_parser_test(const char* input, const char* exp_calls[],
159 const char* exp_errors[])
161 const std::pair< test_helpers_detail::string_vector,
162 test_helpers_detail::string_vector >
163 actual = test_helpers_detail::do_read< Reader >(input);
164 test_helpers_detail::check_equal(exp_calls, actual.first);
165 test_helpers_detail::check_equal(exp_errors, actual.second);