test_helpers.hpp revision 240120
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 81namespace atf { 82namespace tests { 83class tc; 84} 85} 86 87void header_check(const char*); 88void build_check_cxx_o(const atf::tests::tc&, const char*, const char*, bool); 89atf::fs::path get_process_helpers_path(const atf::tests::tc&); 90bool grep_file(const char*, const char*); 91bool grep_string(const std::string&, const char*); 92 93struct run_h_tc_data { 94 const atf::tests::vars_map& m_config; 95 96 run_h_tc_data(const atf::tests::vars_map& config) : 97 m_config(config) {} 98}; 99 100template< class TestCase > 101void 102run_h_tc_child(void* v) 103{ 104 run_h_tc_data* data = static_cast< run_h_tc_data* >(v); 105 106 TestCase tc; 107 tc.init(data->m_config); 108 tc.run("result"); 109 std::exit(EXIT_SUCCESS); 110} 111 112template< class TestCase > 113void 114run_h_tc(atf::tests::vars_map config = atf::tests::vars_map()) 115{ 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")), 121 &data); 122 const atf::process::status s = c.wait(); 123 ATF_REQUIRE(s.exited()); 124} 125 126namespace test_helpers_detail { 127 128typedef std::vector< std::string > string_vector; 129 130template< class Reader > 131std::pair< string_vector, string_vector > 132do_read(const char* input) 133{ 134 string_vector errors; 135 136 std::istringstream is(input); 137 Reader reader(is); 138 try { 139 reader.read(); 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); 147 } 148 149 return std::make_pair(reader.m_calls, errors); 150} 151 152void check_equal(const char*[], const string_vector&); 153 154} // namespace test_helpers_detail 155 156template< class Reader > 157void 158do_parser_test(const char* input, const char* exp_calls[], 159 const char* exp_errors[]) 160{ 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); 166} 167