test_helpers.hpp revision 1.1
1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2009, 2010 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        set_md_var("use.fs", "true"); \
54    } \
55    ATF_TEST_CASE_BODY(name) \
56    { \
57        header_check(*this, hdrname); \
58    }
59
60#define BUILD_TC(name, sfile, descr, failmsg) \
61    ATF_TEST_CASE(name); \
62    ATF_TEST_CASE_HEAD(name) \
63    { \
64        set_md_var("descr", descr); \
65        set_md_var("use.fs", "true"); \
66    } \
67    ATF_TEST_CASE_BODY(name) \
68    { \
69        build_check_cxx_o(*this, sfile, failmsg); \
70    }
71
72namespace atf {
73namespace tests {
74class tc;
75}
76}
77
78void header_check(const atf::tests::tc&, const char*);
79void build_check_cxx_o(const atf::tests::tc&, const char*, const char*);
80atf::fs::path get_process_helpers_path(const atf::tests::tc&);
81bool grep_file(const char*, const char*);
82bool grep_string(const std::string&, const char*);
83
84struct run_h_tc_data {
85    const atf::tests::vars_map& m_config;
86
87    run_h_tc_data(const atf::tests::vars_map& config) :
88        m_config(config) {}
89};
90
91template< class TestCase >
92void
93run_h_tc_child(void* v)
94{
95    run_h_tc_data* data = static_cast< run_h_tc_data* >(v);
96
97    TestCase tc;
98    tc.init(data->m_config);
99    tc.run("result");
100    std::exit(EXIT_SUCCESS);
101}
102
103template< class TestCase >
104void
105run_h_tc(atf::tests::vars_map config = atf::tests::vars_map())
106{
107    run_h_tc_data data(config);
108    atf::process::child c = atf::process::fork(
109        run_h_tc_child< TestCase >,
110        atf::process::stream_redirect_path(atf::fs::path("stdout")),
111        atf::process::stream_redirect_path(atf::fs::path("stderr")),
112        &data);
113    const atf::process::status s = c.wait();
114    ATF_REQUIRE(s.exited());
115}
116
117namespace test_helpers_detail {
118
119typedef std::vector< std::string > string_vector;
120
121template< class Reader >
122std::pair< string_vector, string_vector >
123do_read(const char* input)
124{
125    string_vector errors;
126
127    std::istringstream is(input);
128    Reader reader(is);
129    try {
130        reader.read();
131    } catch (const atf::parser::parse_errors& pes) {
132        for (std::vector< atf::parser::parse_error >::const_iterator iter =
133             pes.begin(); iter != pes.end(); iter++)
134            errors.push_back(*iter);
135    } catch (const atf::parser::parse_error& pe) {
136        ATF_FAIL("Raised a lonely parse error: " +
137                 atf::text::to_string(pe.first) + ": " + pe.second);
138    }
139
140    return std::make_pair(reader.m_calls, errors);
141}
142
143void check_equal(const char*[], const string_vector&);
144
145} // namespace test_helpers_detail
146
147template< class Reader >
148void
149do_parser_test(const char* input, const char* exp_calls[],
150               const char* exp_errors[])
151{
152    const std::pair< test_helpers_detail::string_vector,
153                     test_helpers_detail::string_vector >
154        actual = test_helpers_detail::do_read< Reader >(input);
155    test_helpers_detail::check_equal(exp_calls, actual.first);
156    test_helpers_detail::check_equal(exp_errors, actual.second);
157}
158