test_helpers.hpp revision 262855
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 "process.hpp" 44 45#define HEADER_TC(name, hdrname) \ 46 ATF_TEST_CASE(name); \ 47 ATF_TEST_CASE_HEAD(name) \ 48 { \ 49 set_md_var("descr", "Tests that the " hdrname " file can be " \ 50 "included on its own, without any prerequisites"); \ 51 } \ 52 ATF_TEST_CASE_BODY(name) \ 53 { \ 54 header_check(hdrname); \ 55 } 56 57#define BUILD_TC(name, sfile, descr, failmsg) \ 58 ATF_TEST_CASE(name); \ 59 ATF_TEST_CASE_HEAD(name) \ 60 { \ 61 set_md_var("descr", descr); \ 62 } \ 63 ATF_TEST_CASE_BODY(name) \ 64 { \ 65 if (!build_check_cxx_o_srcdir(*this, sfile)) \ 66 ATF_FAIL(failmsg); \ 67 } 68 69namespace atf { 70namespace tests { 71class tc; 72} 73} 74 75void header_check(const char*); 76bool build_check_cxx_o(const char*); 77bool build_check_cxx_o_srcdir(const atf::tests::tc&, const char*); 78atf::fs::path get_process_helpers_path(const atf::tests::tc&, bool); 79 80struct run_h_tc_data { 81 const atf::tests::vars_map& m_config; 82 83 run_h_tc_data(const atf::tests::vars_map& config) : 84 m_config(config) {} 85}; 86 87template< class TestCase > 88void 89run_h_tc_child(void* v) 90{ 91 run_h_tc_data* data = static_cast< run_h_tc_data* >(v); 92 93 TestCase tc; 94 tc.init(data->m_config); 95 tc.run("result"); 96 std::exit(EXIT_SUCCESS); 97} 98 99template< class TestCase > 100void 101run_h_tc(atf::tests::vars_map config = atf::tests::vars_map()) 102{ 103 run_h_tc_data data(config); 104 atf::process::child c = atf::process::fork( 105 run_h_tc_child< TestCase >, 106 atf::process::stream_redirect_path(atf::fs::path("stdout")), 107 atf::process::stream_redirect_path(atf::fs::path("stderr")), 108 &data); 109 const atf::process::status s = c.wait(); 110 ATF_REQUIRE(s.exited()); 111} 112