xref: /freebsd/contrib/atf/atf-c++/detail/test_helpers.hpp (revision cd0d51baaa4509a1db83251a601d34404d20c990)
1 // Copyright (c) 2009 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 #if defined(ATF_CXX_DETAIL_TEST_HELPERS_H)
27 #   error "Cannot include test_helpers.hpp more than once."
28 #else
29 #   define ATF_CXX_DETAIL_TEST_HELPERS_H
30 #endif
31 
32 #include <cstdlib>
33 #include <iostream>
34 #include <sstream>
35 #include <utility>
36 
37 #include <atf-c++.hpp>
38 
39 #include <atf-c++/detail/process.hpp>
40 
41 #define HEADER_TC(name, hdrname) \
42     ATF_TEST_CASE(name); \
43     ATF_TEST_CASE_HEAD(name) \
44     { \
45         set_md_var("descr", "Tests that the " hdrname " file can be " \
46             "included on its own, without any prerequisites"); \
47     } \
48     ATF_TEST_CASE_BODY(name) \
49     { \
50         header_check(hdrname); \
51     }
52 
53 #define BUILD_TC(name, sfile, descr, failmsg) \
54     ATF_TEST_CASE(name); \
55     ATF_TEST_CASE_HEAD(name) \
56     { \
57         set_md_var("descr", descr); \
58     } \
59     ATF_TEST_CASE_BODY(name) \
60     { \
61         if (!build_check_cxx_o_srcdir(*this, sfile)) \
62             ATF_FAIL(failmsg); \
63     }
64 
65 namespace atf {
66 namespace tests {
67 class tc;
68 }
69 }
70 
71 void header_check(const char*);
72 bool build_check_cxx_o(const char*);
73 bool build_check_cxx_o_srcdir(const atf::tests::tc&, const char*);
74 atf::fs::path get_process_helpers_path(const atf::tests::tc&, bool);
75 
76 struct run_h_tc_data {
77     const atf::tests::vars_map& m_config;
78 
79     run_h_tc_data(const atf::tests::vars_map& config) :
80         m_config(config) {}
81 };
82 
83 template< class TestCase >
84 void
85 run_h_tc_child(void* v)
86 {
87     run_h_tc_data* data = static_cast< run_h_tc_data* >(v);
88 
89     TestCase tc;
90     tc.init(data->m_config);
91     tc.run("result");
92     std::exit(EXIT_SUCCESS);
93 }
94 
95 template< class TestCase >
96 void
97 run_h_tc(atf::tests::vars_map config = atf::tests::vars_map())
98 {
99     run_h_tc_data data(config);
100     atf::process::child c = atf::process::fork(
101         run_h_tc_child< TestCase >,
102         atf::process::stream_redirect_path(atf::fs::path("stdout")),
103         atf::process::stream_redirect_path(atf::fs::path("stderr")),
104         &data);
105     const atf::process::status s = c.wait();
106     ATF_REQUIRE(s.exited());
107 }
108