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