2 // Automated Testing Framework (atf)
4 // Copyright (c) 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
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.
30 #if defined(TOOLS_TEST_HELPERS_H)
31 # error "Cannot include test_helpers.hpp more than once."
33 # define TOOLS_TEST_HELPERS_H
40 #include <atf-c++.hpp>
45 namespace test_helpers_detail
{
47 typedef std::vector
< std::string
> string_vector
;
49 template< class Reader
>
50 std::pair
< string_vector
, string_vector
>
51 do_read(const char* input
)
55 std::istringstream
is(input
);
59 } catch (const tools::parser::parse_errors
& pes
) {
60 for (std::vector
< tools::parser::parse_error
>::const_iterator iter
=
61 pes
.begin(); iter
!= pes
.end(); iter
++)
62 errors
.push_back(*iter
);
63 } catch (const tools::parser::parse_error
& pe
) {
64 ATF_FAIL("Raised a lonely parse error: " +
65 tools::text::to_string(pe
.first
) + ": " + pe
.second
);
68 return std::make_pair(reader
.m_calls
, errors
);
72 check_equal(const char* expected
[], const string_vector
& actual
)
74 const char** expected_iter
= expected
;
75 string_vector::const_iterator actual_iter
= actual
.begin();
78 while (equals
&& *expected_iter
!= NULL
&& actual_iter
!= actual
.end()) {
79 if (*expected_iter
!= *actual_iter
) {
86 if (equals
&& ((*expected_iter
== NULL
&& actual_iter
!= actual
.end()) ||
87 (*expected_iter
!= NULL
&& actual_iter
== actual
.end())))
91 std::cerr
<< "EXPECTED:\n";
92 for (expected_iter
= expected
; *expected_iter
!= NULL
; expected_iter
++)
93 std::cerr
<< *expected_iter
<< "\n";
95 std::cerr
<< "ACTUAL:\n";
96 for (actual_iter
= actual
.begin(); actual_iter
!= actual
.end();
98 std::cerr
<< *actual_iter
<< "\n";
100 ATF_FAIL("Expected results differ to actual values");
104 } // namespace test_helpers_detail
106 template< class Reader
>
108 do_parser_test(const char* input
, const char* exp_calls
[],
109 const char* exp_errors
[])
111 const std::pair
< test_helpers_detail::string_vector
,
112 test_helpers_detail::string_vector
>
113 actual
= test_helpers_detail::do_read
< Reader
>(input
);
114 test_helpers_detail::check_equal(exp_calls
, actual
.first
);
115 test_helpers_detail::check_equal(exp_errors
, actual
.second
);