2 // Automated Testing Framework (atf)
4 // Copyright (c) 2010 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 #include "atf-c++/detail/env.hpp"
31 #include "atf-c++/detail/test_helpers.hpp"
32 #include "atf-c++/config.hpp"
33 #include "atf-c++/macros.hpp"
37 namespace impl
= atf::atf_run
;
38 namespace detail
= atf::atf_run::detail
;
40 using atf::tests::vars_map
;
50 // -------------------------------------------------------------------------
51 // Tests for the "config" parser.
52 // -------------------------------------------------------------------------
54 class config_reader
: protected detail::atf_config_reader
{
56 got_var(const std::string
& name
, const std::string
& val
)
58 m_calls
.push_back("got_var(" + name
+ ", " + val
+ ")");
64 m_calls
.push_back("got_eof()");
68 config_reader(std::istream
& is
) :
69 detail::atf_config_reader(is
)
76 atf_config_reader::read();
79 std::vector
< std::string
> m_calls
;
82 ATF_TEST_CASE_WITHOUT_HEAD(config_1
);
83 ATF_TEST_CASE_BODY(config_1
)
86 "Content-Type: application/X-atf-config; version=\"1\"\n"
90 const char* exp_calls
[] = {
95 const char* exp_errors
[] = {
99 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
102 ATF_TEST_CASE_WITHOUT_HEAD(config_2
);
103 ATF_TEST_CASE_BODY(config_2
)
106 "Content-Type: application/X-atf-config; version=\"1\"\n"
108 "# This is a comment on a line of its own.\n"
109 "# And this is another one.\n"
111 " # Another after some whitespace.\n"
113 "# The last one after an empty line.\n"
116 const char* exp_calls
[] = {
121 const char* exp_errors
[] = {
125 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
128 ATF_TEST_CASE_WITHOUT_HEAD(config_3
);
129 ATF_TEST_CASE_BODY(config_3
)
132 "Content-Type: application/X-atf-config; version=\"1\"\n"
142 "var7 = \"This is a long value.\"\n"
143 "var8 = \"Single-word\"\n"
144 "var9 = \" Single-word \"\n"
145 "var10 = Single-word\n"
148 const char* exp_calls
[] = {
149 "got_var(var1, value1)",
150 "got_var(var2, value2)",
151 "got_var(var3, value3)",
152 "got_var(var4, value4)",
153 "got_var(var5, value5)",
154 "got_var(var6, value6)",
155 "got_var(var7, This is a long value.)",
156 "got_var(var8, Single-word)",
157 "got_var(var9, Single-word )",
158 "got_var(var10, Single-word)",
163 const char* exp_errors
[] = {
167 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
170 ATF_TEST_CASE_WITHOUT_HEAD(config_4
);
171 ATF_TEST_CASE_BODY(config_4
)
174 "Content-Type: application/X-atf-config; version=\"1\"\n"
176 "foo = bar # A comment.\n"
179 const char* exp_calls
[] = {
185 const char* exp_errors
[] = {
189 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
192 ATF_TEST_CASE_WITHOUT_HEAD(config_50
);
193 ATF_TEST_CASE_BODY(config_50
)
196 "Content-Type: application/X-atf-config; version=\"1\"\n"
201 const char* exp_calls
[] = {
205 const char* exp_errors
[] = {
206 "3: Unexpected token `<<NEWLINE>>'; expected equal sign",
210 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
213 ATF_TEST_CASE_WITHOUT_HEAD(config_51
);
214 ATF_TEST_CASE_BODY(config_51
)
217 "Content-Type: application/X-atf-config; version=\"1\"\n"
223 const char* exp_calls
[] = {
227 const char* exp_errors
[] = {
228 "3: Unexpected token `bar'; expected equal sign",
229 "4: Unexpected token `<<NEWLINE>>'; expected equal sign",
233 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
236 ATF_TEST_CASE_WITHOUT_HEAD(config_52
);
237 ATF_TEST_CASE_BODY(config_52
)
240 "Content-Type: application/X-atf-config; version=\"1\"\n"
243 "bar = # A comment.\n"
246 const char* exp_calls
[] = {
250 const char* exp_errors
[] = {
251 "3: Unexpected token `<<NEWLINE>>'; expected word or quoted string",
252 "4: Unexpected token `#'; expected word or quoted string",
256 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
259 ATF_TEST_CASE_WITHOUT_HEAD(config_53
);
260 ATF_TEST_CASE_BODY(config_53
)
263 "Content-Type: application/X-atf-config; version=\"1\"\n"
265 "foo = \"Correct value\" # With comment.\n"
267 "bar = # A comment.\n"
269 "baz = \"Last variable\"\n"
274 const char* exp_calls
[] = {
275 "got_var(foo, Correct value)",
279 const char* exp_errors
[] = {
280 "5: Unexpected token `#'; expected word or quoted string",
284 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
287 ATF_TEST_CASE_WITHOUT_HEAD(config_54
);
288 ATF_TEST_CASE_BODY(config_54
)
291 "Content-Type: application/X-atf-config; version=\"1\"\n"
299 const char* exp_calls
[] = {
303 const char* exp_errors
[] = {
304 "3: Missing double quotes before end of line",
305 "4: Missing double quotes before end of line",
306 "5: Missing double quotes before end of line",
307 "6: Missing double quotes before end of line",
311 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
314 // -------------------------------------------------------------------------
315 // Tests for the free functions.
316 // -------------------------------------------------------------------------
318 ATF_TEST_CASE(merge_configs_both_empty
);
319 ATF_TEST_CASE_HEAD(merge_configs_both_empty
) {}
320 ATF_TEST_CASE_BODY(merge_configs_both_empty
) {
321 vars_map lower
, upper
;
323 ATF_REQUIRE(impl::merge_configs(lower
, upper
).empty());
326 ATF_TEST_CASE(merge_configs_lower_empty
);
327 ATF_TEST_CASE_HEAD(merge_configs_lower_empty
) {}
328 ATF_TEST_CASE_BODY(merge_configs_lower_empty
) {
329 vars_map lower
, upper
;
330 upper
["var"] = "value";
332 vars_map merged
= impl::merge_configs(lower
, upper
);
333 ATF_REQUIRE_EQ("value", merged
["var"]);
336 ATF_TEST_CASE(merge_configs_upper_empty
);
337 ATF_TEST_CASE_HEAD(merge_configs_upper_empty
) {}
338 ATF_TEST_CASE_BODY(merge_configs_upper_empty
) {
339 vars_map lower
, upper
;
340 lower
["var"] = "value";
342 vars_map merged
= impl::merge_configs(lower
, upper
);
343 ATF_REQUIRE_EQ("value", merged
["var"]);
346 ATF_TEST_CASE(merge_configs_mixed
);
347 ATF_TEST_CASE_HEAD(merge_configs_mixed
) {}
348 ATF_TEST_CASE_BODY(merge_configs_mixed
) {
349 vars_map lower
, upper
;
350 lower
["var1"] = "value1";
351 lower
["var2"] = "value2-l";
352 upper
["var2"] = "value2-u";
353 upper
["var3"] = "value3";
355 vars_map merged
= impl::merge_configs(lower
, upper
);
356 ATF_REQUIRE_EQ("value1", merged
["var1"]);
357 ATF_REQUIRE_EQ("value2-u", merged
["var2"]);
358 ATF_REQUIRE_EQ("value3", merged
["var3"]);
361 ATF_TEST_CASE(read_config_files_none
);
362 ATF_TEST_CASE_HEAD(read_config_files_none
) {}
363 ATF_TEST_CASE_BODY(read_config_files_none
) {
364 atf::env::set("ATF_CONFDIR", ".");
365 atf::config::__reinit();
366 ATF_REQUIRE(vars_map() == impl::read_config_files("test-suite"));
369 // -------------------------------------------------------------------------
371 // -------------------------------------------------------------------------
373 ATF_INIT_TEST_CASES(tcs
)
375 ATF_ADD_TEST_CASE(tcs
, config_1
);
376 ATF_ADD_TEST_CASE(tcs
, config_2
);
377 ATF_ADD_TEST_CASE(tcs
, config_3
);
378 ATF_ADD_TEST_CASE(tcs
, config_4
);
379 ATF_ADD_TEST_CASE(tcs
, config_50
);
380 ATF_ADD_TEST_CASE(tcs
, config_51
);
381 ATF_ADD_TEST_CASE(tcs
, config_52
);
382 ATF_ADD_TEST_CASE(tcs
, config_53
);
383 ATF_ADD_TEST_CASE(tcs
, config_54
);
385 ATF_ADD_TEST_CASE(tcs
, merge_configs_both_empty
);
386 ATF_ADD_TEST_CASE(tcs
, merge_configs_lower_empty
);
387 ATF_ADD_TEST_CASE(tcs
, merge_configs_upper_empty
);
388 ATF_ADD_TEST_CASE(tcs
, merge_configs_mixed
);
390 ATF_ADD_TEST_CASE(tcs
, read_config_files_none
);