xref: /minix3/external/bsd/atf/dist/atf-c++/macros_test.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
111be35a1SLionel Sambuc //
211be35a1SLionel Sambuc // Automated Testing Framework (atf)
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Copyright (c) 2008 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc // All rights reserved.
611be35a1SLionel Sambuc //
711be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc // are met:
1011be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc //
1611be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc //
2911be35a1SLionel Sambuc 
3011be35a1SLionel Sambuc extern "C" {
3111be35a1SLionel Sambuc #include <fcntl.h>
3211be35a1SLionel Sambuc #include <unistd.h>
3311be35a1SLionel Sambuc }
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc #include <cerrno>
3611be35a1SLionel Sambuc #include <cstdlib>
3711be35a1SLionel Sambuc #include <iostream>
3811be35a1SLionel Sambuc #include <stdexcept>
3911be35a1SLionel Sambuc 
4011be35a1SLionel Sambuc #include "macros.hpp"
4111be35a1SLionel Sambuc #include "utils.hpp"
4211be35a1SLionel Sambuc 
4311be35a1SLionel Sambuc #include "detail/fs.hpp"
4411be35a1SLionel Sambuc #include "detail/process.hpp"
4511be35a1SLionel Sambuc #include "detail/sanity.hpp"
4611be35a1SLionel Sambuc #include "detail/test_helpers.hpp"
4711be35a1SLionel Sambuc #include "detail/text.hpp"
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc // ------------------------------------------------------------------------
5011be35a1SLionel Sambuc // Auxiliary functions.
5111be35a1SLionel Sambuc // ------------------------------------------------------------------------
5211be35a1SLionel Sambuc 
5311be35a1SLionel Sambuc static
5411be35a1SLionel Sambuc void
create_ctl_file(const char * name)5511be35a1SLionel Sambuc create_ctl_file(const char *name)
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc     ATF_REQUIRE(open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644) != -1);
5811be35a1SLionel Sambuc }
5911be35a1SLionel Sambuc 
6011be35a1SLionel Sambuc // ------------------------------------------------------------------------
6111be35a1SLionel Sambuc // Auxiliary test cases.
6211be35a1SLionel Sambuc // ------------------------------------------------------------------------
6311be35a1SLionel Sambuc 
6411be35a1SLionel Sambuc ATF_TEST_CASE(h_pass);
ATF_TEST_CASE_HEAD(h_pass)6511be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_pass)
6611be35a1SLionel Sambuc {
6711be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
6811be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_pass)6911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_pass)
7011be35a1SLionel Sambuc {
7111be35a1SLionel Sambuc     create_ctl_file("before");
7211be35a1SLionel Sambuc     ATF_PASS();
7311be35a1SLionel Sambuc     create_ctl_file("after");
7411be35a1SLionel Sambuc }
7511be35a1SLionel Sambuc 
7611be35a1SLionel Sambuc ATF_TEST_CASE(h_fail);
ATF_TEST_CASE_HEAD(h_fail)7711be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_fail)
7811be35a1SLionel Sambuc {
7911be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
8011be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_fail)8111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_fail)
8211be35a1SLionel Sambuc {
8311be35a1SLionel Sambuc     create_ctl_file("before");
8411be35a1SLionel Sambuc     ATF_FAIL("Failed on purpose");
8511be35a1SLionel Sambuc     create_ctl_file("after");
8611be35a1SLionel Sambuc }
8711be35a1SLionel Sambuc 
8811be35a1SLionel Sambuc ATF_TEST_CASE(h_skip);
ATF_TEST_CASE_HEAD(h_skip)8911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_skip)
9011be35a1SLionel Sambuc {
9111be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
9211be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_skip)9311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_skip)
9411be35a1SLionel Sambuc {
9511be35a1SLionel Sambuc     create_ctl_file("before");
9611be35a1SLionel Sambuc     ATF_SKIP("Skipped on purpose");
9711be35a1SLionel Sambuc     create_ctl_file("after");
9811be35a1SLionel Sambuc }
9911be35a1SLionel Sambuc 
10011be35a1SLionel Sambuc ATF_TEST_CASE(h_require);
ATF_TEST_CASE_HEAD(h_require)10111be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require)
10211be35a1SLionel Sambuc {
10311be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
10411be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require)10511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require)
10611be35a1SLionel Sambuc {
10711be35a1SLionel Sambuc     bool condition = atf::text::to_bool(get_config_var("condition"));
10811be35a1SLionel Sambuc 
10911be35a1SLionel Sambuc     create_ctl_file("before");
11011be35a1SLionel Sambuc     ATF_REQUIRE(condition);
11111be35a1SLionel Sambuc     create_ctl_file("after");
11211be35a1SLionel Sambuc }
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc ATF_TEST_CASE(h_require_eq);
ATF_TEST_CASE_HEAD(h_require_eq)11511be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require_eq)
11611be35a1SLionel Sambuc {
11711be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
11811be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require_eq)11911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require_eq)
12011be35a1SLionel Sambuc {
12111be35a1SLionel Sambuc     long v1 = atf::text::to_type< long >(get_config_var("v1"));
12211be35a1SLionel Sambuc     long v2 = atf::text::to_type< long >(get_config_var("v2"));
12311be35a1SLionel Sambuc 
12411be35a1SLionel Sambuc     create_ctl_file("before");
12511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(v1, v2);
12611be35a1SLionel Sambuc     create_ctl_file("after");
12711be35a1SLionel Sambuc }
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc ATF_TEST_CASE(h_require_in);
ATF_TEST_CASE_HEAD(h_require_in)13011be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require_in)
13111be35a1SLionel Sambuc {
13211be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
13311be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require_in)13411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require_in)
13511be35a1SLionel Sambuc {
13611be35a1SLionel Sambuc     const std::string element = get_config_var("value");
13711be35a1SLionel Sambuc 
13811be35a1SLionel Sambuc     std::set< std::string > collection;
13911be35a1SLionel Sambuc     collection.insert("foo");
14011be35a1SLionel Sambuc     collection.insert("bar");
14111be35a1SLionel Sambuc     collection.insert("baz");
14211be35a1SLionel Sambuc 
14311be35a1SLionel Sambuc     create_ctl_file("before");
14411be35a1SLionel Sambuc     ATF_REQUIRE_IN(element, collection);
14511be35a1SLionel Sambuc     create_ctl_file("after");
14611be35a1SLionel Sambuc }
14711be35a1SLionel Sambuc 
14811be35a1SLionel Sambuc ATF_TEST_CASE(h_require_match);
ATF_TEST_CASE_HEAD(h_require_match)14911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require_match)
15011be35a1SLionel Sambuc {
15111be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
15211be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require_match)15311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require_match)
15411be35a1SLionel Sambuc {
15511be35a1SLionel Sambuc     const std::string regexp = get_config_var("regexp");
15611be35a1SLionel Sambuc     const std::string string = get_config_var("string");
15711be35a1SLionel Sambuc 
15811be35a1SLionel Sambuc     create_ctl_file("before");
15911be35a1SLionel Sambuc     ATF_REQUIRE_MATCH(regexp, string);
16011be35a1SLionel Sambuc     create_ctl_file("after");
16111be35a1SLionel Sambuc }
16211be35a1SLionel Sambuc 
16311be35a1SLionel Sambuc ATF_TEST_CASE(h_require_not_in);
ATF_TEST_CASE_HEAD(h_require_not_in)16411be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require_not_in)
16511be35a1SLionel Sambuc {
16611be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
16711be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require_not_in)16811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require_not_in)
16911be35a1SLionel Sambuc {
17011be35a1SLionel Sambuc     const std::string element = get_config_var("value");
17111be35a1SLionel Sambuc 
17211be35a1SLionel Sambuc     std::set< std::string > collection;
17311be35a1SLionel Sambuc     collection.insert("foo");
17411be35a1SLionel Sambuc     collection.insert("bar");
17511be35a1SLionel Sambuc     collection.insert("baz");
17611be35a1SLionel Sambuc 
17711be35a1SLionel Sambuc     create_ctl_file("before");
17811be35a1SLionel Sambuc     ATF_REQUIRE_NOT_IN(element, collection);
17911be35a1SLionel Sambuc     create_ctl_file("after");
18011be35a1SLionel Sambuc }
18111be35a1SLionel Sambuc 
18211be35a1SLionel Sambuc ATF_TEST_CASE(h_require_throw);
ATF_TEST_CASE_HEAD(h_require_throw)18311be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require_throw)
18411be35a1SLionel Sambuc {
18511be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
18611be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require_throw)18711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require_throw)
18811be35a1SLionel Sambuc {
18911be35a1SLionel Sambuc     create_ctl_file("before");
19011be35a1SLionel Sambuc 
19111be35a1SLionel Sambuc     if (get_config_var("what") == "throw_int")
19211be35a1SLionel Sambuc         ATF_REQUIRE_THROW(std::runtime_error, if (1) throw int(5));
19311be35a1SLionel Sambuc     else if (get_config_var("what") == "throw_rt")
19411be35a1SLionel Sambuc         ATF_REQUIRE_THROW(std::runtime_error,
19511be35a1SLionel Sambuc                         if (1) throw std::runtime_error("e"));
19611be35a1SLionel Sambuc     else if (get_config_var("what") == "no_throw_rt")
19711be35a1SLionel Sambuc         ATF_REQUIRE_THROW(std::runtime_error,
19811be35a1SLionel Sambuc                         if (0) throw std::runtime_error("e"));
19911be35a1SLionel Sambuc 
20011be35a1SLionel Sambuc     create_ctl_file("after");
20111be35a1SLionel Sambuc }
20211be35a1SLionel Sambuc 
20311be35a1SLionel Sambuc ATF_TEST_CASE(h_require_throw_re);
ATF_TEST_CASE_HEAD(h_require_throw_re)20411be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require_throw_re)
20511be35a1SLionel Sambuc {
20611be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
20711be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require_throw_re)20811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require_throw_re)
20911be35a1SLionel Sambuc {
21011be35a1SLionel Sambuc     create_ctl_file("before");
21111be35a1SLionel Sambuc 
21211be35a1SLionel Sambuc     if (get_config_var("what") == "throw_int")
21311be35a1SLionel Sambuc         ATF_REQUIRE_THROW_RE(std::runtime_error, "5", if (1) throw int(5));
21411be35a1SLionel Sambuc     else if (get_config_var("what") == "throw_rt_match")
21511be35a1SLionel Sambuc         ATF_REQUIRE_THROW_RE(std::runtime_error, "foo.*baz",
21611be35a1SLionel Sambuc                              if (1) throw std::runtime_error("a foo bar baz"));
21711be35a1SLionel Sambuc     else if (get_config_var("what") == "throw_rt_no_match")
21811be35a1SLionel Sambuc         ATF_REQUIRE_THROW_RE(std::runtime_error, "foo.*baz",
21911be35a1SLionel Sambuc                              if (1) throw std::runtime_error("baz foo bar a"));
22011be35a1SLionel Sambuc     else if (get_config_var("what") == "no_throw_rt")
22111be35a1SLionel Sambuc         ATF_REQUIRE_THROW_RE(std::runtime_error, "e",
22211be35a1SLionel Sambuc                              if (0) throw std::runtime_error("e"));
22311be35a1SLionel Sambuc 
22411be35a1SLionel Sambuc     create_ctl_file("after");
22511be35a1SLionel Sambuc }
22611be35a1SLionel Sambuc 
22711be35a1SLionel Sambuc static int
errno_fail_stub(const int raised_errno)22811be35a1SLionel Sambuc errno_fail_stub(const int raised_errno)
22911be35a1SLionel Sambuc {
23011be35a1SLionel Sambuc     errno = raised_errno;
23111be35a1SLionel Sambuc     return -1;
23211be35a1SLionel Sambuc }
23311be35a1SLionel Sambuc 
23411be35a1SLionel Sambuc static int
errno_ok_stub(void)23511be35a1SLionel Sambuc errno_ok_stub(void)
23611be35a1SLionel Sambuc {
23711be35a1SLionel Sambuc     return 0;
23811be35a1SLionel Sambuc }
23911be35a1SLionel Sambuc 
24011be35a1SLionel Sambuc ATF_TEST_CASE(h_check_errno);
ATF_TEST_CASE_HEAD(h_check_errno)24111be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_check_errno)
24211be35a1SLionel Sambuc {
24311be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
24411be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_check_errno)24511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_check_errno)
24611be35a1SLionel Sambuc {
24711be35a1SLionel Sambuc     create_ctl_file("before");
24811be35a1SLionel Sambuc 
24911be35a1SLionel Sambuc     if (get_config_var("what") == "no_error")
25011be35a1SLionel Sambuc         ATF_CHECK_ERRNO(-1, errno_ok_stub() == -1);
25111be35a1SLionel Sambuc     else if (get_config_var("what") == "errno_ok")
25211be35a1SLionel Sambuc         ATF_CHECK_ERRNO(2, errno_fail_stub(2) == -1);
25311be35a1SLionel Sambuc     else if (get_config_var("what") == "errno_fail")
25411be35a1SLionel Sambuc         ATF_CHECK_ERRNO(3, errno_fail_stub(4) == -1);
25511be35a1SLionel Sambuc     else
25611be35a1SLionel Sambuc         UNREACHABLE;
25711be35a1SLionel Sambuc 
25811be35a1SLionel Sambuc     create_ctl_file("after");
25911be35a1SLionel Sambuc }
26011be35a1SLionel Sambuc 
26111be35a1SLionel Sambuc ATF_TEST_CASE(h_require_errno);
ATF_TEST_CASE_HEAD(h_require_errno)26211be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_require_errno)
26311be35a1SLionel Sambuc {
26411be35a1SLionel Sambuc     set_md_var("descr", "Helper test case");
26511be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_require_errno)26611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_require_errno)
26711be35a1SLionel Sambuc {
26811be35a1SLionel Sambuc     create_ctl_file("before");
26911be35a1SLionel Sambuc 
27011be35a1SLionel Sambuc     if (get_config_var("what") == "no_error")
27111be35a1SLionel Sambuc         ATF_REQUIRE_ERRNO(-1, errno_ok_stub() == -1);
27211be35a1SLionel Sambuc     else if (get_config_var("what") == "errno_ok")
27311be35a1SLionel Sambuc         ATF_REQUIRE_ERRNO(2, errno_fail_stub(2) == -1);
27411be35a1SLionel Sambuc     else if (get_config_var("what") == "errno_fail")
27511be35a1SLionel Sambuc         ATF_REQUIRE_ERRNO(3, errno_fail_stub(4) == -1);
27611be35a1SLionel Sambuc     else
27711be35a1SLionel Sambuc         UNREACHABLE;
27811be35a1SLionel Sambuc 
27911be35a1SLionel Sambuc     create_ctl_file("after");
28011be35a1SLionel Sambuc }
28111be35a1SLionel Sambuc 
28211be35a1SLionel Sambuc // ------------------------------------------------------------------------
28311be35a1SLionel Sambuc // Test cases for the macros.
28411be35a1SLionel Sambuc // ------------------------------------------------------------------------
28511be35a1SLionel Sambuc 
28611be35a1SLionel Sambuc ATF_TEST_CASE(pass);
ATF_TEST_CASE_HEAD(pass)28711be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(pass)
28811be35a1SLionel Sambuc {
28911be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_PASS macro");
29011be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(pass)29111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(pass)
29211be35a1SLionel Sambuc {
29311be35a1SLionel Sambuc     ATF_TEST_CASE_USE(h_pass);
29411be35a1SLionel Sambuc     run_h_tc< ATF_TEST_CASE_NAME(h_pass) >();
29511be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
29611be35a1SLionel Sambuc     ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
29711be35a1SLionel Sambuc     ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
29811be35a1SLionel Sambuc }
29911be35a1SLionel Sambuc 
30011be35a1SLionel Sambuc ATF_TEST_CASE(fail);
ATF_TEST_CASE_HEAD(fail)30111be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(fail)
30211be35a1SLionel Sambuc {
30311be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_FAIL macro");
30411be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(fail)30511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(fail)
30611be35a1SLionel Sambuc {
30711be35a1SLionel Sambuc     ATF_TEST_CASE_USE(h_fail);
30811be35a1SLionel Sambuc     run_h_tc< ATF_TEST_CASE_NAME(h_fail) >();
30911be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_file("^failed: Failed on purpose", "result"));
31011be35a1SLionel Sambuc     ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
31111be35a1SLionel Sambuc     ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
31211be35a1SLionel Sambuc }
31311be35a1SLionel Sambuc 
31411be35a1SLionel Sambuc ATF_TEST_CASE(skip);
ATF_TEST_CASE_HEAD(skip)31511be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(skip)
31611be35a1SLionel Sambuc {
31711be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_SKIP macro");
31811be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(skip)31911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(skip)
32011be35a1SLionel Sambuc {
32111be35a1SLionel Sambuc     ATF_TEST_CASE_USE(h_skip);
32211be35a1SLionel Sambuc     run_h_tc< ATF_TEST_CASE_NAME(h_skip) >();
32311be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_file("^skipped: Skipped on purpose",
32411be35a1SLionel Sambuc         "result"));
32511be35a1SLionel Sambuc     ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
32611be35a1SLionel Sambuc     ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
32711be35a1SLionel Sambuc }
32811be35a1SLionel Sambuc 
32911be35a1SLionel Sambuc ATF_TEST_CASE(require);
ATF_TEST_CASE_HEAD(require)33011be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require)
33111be35a1SLionel Sambuc {
33211be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE macro");
33311be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require)33411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require)
33511be35a1SLionel Sambuc {
33611be35a1SLionel Sambuc     struct test {
33711be35a1SLionel Sambuc         const char *cond;
33811be35a1SLionel Sambuc         bool ok;
33911be35a1SLionel Sambuc     } *t, tests[] = {
34011be35a1SLionel Sambuc         { "false", false },
34111be35a1SLionel Sambuc         { "true", true },
34211be35a1SLionel Sambuc         { NULL, false }
34311be35a1SLionel Sambuc     };
34411be35a1SLionel Sambuc 
34511be35a1SLionel Sambuc     const atf::fs::path before("before");
34611be35a1SLionel Sambuc     const atf::fs::path after("after");
34711be35a1SLionel Sambuc 
34811be35a1SLionel Sambuc     for (t = &tests[0]; t->cond != NULL; t++) {
34911be35a1SLionel Sambuc         atf::tests::vars_map config;
35011be35a1SLionel Sambuc         config["condition"] = t->cond;
35111be35a1SLionel Sambuc 
35211be35a1SLionel Sambuc         std::cout << "Checking with a " << t->cond << " value\n";
35311be35a1SLionel Sambuc 
35411be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require);
35511be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require) >(config);
35611be35a1SLionel Sambuc 
35711be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
35811be35a1SLionel Sambuc         if (t->ok) {
35911be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
36011be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
36111be35a1SLionel Sambuc         } else {
36211be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file(
36311be35a1SLionel Sambuc                 "^failed: .*condition not met", "result"));
36411be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
36511be35a1SLionel Sambuc         }
36611be35a1SLionel Sambuc 
36711be35a1SLionel Sambuc         atf::fs::remove(before);
36811be35a1SLionel Sambuc         if (t->ok)
36911be35a1SLionel Sambuc             atf::fs::remove(after);
37011be35a1SLionel Sambuc     }
37111be35a1SLionel Sambuc }
37211be35a1SLionel Sambuc 
37311be35a1SLionel Sambuc ATF_TEST_CASE(require_eq);
ATF_TEST_CASE_HEAD(require_eq)37411be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require_eq)
37511be35a1SLionel Sambuc {
37611be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE_EQ macro");
37711be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require_eq)37811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require_eq)
37911be35a1SLionel Sambuc {
38011be35a1SLionel Sambuc     struct test {
38111be35a1SLionel Sambuc         const char *v1;
38211be35a1SLionel Sambuc         const char *v2;
38311be35a1SLionel Sambuc         bool ok;
38411be35a1SLionel Sambuc     } *t, tests[] = {
38511be35a1SLionel Sambuc         { "1", "1", true },
38611be35a1SLionel Sambuc         { "1", "2", false },
38711be35a1SLionel Sambuc         { "2", "1", false },
38811be35a1SLionel Sambuc         { "2", "2", true },
38911be35a1SLionel Sambuc         { NULL, NULL, false }
39011be35a1SLionel Sambuc     };
39111be35a1SLionel Sambuc 
39211be35a1SLionel Sambuc     const atf::fs::path before("before");
39311be35a1SLionel Sambuc     const atf::fs::path after("after");
39411be35a1SLionel Sambuc 
39511be35a1SLionel Sambuc     for (t = &tests[0]; t->v1 != NULL; t++) {
39611be35a1SLionel Sambuc         atf::tests::vars_map config;
39711be35a1SLionel Sambuc         config["v1"] = t->v1;
39811be35a1SLionel Sambuc         config["v2"] = t->v2;
39911be35a1SLionel Sambuc 
40011be35a1SLionel Sambuc         std::cout << "Checking with " << t->v1 << ", " << t->v2
40111be35a1SLionel Sambuc                   << " and expecting " << (t->ok ? "true" : "false")
40211be35a1SLionel Sambuc                   << "\n";
40311be35a1SLionel Sambuc 
40411be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require_eq);
40511be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require_eq) >(config);
40611be35a1SLionel Sambuc 
40711be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
40811be35a1SLionel Sambuc         if (t->ok) {
40911be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
41011be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
41111be35a1SLionel Sambuc         } else {
41211be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^failed: .*v1 != v2", "result"));
41311be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
41411be35a1SLionel Sambuc         }
41511be35a1SLionel Sambuc 
41611be35a1SLionel Sambuc         atf::fs::remove(before);
41711be35a1SLionel Sambuc         if (t->ok)
41811be35a1SLionel Sambuc             atf::fs::remove(after);
41911be35a1SLionel Sambuc     }
42011be35a1SLionel Sambuc }
42111be35a1SLionel Sambuc 
42211be35a1SLionel Sambuc ATF_TEST_CASE(require_in);
ATF_TEST_CASE_HEAD(require_in)42311be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require_in)
42411be35a1SLionel Sambuc {
42511be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE_IN macro");
42611be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require_in)42711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require_in)
42811be35a1SLionel Sambuc {
42911be35a1SLionel Sambuc     struct test {
43011be35a1SLionel Sambuc         const char *value;
43111be35a1SLionel Sambuc         bool ok;
43211be35a1SLionel Sambuc     } *t, tests[] = {
43311be35a1SLionel Sambuc         { "foo", true },
43411be35a1SLionel Sambuc         { "bar", true },
43511be35a1SLionel Sambuc         { "baz", true },
43611be35a1SLionel Sambuc         { "xxx", false },
43711be35a1SLionel Sambuc         { "fooa", false },
43811be35a1SLionel Sambuc         { "foo ", false },
43911be35a1SLionel Sambuc         { NULL, false }
44011be35a1SLionel Sambuc     };
44111be35a1SLionel Sambuc 
44211be35a1SLionel Sambuc     const atf::fs::path before("before");
44311be35a1SLionel Sambuc     const atf::fs::path after("after");
44411be35a1SLionel Sambuc 
44511be35a1SLionel Sambuc     for (t = &tests[0]; t->value != NULL; t++) {
44611be35a1SLionel Sambuc         atf::tests::vars_map config;
44711be35a1SLionel Sambuc         config["value"] = t->value;
44811be35a1SLionel Sambuc 
44911be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require_in);
45011be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require_in) >(config);
45111be35a1SLionel Sambuc 
45211be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
45311be35a1SLionel Sambuc         if (t->ok) {
45411be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
45511be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
45611be35a1SLionel Sambuc         } else {
45711be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
45811be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
45911be35a1SLionel Sambuc         }
46011be35a1SLionel Sambuc 
46111be35a1SLionel Sambuc         atf::fs::remove(before);
46211be35a1SLionel Sambuc         if (t->ok)
46311be35a1SLionel Sambuc             atf::fs::remove(after);
46411be35a1SLionel Sambuc     }
46511be35a1SLionel Sambuc }
46611be35a1SLionel Sambuc 
46711be35a1SLionel Sambuc ATF_TEST_CASE(require_match);
ATF_TEST_CASE_HEAD(require_match)46811be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require_match)
46911be35a1SLionel Sambuc {
47011be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE_MATCH macro");
47111be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require_match)47211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require_match)
47311be35a1SLionel Sambuc {
47411be35a1SLionel Sambuc     struct test {
47511be35a1SLionel Sambuc         const char *regexp;
47611be35a1SLionel Sambuc         const char *string;
47711be35a1SLionel Sambuc         bool ok;
47811be35a1SLionel Sambuc     } *t, tests[] = {
47911be35a1SLionel Sambuc         { "foo.*bar", "this is a foo, bar, baz", true },
48011be35a1SLionel Sambuc         { "bar.*baz", "this is a baz, bar, foo", false },
48111be35a1SLionel Sambuc         { NULL, NULL, false }
48211be35a1SLionel Sambuc     };
48311be35a1SLionel Sambuc 
48411be35a1SLionel Sambuc     const atf::fs::path before("before");
48511be35a1SLionel Sambuc     const atf::fs::path after("after");
48611be35a1SLionel Sambuc 
48711be35a1SLionel Sambuc     for (t = &tests[0]; t->regexp != NULL; t++) {
48811be35a1SLionel Sambuc         atf::tests::vars_map config;
48911be35a1SLionel Sambuc         config["regexp"] = t->regexp;
49011be35a1SLionel Sambuc         config["string"] = t->string;
49111be35a1SLionel Sambuc 
49211be35a1SLionel Sambuc         std::cout << "Checking with " << t->regexp << ", " << t->string
49311be35a1SLionel Sambuc                   << " and expecting " << (t->ok ? "true" : "false")
49411be35a1SLionel Sambuc                   << "\n";
49511be35a1SLionel Sambuc 
49611be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require_match);
49711be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require_match) >(config);
49811be35a1SLionel Sambuc 
49911be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
50011be35a1SLionel Sambuc         if (t->ok) {
50111be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
50211be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
50311be35a1SLionel Sambuc         } else {
50411be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
50511be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
50611be35a1SLionel Sambuc         }
50711be35a1SLionel Sambuc 
50811be35a1SLionel Sambuc         atf::fs::remove(before);
50911be35a1SLionel Sambuc         if (t->ok)
51011be35a1SLionel Sambuc             atf::fs::remove(after);
51111be35a1SLionel Sambuc     }
51211be35a1SLionel Sambuc }
51311be35a1SLionel Sambuc 
51411be35a1SLionel Sambuc ATF_TEST_CASE(require_not_in);
ATF_TEST_CASE_HEAD(require_not_in)51511be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require_not_in)
51611be35a1SLionel Sambuc {
51711be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE_NOT_IN macro");
51811be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require_not_in)51911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require_not_in)
52011be35a1SLionel Sambuc {
52111be35a1SLionel Sambuc     struct test {
52211be35a1SLionel Sambuc         const char *value;
52311be35a1SLionel Sambuc         bool ok;
52411be35a1SLionel Sambuc     } *t, tests[] = {
52511be35a1SLionel Sambuc         { "foo", false },
52611be35a1SLionel Sambuc         { "bar", false },
52711be35a1SLionel Sambuc         { "baz", false },
52811be35a1SLionel Sambuc         { "xxx", true },
52911be35a1SLionel Sambuc         { "fooa", true },
53011be35a1SLionel Sambuc         { "foo ", true },
53111be35a1SLionel Sambuc         { NULL, false }
53211be35a1SLionel Sambuc     };
53311be35a1SLionel Sambuc 
53411be35a1SLionel Sambuc     const atf::fs::path before("before");
53511be35a1SLionel Sambuc     const atf::fs::path after("after");
53611be35a1SLionel Sambuc 
53711be35a1SLionel Sambuc     for (t = &tests[0]; t->value != NULL; t++) {
53811be35a1SLionel Sambuc         atf::tests::vars_map config;
53911be35a1SLionel Sambuc         config["value"] = t->value;
54011be35a1SLionel Sambuc 
54111be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require_not_in);
54211be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require_not_in) >(config);
54311be35a1SLionel Sambuc 
54411be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
54511be35a1SLionel Sambuc         if (t->ok) {
54611be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
54711be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
54811be35a1SLionel Sambuc         } else {
54911be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
55011be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
55111be35a1SLionel Sambuc         }
55211be35a1SLionel Sambuc 
55311be35a1SLionel Sambuc         atf::fs::remove(before);
55411be35a1SLionel Sambuc         if (t->ok)
55511be35a1SLionel Sambuc             atf::fs::remove(after);
55611be35a1SLionel Sambuc     }
55711be35a1SLionel Sambuc }
55811be35a1SLionel Sambuc 
55911be35a1SLionel Sambuc ATF_TEST_CASE(require_throw);
ATF_TEST_CASE_HEAD(require_throw)56011be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require_throw)
56111be35a1SLionel Sambuc {
56211be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE_THROW macro");
56311be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require_throw)56411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require_throw)
56511be35a1SLionel Sambuc {
56611be35a1SLionel Sambuc     struct test {
56711be35a1SLionel Sambuc         const char *what;
56811be35a1SLionel Sambuc         bool ok;
56911be35a1SLionel Sambuc         const char *msg;
57011be35a1SLionel Sambuc     } *t, tests[] = {
57111be35a1SLionel Sambuc         { "throw_int", false, "unexpected error" },
57211be35a1SLionel Sambuc         { "throw_rt", true, NULL },
57311be35a1SLionel Sambuc         { "no_throw_rt", false, "did not throw" },
57411be35a1SLionel Sambuc         { NULL, false, NULL }
57511be35a1SLionel Sambuc     };
57611be35a1SLionel Sambuc 
57711be35a1SLionel Sambuc     const atf::fs::path before("before");
57811be35a1SLionel Sambuc     const atf::fs::path after("after");
57911be35a1SLionel Sambuc 
58011be35a1SLionel Sambuc     for (t = &tests[0]; t->what != NULL; t++) {
58111be35a1SLionel Sambuc         atf::tests::vars_map config;
58211be35a1SLionel Sambuc         config["what"] = t->what;
58311be35a1SLionel Sambuc 
58411be35a1SLionel Sambuc         std::cout << "Checking with " << t->what << " and expecting "
58511be35a1SLionel Sambuc                   << (t->ok ? "true" : "false") << "\n";
58611be35a1SLionel Sambuc 
58711be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require_throw);
58811be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require_throw) >(config);
58911be35a1SLionel Sambuc 
59011be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
59111be35a1SLionel Sambuc         if (t->ok) {
59211be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
59311be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
59411be35a1SLionel Sambuc         } else {
59511be35a1SLionel Sambuc             std::cout << "Checking that message contains '" << t->msg
59611be35a1SLionel Sambuc                       << "'\n";
59711be35a1SLionel Sambuc             std::string exp_result = std::string("^failed: .*") + t->msg;
59811be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
59911be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
60011be35a1SLionel Sambuc         }
60111be35a1SLionel Sambuc 
60211be35a1SLionel Sambuc         atf::fs::remove(before);
60311be35a1SLionel Sambuc         if (t->ok)
60411be35a1SLionel Sambuc             atf::fs::remove(after);
60511be35a1SLionel Sambuc     }
60611be35a1SLionel Sambuc }
60711be35a1SLionel Sambuc 
60811be35a1SLionel Sambuc ATF_TEST_CASE(require_throw_re);
ATF_TEST_CASE_HEAD(require_throw_re)60911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require_throw_re)
61011be35a1SLionel Sambuc {
61111be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE_THROW_RE macro");
61211be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require_throw_re)61311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require_throw_re)
61411be35a1SLionel Sambuc {
61511be35a1SLionel Sambuc     struct test {
61611be35a1SLionel Sambuc         const char *what;
61711be35a1SLionel Sambuc         bool ok;
61811be35a1SLionel Sambuc         const char *msg;
61911be35a1SLionel Sambuc     } *t, tests[] = {
62011be35a1SLionel Sambuc         { "throw_int", false, "unexpected error" },
62111be35a1SLionel Sambuc         { "throw_rt_match", true, NULL },
62211be35a1SLionel Sambuc         { "throw_rt_no_match", false,
62311be35a1SLionel Sambuc           "threw.*runtime_error\\(baz foo bar a\\).*"
62411be35a1SLionel Sambuc           "does not match 'foo\\.\\*baz'" },
62511be35a1SLionel Sambuc         { "no_throw_rt", false, "did not throw" },
62611be35a1SLionel Sambuc         { NULL, false, NULL }
62711be35a1SLionel Sambuc     };
62811be35a1SLionel Sambuc 
62911be35a1SLionel Sambuc     const atf::fs::path before("before");
63011be35a1SLionel Sambuc     const atf::fs::path after("after");
63111be35a1SLionel Sambuc 
63211be35a1SLionel Sambuc     for (t = &tests[0]; t->what != NULL; t++) {
63311be35a1SLionel Sambuc         atf::tests::vars_map config;
63411be35a1SLionel Sambuc         config["what"] = t->what;
63511be35a1SLionel Sambuc 
63611be35a1SLionel Sambuc         std::cout << "Checking with " << t->what << " and expecting "
63711be35a1SLionel Sambuc                   << (t->ok ? "true" : "false") << "\n";
63811be35a1SLionel Sambuc 
63911be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require_throw_re);
64011be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require_throw_re) >(config);
64111be35a1SLionel Sambuc 
64211be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
64311be35a1SLionel Sambuc         if (t->ok) {
64411be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
64511be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
64611be35a1SLionel Sambuc         } else {
64711be35a1SLionel Sambuc             std::cout << "Checking that message contains '" << t->msg
64811be35a1SLionel Sambuc                       << "'\n";
64911be35a1SLionel Sambuc             std::string exp_result = std::string("^failed: .*") + t->msg;
65011be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
65111be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
65211be35a1SLionel Sambuc         }
65311be35a1SLionel Sambuc 
65411be35a1SLionel Sambuc         atf::fs::remove(before);
65511be35a1SLionel Sambuc         if (t->ok)
65611be35a1SLionel Sambuc             atf::fs::remove(after);
65711be35a1SLionel Sambuc     }
65811be35a1SLionel Sambuc }
65911be35a1SLionel Sambuc 
66011be35a1SLionel Sambuc ATF_TEST_CASE(check_errno);
ATF_TEST_CASE_HEAD(check_errno)66111be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(check_errno)
66211be35a1SLionel Sambuc {
66311be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_CHECK_ERRNO macro");
66411be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(check_errno)66511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(check_errno)
66611be35a1SLionel Sambuc {
66711be35a1SLionel Sambuc     struct test {
66811be35a1SLionel Sambuc         const char *what;
66911be35a1SLionel Sambuc         bool ok;
67011be35a1SLionel Sambuc         const char *msg;
67111be35a1SLionel Sambuc     } *t, tests[] = {
67211be35a1SLionel Sambuc         { "no_error", false,
67311be35a1SLionel Sambuc           "Expected true value in errno_ok_stub\\(\\) == -1" },
67411be35a1SLionel Sambuc         { "errno_ok", true, NULL },
67511be35a1SLionel Sambuc         { "errno_fail", false,
67611be35a1SLionel Sambuc           "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
67711be35a1SLionel Sambuc         { NULL, false, NULL }
67811be35a1SLionel Sambuc     };
67911be35a1SLionel Sambuc 
68011be35a1SLionel Sambuc     const atf::fs::path before("before");
68111be35a1SLionel Sambuc     const atf::fs::path after("after");
68211be35a1SLionel Sambuc 
68311be35a1SLionel Sambuc     for (t = &tests[0]; t->what != NULL; t++) {
68411be35a1SLionel Sambuc         atf::tests::vars_map config;
68511be35a1SLionel Sambuc         config["what"] = t->what;
68611be35a1SLionel Sambuc 
68711be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_check_errno);
68811be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_check_errno) >(config);
68911be35a1SLionel Sambuc 
69011be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
69111be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(after));
69211be35a1SLionel Sambuc 
69311be35a1SLionel Sambuc         if (t->ok) {
69411be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
69511be35a1SLionel Sambuc         } else {
69611be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^failed", "result"));
69711be35a1SLionel Sambuc 
69811be35a1SLionel Sambuc             std::string exp_result = "macros_test.cpp:[0-9]+: " +
69911be35a1SLionel Sambuc                 std::string(t->msg) + "$";
70011be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "stderr"));
70111be35a1SLionel Sambuc         }
70211be35a1SLionel Sambuc 
70311be35a1SLionel Sambuc         atf::fs::remove(before);
70411be35a1SLionel Sambuc         atf::fs::remove(after);
70511be35a1SLionel Sambuc     }
70611be35a1SLionel Sambuc }
70711be35a1SLionel Sambuc 
70811be35a1SLionel Sambuc ATF_TEST_CASE(require_errno);
ATF_TEST_CASE_HEAD(require_errno)70911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(require_errno)
71011be35a1SLionel Sambuc {
71111be35a1SLionel Sambuc     set_md_var("descr", "Tests the ATF_REQUIRE_ERRNO macro");
71211be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(require_errno)71311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(require_errno)
71411be35a1SLionel Sambuc {
71511be35a1SLionel Sambuc     struct test {
71611be35a1SLionel Sambuc         const char *what;
71711be35a1SLionel Sambuc         bool ok;
71811be35a1SLionel Sambuc         const char *msg;
71911be35a1SLionel Sambuc     } *t, tests[] = {
72011be35a1SLionel Sambuc         { "no_error", false,
72111be35a1SLionel Sambuc           "Expected true value in errno_ok_stub\\(\\) == -1" },
72211be35a1SLionel Sambuc         { "errno_ok", true, NULL },
72311be35a1SLionel Sambuc         { "errno_fail", false,
72411be35a1SLionel Sambuc           "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
72511be35a1SLionel Sambuc         { NULL, false, NULL }
72611be35a1SLionel Sambuc     };
72711be35a1SLionel Sambuc 
72811be35a1SLionel Sambuc     const atf::fs::path before("before");
72911be35a1SLionel Sambuc     const atf::fs::path after("after");
73011be35a1SLionel Sambuc 
73111be35a1SLionel Sambuc     for (t = &tests[0]; t->what != NULL; t++) {
73211be35a1SLionel Sambuc         atf::tests::vars_map config;
73311be35a1SLionel Sambuc         config["what"] = t->what;
73411be35a1SLionel Sambuc 
73511be35a1SLionel Sambuc         ATF_TEST_CASE_USE(h_require_errno);
73611be35a1SLionel Sambuc         run_h_tc< ATF_TEST_CASE_NAME(h_require_errno) >(config);
73711be35a1SLionel Sambuc 
73811be35a1SLionel Sambuc         ATF_REQUIRE(atf::fs::exists(before));
73911be35a1SLionel Sambuc         if (t->ok) {
74011be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
74111be35a1SLionel Sambuc             ATF_REQUIRE(atf::fs::exists(after));
74211be35a1SLionel Sambuc         } else {
74311be35a1SLionel Sambuc             std::string exp_result = "^failed: .*macros_test.cpp:[0-9]+: " +
74411be35a1SLionel Sambuc                 std::string(t->msg) + "$";
74511be35a1SLionel Sambuc             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
74611be35a1SLionel Sambuc 
74711be35a1SLionel Sambuc             ATF_REQUIRE(!atf::fs::exists(after));
74811be35a1SLionel Sambuc         }
74911be35a1SLionel Sambuc 
75011be35a1SLionel Sambuc         atf::fs::remove(before);
75111be35a1SLionel Sambuc         if (t->ok)
75211be35a1SLionel Sambuc             atf::fs::remove(after);
75311be35a1SLionel Sambuc     }
75411be35a1SLionel Sambuc }
75511be35a1SLionel Sambuc 
75611be35a1SLionel Sambuc // ------------------------------------------------------------------------
75711be35a1SLionel Sambuc // Tests cases for the header file.
75811be35a1SLionel Sambuc // ------------------------------------------------------------------------
75911be35a1SLionel Sambuc 
76011be35a1SLionel Sambuc HEADER_TC(include, "atf-c++/macros.hpp");
76111be35a1SLionel Sambuc BUILD_TC(use, "macros_hpp_test.cpp",
76211be35a1SLionel Sambuc          "Tests that the macros provided by the atf-c++/macros.hpp file "
76311be35a1SLionel Sambuc          "do not cause syntax errors when used",
76411be35a1SLionel Sambuc          "Build of macros_hpp_test.cpp failed; some macros in "
76511be35a1SLionel Sambuc          "atf-c++/macros.hpp are broken");
766*0a6a1f1dSLionel Sambuc 
767*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(detect_unused_tests);
ATF_TEST_CASE_HEAD(detect_unused_tests)768*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(detect_unused_tests)
769*0a6a1f1dSLionel Sambuc {
770*0a6a1f1dSLionel Sambuc     set_md_var("descr",
771*0a6a1f1dSLionel Sambuc                "Tests that defining an unused test case raises a warning (and "
772*0a6a1f1dSLionel Sambuc                "thus an error)");
773*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(detect_unused_tests)774*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(detect_unused_tests)
775*0a6a1f1dSLionel Sambuc {
776*0a6a1f1dSLionel Sambuc     const char* validate_compiler =
777*0a6a1f1dSLionel Sambuc         "class test_class { public: int dummy; };\n"
778*0a6a1f1dSLionel Sambuc         "#define define_unused static test_class unused\n"
779*0a6a1f1dSLionel Sambuc         "define_unused;\n";
780*0a6a1f1dSLionel Sambuc 
781*0a6a1f1dSLionel Sambuc     atf::utils::create_file("compiler_test.cpp", validate_compiler);
782*0a6a1f1dSLionel Sambuc     if (build_check_cxx_o("compiler_test.cpp"))
783*0a6a1f1dSLionel Sambuc         expect_fail("Compiler does not raise a warning on an unused "
784*0a6a1f1dSLionel Sambuc                     "static global variable declared by a macro");
785*0a6a1f1dSLionel Sambuc 
786*0a6a1f1dSLionel Sambuc #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
787*0a6a1f1dSLionel Sambuc         expect_fail("PR 49187");
788*0a6a1f1dSLionel Sambuc #endif
789*0a6a1f1dSLionel Sambuc 
790*0a6a1f1dSLionel Sambuc     if (build_check_cxx_o_srcdir(*this, "unused_test.cpp"))
791*0a6a1f1dSLionel Sambuc         ATF_FAIL("Build of unused_test.cpp passed; unused test cases are "
792*0a6a1f1dSLionel Sambuc                  "not properly detected");
793*0a6a1f1dSLionel Sambuc }
79411be35a1SLionel Sambuc 
79511be35a1SLionel Sambuc // ------------------------------------------------------------------------
79611be35a1SLionel Sambuc // Main.
79711be35a1SLionel Sambuc // ------------------------------------------------------------------------
79811be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)79911be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
80011be35a1SLionel Sambuc {
80111be35a1SLionel Sambuc     // Add the test cases for the macros.
80211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, pass);
80311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, fail);
80411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, skip);
80511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, check_errno);
80611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require);
80711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require_eq);
80811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require_in);
80911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require_match);
81011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require_not_in);
81111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require_throw);
81211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require_throw_re);
81311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, require_errno);
81411be35a1SLionel Sambuc 
81511be35a1SLionel Sambuc     // Add the test cases for the header file.
81611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, include);
81711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, use);
81811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, detect_unused_tests);
81911be35a1SLionel Sambuc }
820