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.
31 #include <sys/types.h>
39 #include "atf-c++/macros.hpp"
41 #include "atf-c++/detail/exceptions.hpp"
42 #include "atf-c++/detail/test_helpers.hpp"
44 #include "atffile.hpp"
46 namespace detail = atf::atf_run::detail;
48 // ------------------------------------------------------------------------
49 // Auxiliary functions.
50 // ------------------------------------------------------------------------
55 std::auto_ptr< std::ofstream >
58 std::auto_ptr< std::ofstream > os(new std::ofstream("Atffile"));
61 (*os) << "Content-Type: application/X-atf-atffile; version=\"1\"\n\n";
67 touch_exec(const char* name)
69 std::ofstream os(name);
72 ATF_REQUIRE(::chmod(name, S_IRWXU) != -1);
77 is_in(const std::string& value, const std::vector< std::string >& v)
79 return std::find(v.begin(), v.end(), value) != v.end();
82 } // anonymous namespace
84 // ------------------------------------------------------------------------
85 // Tests cases for the "atffile" parser.
86 // ------------------------------------------------------------------------
88 class atffile_reader : protected detail::atf_atffile_reader {
90 got_conf(const std::string& name, const std::string& val)
92 m_calls.push_back("got_conf(" + name + ", " + val + ")");
96 got_prop(const std::string& name, const std::string& val)
98 m_calls.push_back("got_prop(" + name + ", " + val + ")");
102 got_tp(const std::string& name, bool isglob)
104 m_calls.push_back("got_tp(" + name + ", " + (isglob ? "true" : "false")
111 m_calls.push_back("got_eof()");
115 atffile_reader(std::istream& is) :
116 detail::atf_atffile_reader(is)
123 atf_atffile_reader::read();
126 std::vector< std::string > m_calls;
129 ATF_TEST_CASE_WITHOUT_HEAD(atffile_1);
130 ATF_TEST_CASE_BODY(atffile_1)
133 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
137 const char* exp_calls[] = {
142 const char* exp_errors[] = {
146 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
149 ATF_TEST_CASE_WITHOUT_HEAD(atffile_2);
150 ATF_TEST_CASE_BODY(atffile_2)
153 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
155 "# This is a comment on a line of its own.\n"
156 "# And this is another one.\n"
158 " # Another after some whitespace.\n"
160 "# The last one after an empty line.\n"
163 const char* exp_calls[] = {
168 const char* exp_errors[] = {
172 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
175 ATF_TEST_CASE_WITHOUT_HEAD(atffile_3);
176 ATF_TEST_CASE_BODY(atffile_3)
179 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
181 "conf: var1=value1\n"
182 "conf: var2 = value2\n"
183 "conf: var3 = value3\n"
184 "conf: var4 = value4\n"
187 " conf:var6=value6\n"
189 "conf: var7 = \"This is a long value.\"\n"
190 "conf: var8 = \"Single-word\"\n"
191 "conf: var9 = \" Single-word \"\n"
192 "conf: var10 = Single-word\n"
195 const char* exp_calls[] = {
196 "got_conf(var1, value1)",
197 "got_conf(var2, value2)",
198 "got_conf(var3, value3)",
199 "got_conf(var4, value4)",
200 "got_conf(var5, value5)",
201 "got_conf(var6, value6)",
202 "got_conf(var7, This is a long value.)",
203 "got_conf(var8, Single-word)",
204 "got_conf(var9, Single-word )",
205 "got_conf(var10, Single-word)",
210 const char* exp_errors[] = {
214 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
217 ATF_TEST_CASE_WITHOUT_HEAD(atffile_4);
218 ATF_TEST_CASE_BODY(atffile_4)
221 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
223 "prop: var1=value1\n"
224 "prop: var2 = value2\n"
225 "prop: var3 = value3\n"
226 "prop: var4 = value4\n"
229 " prop:var6=value6\n"
231 "prop: var7 = \"This is a long value.\"\n"
232 "prop: var8 = \"Single-word\"\n"
233 "prop: var9 = \" Single-word \"\n"
234 "prop: var10 = Single-word\n"
237 const char* exp_calls[] = {
238 "got_prop(var1, value1)",
239 "got_prop(var2, value2)",
240 "got_prop(var3, value3)",
241 "got_prop(var4, value4)",
242 "got_prop(var5, value5)",
243 "got_prop(var6, value6)",
244 "got_prop(var7, This is a long value.)",
245 "got_prop(var8, Single-word)",
246 "got_prop(var9, Single-word )",
247 "got_prop(var10, Single-word)",
252 const char* exp_errors[] = {
256 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
259 ATF_TEST_CASE_WITHOUT_HEAD(atffile_5);
260 ATF_TEST_CASE_BODY(atffile_5)
263 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
270 "tp: \"name with spaces\"\n"
271 "tp: \"single-word\"\n"
275 "tp-glob: foo*?bar\n"
276 "tp-glob: foo*?bar\n"
277 "tp-glob: foo*?bar\n"
278 "tp-glob: foo*?bar\n"
279 "tp-glob: \"glob * with ? spaces\"\n"
280 "tp-glob: \"single-*-word\"\n"
281 "tp-glob: single-*-word\n"
284 const char* exp_calls[] = {
285 "got_tp(foo, false)",
286 "got_tp(foo, false)",
287 "got_tp(foo, false)",
288 "got_tp(foo, false)",
289 "got_tp(foo, false)",
290 "got_tp(name with spaces, false)",
291 "got_tp(single-word, false)",
292 "got_tp(single-word, false)",
293 "got_tp(foo*?bar, true)",
294 "got_tp(foo*?bar, true)",
295 "got_tp(foo*?bar, true)",
296 "got_tp(foo*?bar, true)",
297 "got_tp(foo*?bar, true)",
298 "got_tp(glob * with ? spaces, true)",
299 "got_tp(single-*-word, true)",
300 "got_tp(single-*-word, true)",
305 const char* exp_errors[] = {
309 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
312 ATF_TEST_CASE_WITHOUT_HEAD(atffile_6);
313 ATF_TEST_CASE_BODY(atffile_6)
316 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
318 "prop: foo = bar # A comment.\n"
319 "conf: foo = bar # A comment.\n"
320 "tp: foo # A comment.\n"
321 "tp-glob: foo # A comment.\n"
324 const char* exp_calls[] = {
325 "got_prop(foo, bar)",
326 "got_conf(foo, bar)",
327 "got_tp(foo, false)",
333 const char* exp_errors[] = {
337 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
340 ATF_TEST_CASE_WITHOUT_HEAD(atffile_50);
341 ATF_TEST_CASE_BODY(atffile_50)
344 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
349 const char* exp_calls[] = {
353 // NO_CHECK_STYLE_BEGIN
354 const char* exp_errors[] = {
355 "3: Unexpected token `foo'; expected conf, #, prop, tp, tp-glob, a new line or eof",
358 // NO_CHECK_STYLE_END
360 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
363 ATF_TEST_CASE_WITHOUT_HEAD(atffile_51);
364 ATF_TEST_CASE_BODY(atffile_51)
367 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
373 const char* exp_calls[] = {
377 // NO_CHECK_STYLE_BEGIN
378 const char* exp_errors[] = {
379 "3: Unexpected token `foo'; expected conf, #, prop, tp, tp-glob, a new line or eof",
380 "4: Unexpected token `baz'; expected conf, #, prop, tp, tp-glob, a new line or eof",
383 // NO_CHECK_STYLE_END
385 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
388 ATF_TEST_CASE_WITHOUT_HEAD(atffile_52);
389 ATF_TEST_CASE_BODY(atffile_52)
392 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
397 "conf: bar = # A comment.\n"
402 "prop: bar = # A comment.\n"
410 "tp-glob: # A comment.\n"
413 const char* exp_calls[] = {
417 const char* exp_errors[] = {
418 "3: Unexpected token `<<NEWLINE>>'; expected `:'",
419 "4: Unexpected token `<<NEWLINE>>'; expected variable name",
420 "5: Unexpected token `<<NEWLINE>>'; expected word or quoted string",
421 "6: Unexpected token `#'; expected word or quoted string",
422 "8: Unexpected token `<<NEWLINE>>'; expected `:'",
423 "9: Unexpected token `<<NEWLINE>>'; expected property name",
424 "10: Unexpected token `<<NEWLINE>>'; expected word or quoted string",
425 "11: Unexpected token `#'; expected word or quoted string",
426 "13: Unexpected token `<<NEWLINE>>'; expected `:'",
427 "14: Unexpected token `<<NEWLINE>>'; expected word or quoted string",
428 "15: Unexpected token `#'; expected word or quoted string",
429 "17: Unexpected token `<<NEWLINE>>'; expected `:'",
430 "18: Unexpected token `<<NEWLINE>>'; expected word or quoted string",
431 "19: Unexpected token `#'; expected word or quoted string",
435 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
438 ATF_TEST_CASE_WITHOUT_HEAD(atffile_53);
439 ATF_TEST_CASE_BODY(atffile_53)
442 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
444 "prop: foo = \"Correct value\" # With comment.\n"
446 "prop: bar = # A comment.\n"
448 "prop: baz = \"Last variable\"\n"
453 const char* exp_calls[] = {
454 "got_prop(foo, Correct value)",
458 const char* exp_errors[] = {
459 "5: Unexpected token `#'; expected word or quoted string",
463 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
466 ATF_TEST_CASE_WITHOUT_HEAD(atffile_54);
467 ATF_TEST_CASE_BODY(atffile_54)
470 "Content-Type: application/X-atf-atffile; version=\"1\"\n"
473 "prop: bar = \"text\n"
474 "prop: baz = \"te\\\"xt\n"
475 "prop: last = \"\\\"\n"
478 const char* exp_calls[] = {
482 const char* exp_errors[] = {
483 "3: Missing double quotes before end of line",
484 "4: Missing double quotes before end of line",
485 "5: Missing double quotes before end of line",
486 "6: Missing double quotes before end of line",
490 do_parser_test< atffile_reader >(input, exp_calls, exp_errors);
493 // ------------------------------------------------------------------------
494 // Tests cases for the "atffile" class.
495 // ------------------------------------------------------------------------
497 ATF_TEST_CASE(atffile_getters);
498 ATF_TEST_CASE_HEAD(atffile_getters) {}
499 ATF_TEST_CASE_BODY(atffile_getters) {
500 atf::tests::vars_map config_vars;
501 config_vars["config-var-1"] = "value 1";
503 std::vector< std::string > test_program_names;
504 test_program_names.push_back("test-program-1");
506 atf::tests::vars_map properties;
507 properties["test-suite"] = "a test name";
509 const atf::atf_run::atffile atffile(config_vars, test_program_names,
511 ATF_REQUIRE(config_vars == atffile.conf());
512 ATF_REQUIRE(test_program_names == atffile.tps());
513 ATF_REQUIRE(properties == atffile.props());
516 // ------------------------------------------------------------------------
517 // Tests cases for the free functions.
518 // ------------------------------------------------------------------------
520 ATF_TEST_CASE_WITHOUT_HEAD(read_ok_simple);
521 ATF_TEST_CASE_BODY(read_ok_simple) {
522 std::auto_ptr< std::ofstream > os = new_atffile();
523 (*os) << "prop: test-suite = foo\n";
524 (*os) << "tp: tp-1\n";
525 (*os) << "conf: var1 = value1\n";
526 (*os) << "tp: tp-2\n";
527 (*os) << "tp: tp-3\n";
528 (*os) << "prop: prop1 = propvalue1\n";
529 (*os) << "conf: var2 = value2\n";
536 const atf::atf_run::atffile atffile = atf::atf_run::read_atffile(
537 atf::fs::path("Atffile"));
538 ATF_REQUIRE_EQ(2, atffile.conf().size());
539 ATF_REQUIRE_EQ("value1", atffile.conf().find("var1")->second);
540 ATF_REQUIRE_EQ("value2", atffile.conf().find("var2")->second);
541 ATF_REQUIRE_EQ(3, atffile.tps().size());
542 ATF_REQUIRE(is_in("tp-1", atffile.tps()));
543 ATF_REQUIRE(is_in("tp-2", atffile.tps()));
544 ATF_REQUIRE(is_in("tp-3", atffile.tps()));
545 ATF_REQUIRE_EQ(2, atffile.props().size());
546 ATF_REQUIRE_EQ("foo", atffile.props().find("test-suite")->second);
547 ATF_REQUIRE_EQ("propvalue1", atffile.props().find("prop1")->second);
550 ATF_TEST_CASE_WITHOUT_HEAD(read_ok_some_globs);
551 ATF_TEST_CASE_BODY(read_ok_some_globs) {
552 std::auto_ptr< std::ofstream > os = new_atffile();
553 (*os) << "prop: test-suite = foo\n";
554 (*os) << "tp: foo\n";
555 (*os) << "tp-glob: *K*\n";
556 (*os) << "tp: bar\n";
557 (*os) << "tp-glob: t_*\n";
564 touch_exec("t_hello");
565 touch_exec("zzzt_hello");
567 const atf::atf_run::atffile atffile = atf::atf_run::read_atffile(
568 atf::fs::path("Atffile"));
569 ATF_REQUIRE_EQ(5, atffile.tps().size());
570 ATF_REQUIRE(is_in("foo", atffile.tps()));
571 ATF_REQUIRE(is_in("bar", atffile.tps()));
572 ATF_REQUIRE(is_in("aK", atffile.tps()));
573 ATF_REQUIRE(is_in("KKKKK", atffile.tps()));
574 ATF_REQUIRE(is_in("t_hello", atffile.tps()));
577 ATF_TEST_CASE_WITHOUT_HEAD(read_missing_test_suite);
578 ATF_TEST_CASE_BODY(read_missing_test_suite) {
579 std::auto_ptr< std::ofstream > os = new_atffile();
583 (void)atf::atf_run::read_atffile(atf::fs::path("Atffile"));
584 ATF_FAIL("Missing property 'test-suite' did not raise an error");
585 } catch (const atf::not_found_error< std::string >& e) {
586 ATF_REQUIRE_EQ("test-suite", e.get_value());
590 ATF_TEST_CASE_WITHOUT_HEAD(read_missing_test_program);
591 ATF_TEST_CASE_BODY(read_missing_test_program) {
592 std::auto_ptr< std::ofstream > os = new_atffile();
593 (*os) << "tp: foo\n";
594 (*os) << "tp: bar\n";
595 (*os) << "tp: baz\n";
602 (void)atf::atf_run::read_atffile(atf::fs::path("Atffile"));
603 ATF_FAIL("Missing file 'bar' did not raise an error");
604 } catch (const atf::not_found_error< atf::fs::path >& e) {
605 ATF_REQUIRE_EQ("bar", e.get_value().str());
609 // ------------------------------------------------------------------------
611 // ------------------------------------------------------------------------
613 ATF_INIT_TEST_CASES(tcs)
615 // Add the test cases for the parser class.
616 ATF_ADD_TEST_CASE(tcs, atffile_1);
617 ATF_ADD_TEST_CASE(tcs, atffile_2);
618 ATF_ADD_TEST_CASE(tcs, atffile_3);
619 ATF_ADD_TEST_CASE(tcs, atffile_4);
620 ATF_ADD_TEST_CASE(tcs, atffile_5);
621 ATF_ADD_TEST_CASE(tcs, atffile_6);
622 ATF_ADD_TEST_CASE(tcs, atffile_50);
623 ATF_ADD_TEST_CASE(tcs, atffile_51);
624 ATF_ADD_TEST_CASE(tcs, atffile_52);
625 ATF_ADD_TEST_CASE(tcs, atffile_53);
626 ATF_ADD_TEST_CASE(tcs, atffile_54);
628 // Add the test cases for the atffile class.
629 ATF_ADD_TEST_CASE(tcs, atffile_getters);
631 // Add the test cases for the free functions.
632 ATF_ADD_TEST_CASE(tcs, read_ok_simple);
633 ATF_ADD_TEST_CASE(tcs, read_ok_some_globs);
634 ATF_ADD_TEST_CASE(tcs, read_missing_test_suite);
635 ATF_ADD_TEST_CASE(tcs, read_missing_test_program);