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++.hpp>
33 #include "config_file.hpp"
35 #include "test_helpers.hpp"
37 namespace impl
= tools::config_file
;
38 namespace detail
= tools::config_file::detail
;
42 typedef std::map
< std::string
, std::string
> vars_map
;
44 } // anonymous namespace
54 // -------------------------------------------------------------------------
55 // Tests for the "config" parser.
56 // -------------------------------------------------------------------------
58 class config_reader
: protected detail::atf_config_reader
{
60 got_var(const std::string
& name
, const std::string
& val
)
62 m_calls
.push_back("got_var(" + name
+ ", " + val
+ ")");
68 m_calls
.push_back("got_eof()");
72 config_reader(std::istream
& is
) :
73 detail::atf_config_reader(is
)
80 atf_config_reader::read();
83 std::vector
< std::string
> m_calls
;
86 ATF_TEST_CASE_WITHOUT_HEAD(config_1
);
87 ATF_TEST_CASE_BODY(config_1
)
90 "Content-Type: application/X-atf-config; version=\"1\"\n"
94 const char* exp_calls
[] = {
99 const char* exp_errors
[] = {
103 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
106 ATF_TEST_CASE_WITHOUT_HEAD(config_2
);
107 ATF_TEST_CASE_BODY(config_2
)
110 "Content-Type: application/X-atf-config; version=\"1\"\n"
112 "# This is a comment on a line of its own.\n"
113 "# And this is another one.\n"
115 " # Another after some whitespace.\n"
117 "# The last one after an empty line.\n"
120 const char* exp_calls
[] = {
125 const char* exp_errors
[] = {
129 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
132 ATF_TEST_CASE_WITHOUT_HEAD(config_3
);
133 ATF_TEST_CASE_BODY(config_3
)
136 "Content-Type: application/X-atf-config; version=\"1\"\n"
146 "var7 = \"This is a long value.\"\n"
147 "var8 = \"Single-word\"\n"
148 "var9 = \" Single-word \"\n"
149 "var10 = Single-word\n"
152 const char* exp_calls
[] = {
153 "got_var(var1, value1)",
154 "got_var(var2, value2)",
155 "got_var(var3, value3)",
156 "got_var(var4, value4)",
157 "got_var(var5, value5)",
158 "got_var(var6, value6)",
159 "got_var(var7, This is a long value.)",
160 "got_var(var8, Single-word)",
161 "got_var(var9, Single-word )",
162 "got_var(var10, Single-word)",
167 const char* exp_errors
[] = {
171 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
174 ATF_TEST_CASE_WITHOUT_HEAD(config_4
);
175 ATF_TEST_CASE_BODY(config_4
)
178 "Content-Type: application/X-atf-config; version=\"1\"\n"
180 "foo = bar # A comment.\n"
183 const char* exp_calls
[] = {
189 const char* exp_errors
[] = {
193 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
196 ATF_TEST_CASE_WITHOUT_HEAD(config_50
);
197 ATF_TEST_CASE_BODY(config_50
)
200 "Content-Type: application/X-atf-config; version=\"1\"\n"
205 const char* exp_calls
[] = {
209 const char* exp_errors
[] = {
210 "3: Unexpected token `<<NEWLINE>>'; expected equal sign",
214 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
217 ATF_TEST_CASE_WITHOUT_HEAD(config_51
);
218 ATF_TEST_CASE_BODY(config_51
)
221 "Content-Type: application/X-atf-config; version=\"1\"\n"
227 const char* exp_calls
[] = {
231 const char* exp_errors
[] = {
232 "3: Unexpected token `bar'; expected equal sign",
233 "4: Unexpected token `<<NEWLINE>>'; expected equal sign",
237 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
240 ATF_TEST_CASE_WITHOUT_HEAD(config_52
);
241 ATF_TEST_CASE_BODY(config_52
)
244 "Content-Type: application/X-atf-config; version=\"1\"\n"
247 "bar = # A comment.\n"
250 const char* exp_calls
[] = {
254 const char* exp_errors
[] = {
255 "3: Unexpected token `<<NEWLINE>>'; expected word or quoted string",
256 "4: Unexpected token `#'; expected word or quoted string",
260 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
263 ATF_TEST_CASE_WITHOUT_HEAD(config_53
);
264 ATF_TEST_CASE_BODY(config_53
)
267 "Content-Type: application/X-atf-config; version=\"1\"\n"
269 "foo = \"Correct value\" # With comment.\n"
271 "bar = # A comment.\n"
273 "baz = \"Last variable\"\n"
278 const char* exp_calls
[] = {
279 "got_var(foo, Correct value)",
283 const char* exp_errors
[] = {
284 "5: Unexpected token `#'; expected word or quoted string",
288 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
291 ATF_TEST_CASE_WITHOUT_HEAD(config_54
);
292 ATF_TEST_CASE_BODY(config_54
)
295 "Content-Type: application/X-atf-config; version=\"1\"\n"
303 const char* exp_calls
[] = {
307 const char* exp_errors
[] = {
308 "3: Missing double quotes before end of line",
309 "4: Missing double quotes before end of line",
310 "5: Missing double quotes before end of line",
311 "6: Missing double quotes before end of line",
315 do_parser_test
< config_reader
>(input
, exp_calls
, exp_errors
);
318 // -------------------------------------------------------------------------
319 // Tests for the free functions.
320 // -------------------------------------------------------------------------
322 ATF_TEST_CASE(merge_configs_both_empty
);
323 ATF_TEST_CASE_HEAD(merge_configs_both_empty
) {}
324 ATF_TEST_CASE_BODY(merge_configs_both_empty
) {
325 vars_map lower
, upper
;
327 ATF_REQUIRE(impl::merge_configs(lower
, upper
).empty());
330 ATF_TEST_CASE(merge_configs_lower_empty
);
331 ATF_TEST_CASE_HEAD(merge_configs_lower_empty
) {}
332 ATF_TEST_CASE_BODY(merge_configs_lower_empty
) {
333 vars_map lower
, upper
;
334 upper
["var"] = "value";
336 vars_map merged
= impl::merge_configs(lower
, upper
);
337 ATF_REQUIRE_EQ("value", merged
["var"]);
340 ATF_TEST_CASE(merge_configs_upper_empty
);
341 ATF_TEST_CASE_HEAD(merge_configs_upper_empty
) {}
342 ATF_TEST_CASE_BODY(merge_configs_upper_empty
) {
343 vars_map lower
, upper
;
344 lower
["var"] = "value";
346 vars_map merged
= impl::merge_configs(lower
, upper
);
347 ATF_REQUIRE_EQ("value", merged
["var"]);
350 ATF_TEST_CASE(merge_configs_mixed
);
351 ATF_TEST_CASE_HEAD(merge_configs_mixed
) {}
352 ATF_TEST_CASE_BODY(merge_configs_mixed
) {
353 vars_map lower
, upper
;
354 lower
["var1"] = "value1";
355 lower
["var2"] = "value2-l";
356 upper
["var2"] = "value2-u";
357 upper
["var3"] = "value3";
359 vars_map merged
= impl::merge_configs(lower
, upper
);
360 ATF_REQUIRE_EQ("value1", merged
["var1"]);
361 ATF_REQUIRE_EQ("value2-u", merged
["var2"]);
362 ATF_REQUIRE_EQ("value3", merged
["var3"]);
365 ATF_TEST_CASE(read_config_files_none
);
366 ATF_TEST_CASE_HEAD(read_config_files_none
) {}
367 ATF_TEST_CASE_BODY(read_config_files_none
) {
368 tools::env::set("ATF_CONFDIR", ".");
369 atf::config::__reinit();
370 ATF_REQUIRE(vars_map() == impl::read_config_files("test-suite"));
373 // -------------------------------------------------------------------------
375 // -------------------------------------------------------------------------
377 ATF_INIT_TEST_CASES(tcs
)
379 ATF_ADD_TEST_CASE(tcs
, config_1
);
380 ATF_ADD_TEST_CASE(tcs
, config_2
);
381 ATF_ADD_TEST_CASE(tcs
, config_3
);
382 ATF_ADD_TEST_CASE(tcs
, config_4
);
383 ATF_ADD_TEST_CASE(tcs
, config_50
);
384 ATF_ADD_TEST_CASE(tcs
, config_51
);
385 ATF_ADD_TEST_CASE(tcs
, config_52
);
386 ATF_ADD_TEST_CASE(tcs
, config_53
);
387 ATF_ADD_TEST_CASE(tcs
, config_54
);
389 ATF_ADD_TEST_CASE(tcs
, merge_configs_both_empty
);
390 ATF_ADD_TEST_CASE(tcs
, merge_configs_lower_empty
);
391 ATF_ADD_TEST_CASE(tcs
, merge_configs_upper_empty
);
392 ATF_ADD_TEST_CASE(tcs
, merge_configs_mixed
);
394 ATF_ADD_TEST_CASE(tcs
, read_config_files_none
);