xref: /freebsd/contrib/atf/atf-c++/macros_test.cpp (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1c243e490SMarcel Moolenaar // Copyright (c) 2008 The NetBSD Foundation, Inc.
2c243e490SMarcel Moolenaar // All rights reserved.
3c243e490SMarcel Moolenaar //
4c243e490SMarcel Moolenaar // Redistribution and use in source and binary forms, with or without
5c243e490SMarcel Moolenaar // modification, are permitted provided that the following conditions
6c243e490SMarcel Moolenaar // are met:
7c243e490SMarcel Moolenaar // 1. Redistributions of source code must retain the above copyright
8c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer.
9c243e490SMarcel Moolenaar // 2. Redistributions in binary form must reproduce the above copyright
10c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer in the
11c243e490SMarcel Moolenaar //    documentation and/or other materials provided with the distribution.
12c243e490SMarcel Moolenaar //
13c243e490SMarcel Moolenaar // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14c243e490SMarcel Moolenaar // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15c243e490SMarcel Moolenaar // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16c243e490SMarcel Moolenaar // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17c243e490SMarcel Moolenaar // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18c243e490SMarcel Moolenaar // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c243e490SMarcel Moolenaar // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20c243e490SMarcel Moolenaar // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21c243e490SMarcel Moolenaar // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22c243e490SMarcel Moolenaar // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23c243e490SMarcel Moolenaar // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24c243e490SMarcel Moolenaar // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*0677dfd1SJulio Merino 
26*0677dfd1SJulio Merino #include "atf-c++/macros.hpp"
27c243e490SMarcel Moolenaar 
28c243e490SMarcel Moolenaar extern "C" {
29c243e490SMarcel Moolenaar #include <fcntl.h>
30c243e490SMarcel Moolenaar #include <unistd.h>
31c243e490SMarcel Moolenaar }
32c243e490SMarcel Moolenaar 
33c243e490SMarcel Moolenaar #include <cerrno>
34c243e490SMarcel Moolenaar #include <cstdlib>
35c243e490SMarcel Moolenaar #include <iostream>
36c243e490SMarcel Moolenaar #include <stdexcept>
37c243e490SMarcel Moolenaar 
38*0677dfd1SJulio Merino #include <atf-c++.hpp>
39c243e490SMarcel Moolenaar 
40*0677dfd1SJulio Merino #include "atf-c++/detail/fs.hpp"
41*0677dfd1SJulio Merino #include "atf-c++/detail/process.hpp"
42*0677dfd1SJulio Merino #include "atf-c++/detail/sanity.hpp"
43*0677dfd1SJulio Merino #include "atf-c++/detail/test_helpers.hpp"
44*0677dfd1SJulio Merino #include "atf-c++/detail/text.hpp"
45*0677dfd1SJulio Merino #include "atf-c++/utils.hpp"
46c243e490SMarcel Moolenaar 
47c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
48c243e490SMarcel Moolenaar // Auxiliary functions.
49c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
50c243e490SMarcel Moolenaar 
51c243e490SMarcel Moolenaar static
52c243e490SMarcel Moolenaar void
create_ctl_file(const char * name)53c243e490SMarcel Moolenaar create_ctl_file(const char *name)
54c243e490SMarcel Moolenaar {
55c243e490SMarcel Moolenaar     ATF_REQUIRE(open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644) != -1);
56c243e490SMarcel Moolenaar }
57c243e490SMarcel Moolenaar 
58c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
59c243e490SMarcel Moolenaar // Auxiliary test cases.
60c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
61c243e490SMarcel Moolenaar 
62c243e490SMarcel Moolenaar ATF_TEST_CASE(h_pass);
ATF_TEST_CASE_HEAD(h_pass)63c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_pass)
64c243e490SMarcel Moolenaar {
65c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
66c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_pass)67c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_pass)
68c243e490SMarcel Moolenaar {
69c243e490SMarcel Moolenaar     create_ctl_file("before");
70c243e490SMarcel Moolenaar     ATF_PASS();
71c243e490SMarcel Moolenaar     create_ctl_file("after");
72c243e490SMarcel Moolenaar }
73c243e490SMarcel Moolenaar 
74c243e490SMarcel Moolenaar ATF_TEST_CASE(h_fail);
ATF_TEST_CASE_HEAD(h_fail)75c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_fail)
76c243e490SMarcel Moolenaar {
77c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
78c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_fail)79c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_fail)
80c243e490SMarcel Moolenaar {
81c243e490SMarcel Moolenaar     create_ctl_file("before");
82c243e490SMarcel Moolenaar     ATF_FAIL("Failed on purpose");
83c243e490SMarcel Moolenaar     create_ctl_file("after");
84c243e490SMarcel Moolenaar }
85c243e490SMarcel Moolenaar 
86c243e490SMarcel Moolenaar ATF_TEST_CASE(h_skip);
ATF_TEST_CASE_HEAD(h_skip)87c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_skip)
88c243e490SMarcel Moolenaar {
89c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
90c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_skip)91c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_skip)
92c243e490SMarcel Moolenaar {
93c243e490SMarcel Moolenaar     create_ctl_file("before");
94c243e490SMarcel Moolenaar     ATF_SKIP("Skipped on purpose");
95c243e490SMarcel Moolenaar     create_ctl_file("after");
96c243e490SMarcel Moolenaar }
97c243e490SMarcel Moolenaar 
98c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require);
ATF_TEST_CASE_HEAD(h_require)99c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require)
100c243e490SMarcel Moolenaar {
101c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
102c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require)103c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require)
104c243e490SMarcel Moolenaar {
105c243e490SMarcel Moolenaar     bool condition = atf::text::to_bool(get_config_var("condition"));
106c243e490SMarcel Moolenaar 
107c243e490SMarcel Moolenaar     create_ctl_file("before");
108c243e490SMarcel Moolenaar     ATF_REQUIRE(condition);
109c243e490SMarcel Moolenaar     create_ctl_file("after");
110c243e490SMarcel Moolenaar }
111c243e490SMarcel Moolenaar 
112c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require_eq);
ATF_TEST_CASE_HEAD(h_require_eq)113c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require_eq)
114c243e490SMarcel Moolenaar {
115c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
116c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require_eq)117c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require_eq)
118c243e490SMarcel Moolenaar {
119c243e490SMarcel Moolenaar     long v1 = atf::text::to_type< long >(get_config_var("v1"));
120c243e490SMarcel Moolenaar     long v2 = atf::text::to_type< long >(get_config_var("v2"));
121c243e490SMarcel Moolenaar 
122c243e490SMarcel Moolenaar     create_ctl_file("before");
123c243e490SMarcel Moolenaar     ATF_REQUIRE_EQ(v1, v2);
124c243e490SMarcel Moolenaar     create_ctl_file("after");
125c243e490SMarcel Moolenaar }
126c243e490SMarcel Moolenaar 
127c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require_in);
ATF_TEST_CASE_HEAD(h_require_in)128c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require_in)
129c243e490SMarcel Moolenaar {
130c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
131c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require_in)132c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require_in)
133c243e490SMarcel Moolenaar {
134c243e490SMarcel Moolenaar     const std::string element = get_config_var("value");
135c243e490SMarcel Moolenaar 
136c243e490SMarcel Moolenaar     std::set< std::string > collection;
137c243e490SMarcel Moolenaar     collection.insert("foo");
138c243e490SMarcel Moolenaar     collection.insert("bar");
139c243e490SMarcel Moolenaar     collection.insert("baz");
140c243e490SMarcel Moolenaar 
141c243e490SMarcel Moolenaar     create_ctl_file("before");
142c243e490SMarcel Moolenaar     ATF_REQUIRE_IN(element, collection);
143c243e490SMarcel Moolenaar     create_ctl_file("after");
144c243e490SMarcel Moolenaar }
145c243e490SMarcel Moolenaar 
146c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require_match);
ATF_TEST_CASE_HEAD(h_require_match)147c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require_match)
148c243e490SMarcel Moolenaar {
149c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
150c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require_match)151c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require_match)
152c243e490SMarcel Moolenaar {
153c243e490SMarcel Moolenaar     const std::string regexp = get_config_var("regexp");
154c243e490SMarcel Moolenaar     const std::string string = get_config_var("string");
155c243e490SMarcel Moolenaar 
156c243e490SMarcel Moolenaar     create_ctl_file("before");
157c243e490SMarcel Moolenaar     ATF_REQUIRE_MATCH(regexp, string);
158c243e490SMarcel Moolenaar     create_ctl_file("after");
159c243e490SMarcel Moolenaar }
160c243e490SMarcel Moolenaar 
161c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require_not_in);
ATF_TEST_CASE_HEAD(h_require_not_in)162c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require_not_in)
163c243e490SMarcel Moolenaar {
164c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
165c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require_not_in)166c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require_not_in)
167c243e490SMarcel Moolenaar {
168c243e490SMarcel Moolenaar     const std::string element = get_config_var("value");
169c243e490SMarcel Moolenaar 
170c243e490SMarcel Moolenaar     std::set< std::string > collection;
171c243e490SMarcel Moolenaar     collection.insert("foo");
172c243e490SMarcel Moolenaar     collection.insert("bar");
173c243e490SMarcel Moolenaar     collection.insert("baz");
174c243e490SMarcel Moolenaar 
175c243e490SMarcel Moolenaar     create_ctl_file("before");
176c243e490SMarcel Moolenaar     ATF_REQUIRE_NOT_IN(element, collection);
177c243e490SMarcel Moolenaar     create_ctl_file("after");
178c243e490SMarcel Moolenaar }
179c243e490SMarcel Moolenaar 
180c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require_throw);
ATF_TEST_CASE_HEAD(h_require_throw)181c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require_throw)
182c243e490SMarcel Moolenaar {
183c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
184c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require_throw)185c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require_throw)
186c243e490SMarcel Moolenaar {
187c243e490SMarcel Moolenaar     create_ctl_file("before");
188c243e490SMarcel Moolenaar 
189c243e490SMarcel Moolenaar     if (get_config_var("what") == "throw_int")
190c243e490SMarcel Moolenaar         ATF_REQUIRE_THROW(std::runtime_error, if (1) throw int(5));
191c243e490SMarcel Moolenaar     else if (get_config_var("what") == "throw_rt")
192c243e490SMarcel Moolenaar         ATF_REQUIRE_THROW(std::runtime_error,
193c243e490SMarcel Moolenaar                         if (1) throw std::runtime_error("e"));
194c243e490SMarcel Moolenaar     else if (get_config_var("what") == "no_throw_rt")
195c243e490SMarcel Moolenaar         ATF_REQUIRE_THROW(std::runtime_error,
196c243e490SMarcel Moolenaar                         if (0) throw std::runtime_error("e"));
197c243e490SMarcel Moolenaar 
198c243e490SMarcel Moolenaar     create_ctl_file("after");
199c243e490SMarcel Moolenaar }
200c243e490SMarcel Moolenaar 
201c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require_throw_re);
ATF_TEST_CASE_HEAD(h_require_throw_re)202c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require_throw_re)
203c243e490SMarcel Moolenaar {
204c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
205c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require_throw_re)206c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require_throw_re)
207c243e490SMarcel Moolenaar {
208c243e490SMarcel Moolenaar     create_ctl_file("before");
209c243e490SMarcel Moolenaar 
210c243e490SMarcel Moolenaar     if (get_config_var("what") == "throw_int")
211c243e490SMarcel Moolenaar         ATF_REQUIRE_THROW_RE(std::runtime_error, "5", if (1) throw int(5));
212c243e490SMarcel Moolenaar     else if (get_config_var("what") == "throw_rt_match")
213c243e490SMarcel Moolenaar         ATF_REQUIRE_THROW_RE(std::runtime_error, "foo.*baz",
214c243e490SMarcel Moolenaar                              if (1) throw std::runtime_error("a foo bar baz"));
215c243e490SMarcel Moolenaar     else if (get_config_var("what") == "throw_rt_no_match")
216c243e490SMarcel Moolenaar         ATF_REQUIRE_THROW_RE(std::runtime_error, "foo.*baz",
217c243e490SMarcel Moolenaar                              if (1) throw std::runtime_error("baz foo bar a"));
218c243e490SMarcel Moolenaar     else if (get_config_var("what") == "no_throw_rt")
219c243e490SMarcel Moolenaar         ATF_REQUIRE_THROW_RE(std::runtime_error, "e",
220c243e490SMarcel Moolenaar                              if (0) throw std::runtime_error("e"));
221c243e490SMarcel Moolenaar 
222c243e490SMarcel Moolenaar     create_ctl_file("after");
223c243e490SMarcel Moolenaar }
224c243e490SMarcel Moolenaar 
225c243e490SMarcel Moolenaar static int
errno_fail_stub(const int raised_errno)226c243e490SMarcel Moolenaar errno_fail_stub(const int raised_errno)
227c243e490SMarcel Moolenaar {
228c243e490SMarcel Moolenaar     errno = raised_errno;
229c243e490SMarcel Moolenaar     return -1;
230c243e490SMarcel Moolenaar }
231c243e490SMarcel Moolenaar 
232c243e490SMarcel Moolenaar static int
errno_ok_stub(void)233c243e490SMarcel Moolenaar errno_ok_stub(void)
234c243e490SMarcel Moolenaar {
235c243e490SMarcel Moolenaar     return 0;
236c243e490SMarcel Moolenaar }
237c243e490SMarcel Moolenaar 
238c243e490SMarcel Moolenaar ATF_TEST_CASE(h_check_errno);
ATF_TEST_CASE_HEAD(h_check_errno)239c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_check_errno)
240c243e490SMarcel Moolenaar {
241c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
242c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_check_errno)243c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_check_errno)
244c243e490SMarcel Moolenaar {
245c243e490SMarcel Moolenaar     create_ctl_file("before");
246c243e490SMarcel Moolenaar 
247c243e490SMarcel Moolenaar     if (get_config_var("what") == "no_error")
248c243e490SMarcel Moolenaar         ATF_CHECK_ERRNO(-1, errno_ok_stub() == -1);
249c243e490SMarcel Moolenaar     else if (get_config_var("what") == "errno_ok")
250c243e490SMarcel Moolenaar         ATF_CHECK_ERRNO(2, errno_fail_stub(2) == -1);
251c243e490SMarcel Moolenaar     else if (get_config_var("what") == "errno_fail")
252c243e490SMarcel Moolenaar         ATF_CHECK_ERRNO(3, errno_fail_stub(4) == -1);
253c243e490SMarcel Moolenaar     else
254c243e490SMarcel Moolenaar         UNREACHABLE;
255c243e490SMarcel Moolenaar 
256c243e490SMarcel Moolenaar     create_ctl_file("after");
257c243e490SMarcel Moolenaar }
258c243e490SMarcel Moolenaar 
259c243e490SMarcel Moolenaar ATF_TEST_CASE(h_require_errno);
ATF_TEST_CASE_HEAD(h_require_errno)260c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(h_require_errno)
261c243e490SMarcel Moolenaar {
262c243e490SMarcel Moolenaar     set_md_var("descr", "Helper test case");
263c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(h_require_errno)264c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(h_require_errno)
265c243e490SMarcel Moolenaar {
266c243e490SMarcel Moolenaar     create_ctl_file("before");
267c243e490SMarcel Moolenaar 
268c243e490SMarcel Moolenaar     if (get_config_var("what") == "no_error")
269c243e490SMarcel Moolenaar         ATF_REQUIRE_ERRNO(-1, errno_ok_stub() == -1);
270c243e490SMarcel Moolenaar     else if (get_config_var("what") == "errno_ok")
271c243e490SMarcel Moolenaar         ATF_REQUIRE_ERRNO(2, errno_fail_stub(2) == -1);
272c243e490SMarcel Moolenaar     else if (get_config_var("what") == "errno_fail")
273c243e490SMarcel Moolenaar         ATF_REQUIRE_ERRNO(3, errno_fail_stub(4) == -1);
274c243e490SMarcel Moolenaar     else
275c243e490SMarcel Moolenaar         UNREACHABLE;
276c243e490SMarcel Moolenaar 
277c243e490SMarcel Moolenaar     create_ctl_file("after");
278c243e490SMarcel Moolenaar }
279c243e490SMarcel Moolenaar 
280c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
281c243e490SMarcel Moolenaar // Test cases for the macros.
282c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
283c243e490SMarcel Moolenaar 
284c243e490SMarcel Moolenaar ATF_TEST_CASE(pass);
ATF_TEST_CASE_HEAD(pass)285c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(pass)
286c243e490SMarcel Moolenaar {
287c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_PASS macro");
288c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(pass)289c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(pass)
290c243e490SMarcel Moolenaar {
291c243e490SMarcel Moolenaar     ATF_TEST_CASE_USE(h_pass);
292c243e490SMarcel Moolenaar     run_h_tc< ATF_TEST_CASE_NAME(h_pass) >();
293a18eacbeSJulio Merino     ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
294c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
295c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
296c243e490SMarcel Moolenaar }
297c243e490SMarcel Moolenaar 
298c243e490SMarcel Moolenaar ATF_TEST_CASE(fail);
ATF_TEST_CASE_HEAD(fail)299c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(fail)
300c243e490SMarcel Moolenaar {
301c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_FAIL macro");
302c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(fail)303c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(fail)
304c243e490SMarcel Moolenaar {
305c243e490SMarcel Moolenaar     ATF_TEST_CASE_USE(h_fail);
306c243e490SMarcel Moolenaar     run_h_tc< ATF_TEST_CASE_NAME(h_fail) >();
307a18eacbeSJulio Merino     ATF_REQUIRE(atf::utils::grep_file("^failed: Failed on purpose", "result"));
308c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
309c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
310c243e490SMarcel Moolenaar }
311c243e490SMarcel Moolenaar 
312c243e490SMarcel Moolenaar ATF_TEST_CASE(skip);
ATF_TEST_CASE_HEAD(skip)313c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(skip)
314c243e490SMarcel Moolenaar {
315c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_SKIP macro");
316c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(skip)317c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(skip)
318c243e490SMarcel Moolenaar {
319c243e490SMarcel Moolenaar     ATF_TEST_CASE_USE(h_skip);
320c243e490SMarcel Moolenaar     run_h_tc< ATF_TEST_CASE_NAME(h_skip) >();
321a18eacbeSJulio Merino     ATF_REQUIRE(atf::utils::grep_file("^skipped: Skipped on purpose",
322a18eacbeSJulio Merino         "result"));
323c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
324c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
325c243e490SMarcel Moolenaar }
326c243e490SMarcel Moolenaar 
327c243e490SMarcel Moolenaar ATF_TEST_CASE(require);
ATF_TEST_CASE_HEAD(require)328c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require)
329c243e490SMarcel Moolenaar {
330c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE macro");
331c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require)332c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require)
333c243e490SMarcel Moolenaar {
334c243e490SMarcel Moolenaar     struct test {
335c243e490SMarcel Moolenaar         const char *cond;
336c243e490SMarcel Moolenaar         bool ok;
337c243e490SMarcel Moolenaar     } *t, tests[] = {
338c243e490SMarcel Moolenaar         { "false", false },
339c243e490SMarcel Moolenaar         { "true", true },
340c243e490SMarcel Moolenaar         { NULL, false }
341c243e490SMarcel Moolenaar     };
342c243e490SMarcel Moolenaar 
343c243e490SMarcel Moolenaar     const atf::fs::path before("before");
344c243e490SMarcel Moolenaar     const atf::fs::path after("after");
345c243e490SMarcel Moolenaar 
346c243e490SMarcel Moolenaar     for (t = &tests[0]; t->cond != NULL; t++) {
347c243e490SMarcel Moolenaar         atf::tests::vars_map config;
348c243e490SMarcel Moolenaar         config["condition"] = t->cond;
349c243e490SMarcel Moolenaar 
350c243e490SMarcel Moolenaar         std::cout << "Checking with a " << t->cond << " value\n";
351c243e490SMarcel Moolenaar 
352c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require);
353c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require) >(config);
354c243e490SMarcel Moolenaar 
355c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
356c243e490SMarcel Moolenaar         if (t->ok) {
357a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
358c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
359c243e490SMarcel Moolenaar         } else {
360a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file(
361a18eacbeSJulio Merino                 "^failed: .*condition not met", "result"));
362c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
363c243e490SMarcel Moolenaar         }
364c243e490SMarcel Moolenaar 
365c243e490SMarcel Moolenaar         atf::fs::remove(before);
366c243e490SMarcel Moolenaar         if (t->ok)
367c243e490SMarcel Moolenaar             atf::fs::remove(after);
368c243e490SMarcel Moolenaar     }
369c243e490SMarcel Moolenaar }
370c243e490SMarcel Moolenaar 
371c243e490SMarcel Moolenaar ATF_TEST_CASE(require_eq);
ATF_TEST_CASE_HEAD(require_eq)372c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require_eq)
373c243e490SMarcel Moolenaar {
374c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE_EQ macro");
375c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require_eq)376c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require_eq)
377c243e490SMarcel Moolenaar {
378c243e490SMarcel Moolenaar     struct test {
379c243e490SMarcel Moolenaar         const char *v1;
380c243e490SMarcel Moolenaar         const char *v2;
381c243e490SMarcel Moolenaar         bool ok;
382c243e490SMarcel Moolenaar     } *t, tests[] = {
383c243e490SMarcel Moolenaar         { "1", "1", true },
384c243e490SMarcel Moolenaar         { "1", "2", false },
385c243e490SMarcel Moolenaar         { "2", "1", false },
386c243e490SMarcel Moolenaar         { "2", "2", true },
387c243e490SMarcel Moolenaar         { NULL, NULL, false }
388c243e490SMarcel Moolenaar     };
389c243e490SMarcel Moolenaar 
390c243e490SMarcel Moolenaar     const atf::fs::path before("before");
391c243e490SMarcel Moolenaar     const atf::fs::path after("after");
392c243e490SMarcel Moolenaar 
393c243e490SMarcel Moolenaar     for (t = &tests[0]; t->v1 != NULL; t++) {
394c243e490SMarcel Moolenaar         atf::tests::vars_map config;
395c243e490SMarcel Moolenaar         config["v1"] = t->v1;
396c243e490SMarcel Moolenaar         config["v2"] = t->v2;
397c243e490SMarcel Moolenaar 
398c243e490SMarcel Moolenaar         std::cout << "Checking with " << t->v1 << ", " << t->v2
399c243e490SMarcel Moolenaar                   << " and expecting " << (t->ok ? "true" : "false")
400c243e490SMarcel Moolenaar                   << "\n";
401c243e490SMarcel Moolenaar 
402c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require_eq);
403c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require_eq) >(config);
404c243e490SMarcel Moolenaar 
405c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
406c243e490SMarcel Moolenaar         if (t->ok) {
407a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
408c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
409c243e490SMarcel Moolenaar         } else {
410a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^failed: .*v1 != v2", "result"));
411c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
412c243e490SMarcel Moolenaar         }
413c243e490SMarcel Moolenaar 
414c243e490SMarcel Moolenaar         atf::fs::remove(before);
415c243e490SMarcel Moolenaar         if (t->ok)
416c243e490SMarcel Moolenaar             atf::fs::remove(after);
417c243e490SMarcel Moolenaar     }
418c243e490SMarcel Moolenaar }
419c243e490SMarcel Moolenaar 
420c243e490SMarcel Moolenaar ATF_TEST_CASE(require_in);
ATF_TEST_CASE_HEAD(require_in)421c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require_in)
422c243e490SMarcel Moolenaar {
423c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE_IN macro");
424c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require_in)425c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require_in)
426c243e490SMarcel Moolenaar {
427c243e490SMarcel Moolenaar     struct test {
428c243e490SMarcel Moolenaar         const char *value;
429c243e490SMarcel Moolenaar         bool ok;
430c243e490SMarcel Moolenaar     } *t, tests[] = {
431c243e490SMarcel Moolenaar         { "foo", true },
432c243e490SMarcel Moolenaar         { "bar", true },
433c243e490SMarcel Moolenaar         { "baz", true },
434c243e490SMarcel Moolenaar         { "xxx", false },
435c243e490SMarcel Moolenaar         { "fooa", false },
436c243e490SMarcel Moolenaar         { "foo ", false },
437c243e490SMarcel Moolenaar         { NULL, false }
438c243e490SMarcel Moolenaar     };
439c243e490SMarcel Moolenaar 
440c243e490SMarcel Moolenaar     const atf::fs::path before("before");
441c243e490SMarcel Moolenaar     const atf::fs::path after("after");
442c243e490SMarcel Moolenaar 
443c243e490SMarcel Moolenaar     for (t = &tests[0]; t->value != NULL; t++) {
444c243e490SMarcel Moolenaar         atf::tests::vars_map config;
445c243e490SMarcel Moolenaar         config["value"] = t->value;
446c243e490SMarcel Moolenaar 
447c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require_in);
448c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require_in) >(config);
449c243e490SMarcel Moolenaar 
450c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
451c243e490SMarcel Moolenaar         if (t->ok) {
452a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
453c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
454c243e490SMarcel Moolenaar         } else {
455a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
456c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
457c243e490SMarcel Moolenaar         }
458c243e490SMarcel Moolenaar 
459c243e490SMarcel Moolenaar         atf::fs::remove(before);
460c243e490SMarcel Moolenaar         if (t->ok)
461c243e490SMarcel Moolenaar             atf::fs::remove(after);
462c243e490SMarcel Moolenaar     }
463c243e490SMarcel Moolenaar }
464c243e490SMarcel Moolenaar 
465c243e490SMarcel Moolenaar ATF_TEST_CASE(require_match);
ATF_TEST_CASE_HEAD(require_match)466c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require_match)
467c243e490SMarcel Moolenaar {
468c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE_MATCH macro");
469c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require_match)470c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require_match)
471c243e490SMarcel Moolenaar {
472c243e490SMarcel Moolenaar     struct test {
473c243e490SMarcel Moolenaar         const char *regexp;
474c243e490SMarcel Moolenaar         const char *string;
475c243e490SMarcel Moolenaar         bool ok;
476c243e490SMarcel Moolenaar     } *t, tests[] = {
477c243e490SMarcel Moolenaar         { "foo.*bar", "this is a foo, bar, baz", true },
478c243e490SMarcel Moolenaar         { "bar.*baz", "this is a baz, bar, foo", false },
479c243e490SMarcel Moolenaar         { NULL, NULL, false }
480c243e490SMarcel Moolenaar     };
481c243e490SMarcel Moolenaar 
482c243e490SMarcel Moolenaar     const atf::fs::path before("before");
483c243e490SMarcel Moolenaar     const atf::fs::path after("after");
484c243e490SMarcel Moolenaar 
485c243e490SMarcel Moolenaar     for (t = &tests[0]; t->regexp != NULL; t++) {
486c243e490SMarcel Moolenaar         atf::tests::vars_map config;
487c243e490SMarcel Moolenaar         config["regexp"] = t->regexp;
488c243e490SMarcel Moolenaar         config["string"] = t->string;
489c243e490SMarcel Moolenaar 
490c243e490SMarcel Moolenaar         std::cout << "Checking with " << t->regexp << ", " << t->string
491c243e490SMarcel Moolenaar                   << " and expecting " << (t->ok ? "true" : "false")
492c243e490SMarcel Moolenaar                   << "\n";
493c243e490SMarcel Moolenaar 
494c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require_match);
495c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require_match) >(config);
496c243e490SMarcel Moolenaar 
497c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
498c243e490SMarcel Moolenaar         if (t->ok) {
499a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
500c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
501c243e490SMarcel Moolenaar         } else {
502a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
503c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
504c243e490SMarcel Moolenaar         }
505c243e490SMarcel Moolenaar 
506c243e490SMarcel Moolenaar         atf::fs::remove(before);
507c243e490SMarcel Moolenaar         if (t->ok)
508c243e490SMarcel Moolenaar             atf::fs::remove(after);
509c243e490SMarcel Moolenaar     }
510c243e490SMarcel Moolenaar }
511c243e490SMarcel Moolenaar 
512c243e490SMarcel Moolenaar ATF_TEST_CASE(require_not_in);
ATF_TEST_CASE_HEAD(require_not_in)513c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require_not_in)
514c243e490SMarcel Moolenaar {
515c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE_NOT_IN macro");
516c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require_not_in)517c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require_not_in)
518c243e490SMarcel Moolenaar {
519c243e490SMarcel Moolenaar     struct test {
520c243e490SMarcel Moolenaar         const char *value;
521c243e490SMarcel Moolenaar         bool ok;
522c243e490SMarcel Moolenaar     } *t, tests[] = {
523c243e490SMarcel Moolenaar         { "foo", false },
524c243e490SMarcel Moolenaar         { "bar", false },
525c243e490SMarcel Moolenaar         { "baz", false },
526c243e490SMarcel Moolenaar         { "xxx", true },
527c243e490SMarcel Moolenaar         { "fooa", true },
528c243e490SMarcel Moolenaar         { "foo ", true },
529c243e490SMarcel Moolenaar         { NULL, false }
530c243e490SMarcel Moolenaar     };
531c243e490SMarcel Moolenaar 
532c243e490SMarcel Moolenaar     const atf::fs::path before("before");
533c243e490SMarcel Moolenaar     const atf::fs::path after("after");
534c243e490SMarcel Moolenaar 
535c243e490SMarcel Moolenaar     for (t = &tests[0]; t->value != NULL; t++) {
536c243e490SMarcel Moolenaar         atf::tests::vars_map config;
537c243e490SMarcel Moolenaar         config["value"] = t->value;
538c243e490SMarcel Moolenaar 
539c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require_not_in);
540c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require_not_in) >(config);
541c243e490SMarcel Moolenaar 
542c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
543c243e490SMarcel Moolenaar         if (t->ok) {
544a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
545c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
546c243e490SMarcel Moolenaar         } else {
547a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
548c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
549c243e490SMarcel Moolenaar         }
550c243e490SMarcel Moolenaar 
551c243e490SMarcel Moolenaar         atf::fs::remove(before);
552c243e490SMarcel Moolenaar         if (t->ok)
553c243e490SMarcel Moolenaar             atf::fs::remove(after);
554c243e490SMarcel Moolenaar     }
555c243e490SMarcel Moolenaar }
556c243e490SMarcel Moolenaar 
557c243e490SMarcel Moolenaar ATF_TEST_CASE(require_throw);
ATF_TEST_CASE_HEAD(require_throw)558c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require_throw)
559c243e490SMarcel Moolenaar {
560c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE_THROW macro");
561c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require_throw)562c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require_throw)
563c243e490SMarcel Moolenaar {
564c243e490SMarcel Moolenaar     struct test {
565c243e490SMarcel Moolenaar         const char *what;
566c243e490SMarcel Moolenaar         bool ok;
567c243e490SMarcel Moolenaar         const char *msg;
568c243e490SMarcel Moolenaar     } *t, tests[] = {
569c243e490SMarcel Moolenaar         { "throw_int", false, "unexpected error" },
570c243e490SMarcel Moolenaar         { "throw_rt", true, NULL },
571c243e490SMarcel Moolenaar         { "no_throw_rt", false, "did not throw" },
572c243e490SMarcel Moolenaar         { NULL, false, NULL }
573c243e490SMarcel Moolenaar     };
574c243e490SMarcel Moolenaar 
575c243e490SMarcel Moolenaar     const atf::fs::path before("before");
576c243e490SMarcel Moolenaar     const atf::fs::path after("after");
577c243e490SMarcel Moolenaar 
578c243e490SMarcel Moolenaar     for (t = &tests[0]; t->what != NULL; t++) {
579c243e490SMarcel Moolenaar         atf::tests::vars_map config;
580c243e490SMarcel Moolenaar         config["what"] = t->what;
581c243e490SMarcel Moolenaar 
582c243e490SMarcel Moolenaar         std::cout << "Checking with " << t->what << " and expecting "
583c243e490SMarcel Moolenaar                   << (t->ok ? "true" : "false") << "\n";
584c243e490SMarcel Moolenaar 
585c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require_throw);
586c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require_throw) >(config);
587c243e490SMarcel Moolenaar 
588c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
589c243e490SMarcel Moolenaar         if (t->ok) {
590a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
591c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
592c243e490SMarcel Moolenaar         } else {
593c243e490SMarcel Moolenaar             std::cout << "Checking that message contains '" << t->msg
594c243e490SMarcel Moolenaar                       << "'\n";
595c243e490SMarcel Moolenaar             std::string exp_result = std::string("^failed: .*") + t->msg;
596a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
597c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
598c243e490SMarcel Moolenaar         }
599c243e490SMarcel Moolenaar 
600c243e490SMarcel Moolenaar         atf::fs::remove(before);
601c243e490SMarcel Moolenaar         if (t->ok)
602c243e490SMarcel Moolenaar             atf::fs::remove(after);
603c243e490SMarcel Moolenaar     }
604c243e490SMarcel Moolenaar }
605c243e490SMarcel Moolenaar 
606c243e490SMarcel Moolenaar ATF_TEST_CASE(require_throw_re);
ATF_TEST_CASE_HEAD(require_throw_re)607c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require_throw_re)
608c243e490SMarcel Moolenaar {
609c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE_THROW_RE macro");
610c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require_throw_re)611c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require_throw_re)
612c243e490SMarcel Moolenaar {
613c243e490SMarcel Moolenaar     struct test {
614c243e490SMarcel Moolenaar         const char *what;
615c243e490SMarcel Moolenaar         bool ok;
616c243e490SMarcel Moolenaar         const char *msg;
617c243e490SMarcel Moolenaar     } *t, tests[] = {
618c243e490SMarcel Moolenaar         { "throw_int", false, "unexpected error" },
619c243e490SMarcel Moolenaar         { "throw_rt_match", true, NULL },
620c243e490SMarcel Moolenaar         { "throw_rt_no_match", false,
621c243e490SMarcel Moolenaar           "threw.*runtime_error\\(baz foo bar a\\).*"
622c243e490SMarcel Moolenaar           "does not match 'foo\\.\\*baz'" },
623c243e490SMarcel Moolenaar         { "no_throw_rt", false, "did not throw" },
624c243e490SMarcel Moolenaar         { NULL, false, NULL }
625c243e490SMarcel Moolenaar     };
626c243e490SMarcel Moolenaar 
627c243e490SMarcel Moolenaar     const atf::fs::path before("before");
628c243e490SMarcel Moolenaar     const atf::fs::path after("after");
629c243e490SMarcel Moolenaar 
630c243e490SMarcel Moolenaar     for (t = &tests[0]; t->what != NULL; t++) {
631c243e490SMarcel Moolenaar         atf::tests::vars_map config;
632c243e490SMarcel Moolenaar         config["what"] = t->what;
633c243e490SMarcel Moolenaar 
634c243e490SMarcel Moolenaar         std::cout << "Checking with " << t->what << " and expecting "
635c243e490SMarcel Moolenaar                   << (t->ok ? "true" : "false") << "\n";
636c243e490SMarcel Moolenaar 
637c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require_throw_re);
638c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require_throw_re) >(config);
639c243e490SMarcel Moolenaar 
640c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
641c243e490SMarcel Moolenaar         if (t->ok) {
642a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
643c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
644c243e490SMarcel Moolenaar         } else {
645c243e490SMarcel Moolenaar             std::cout << "Checking that message contains '" << t->msg
646c243e490SMarcel Moolenaar                       << "'\n";
647c243e490SMarcel Moolenaar             std::string exp_result = std::string("^failed: .*") + t->msg;
648a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
649c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
650c243e490SMarcel Moolenaar         }
651c243e490SMarcel Moolenaar 
652c243e490SMarcel Moolenaar         atf::fs::remove(before);
653c243e490SMarcel Moolenaar         if (t->ok)
654c243e490SMarcel Moolenaar             atf::fs::remove(after);
655c243e490SMarcel Moolenaar     }
656c243e490SMarcel Moolenaar }
657c243e490SMarcel Moolenaar 
658c243e490SMarcel Moolenaar ATF_TEST_CASE(check_errno);
ATF_TEST_CASE_HEAD(check_errno)659c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(check_errno)
660c243e490SMarcel Moolenaar {
661c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_CHECK_ERRNO macro");
662c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(check_errno)663c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(check_errno)
664c243e490SMarcel Moolenaar {
665c243e490SMarcel Moolenaar     struct test {
666c243e490SMarcel Moolenaar         const char *what;
667c243e490SMarcel Moolenaar         bool ok;
668c243e490SMarcel Moolenaar         const char *msg;
669c243e490SMarcel Moolenaar     } *t, tests[] = {
670c243e490SMarcel Moolenaar         { "no_error", false,
671c243e490SMarcel Moolenaar           "Expected true value in errno_ok_stub\\(\\) == -1" },
672c243e490SMarcel Moolenaar         { "errno_ok", true, NULL },
673c243e490SMarcel Moolenaar         { "errno_fail", false,
674c243e490SMarcel Moolenaar           "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
675c243e490SMarcel Moolenaar         { NULL, false, NULL }
676c243e490SMarcel Moolenaar     };
677c243e490SMarcel Moolenaar 
678c243e490SMarcel Moolenaar     const atf::fs::path before("before");
679c243e490SMarcel Moolenaar     const atf::fs::path after("after");
680c243e490SMarcel Moolenaar 
681c243e490SMarcel Moolenaar     for (t = &tests[0]; t->what != NULL; t++) {
682c243e490SMarcel Moolenaar         atf::tests::vars_map config;
683c243e490SMarcel Moolenaar         config["what"] = t->what;
684c243e490SMarcel Moolenaar 
685c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_check_errno);
686c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_check_errno) >(config);
687c243e490SMarcel Moolenaar 
688c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
689c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(after));
690c243e490SMarcel Moolenaar 
691c243e490SMarcel Moolenaar         if (t->ok) {
692a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
693c243e490SMarcel Moolenaar         } else {
694a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^failed", "result"));
695c243e490SMarcel Moolenaar 
696c243e490SMarcel Moolenaar             std::string exp_result = "macros_test.cpp:[0-9]+: " +
697c243e490SMarcel Moolenaar                 std::string(t->msg) + "$";
698a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "stderr"));
699c243e490SMarcel Moolenaar         }
700c243e490SMarcel Moolenaar 
701c243e490SMarcel Moolenaar         atf::fs::remove(before);
702c243e490SMarcel Moolenaar         atf::fs::remove(after);
703c243e490SMarcel Moolenaar     }
704c243e490SMarcel Moolenaar }
705c243e490SMarcel Moolenaar 
706c243e490SMarcel Moolenaar ATF_TEST_CASE(require_errno);
ATF_TEST_CASE_HEAD(require_errno)707c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(require_errno)
708c243e490SMarcel Moolenaar {
709c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the ATF_REQUIRE_ERRNO macro");
710c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(require_errno)711c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(require_errno)
712c243e490SMarcel Moolenaar {
713c243e490SMarcel Moolenaar     struct test {
714c243e490SMarcel Moolenaar         const char *what;
715c243e490SMarcel Moolenaar         bool ok;
716c243e490SMarcel Moolenaar         const char *msg;
717c243e490SMarcel Moolenaar     } *t, tests[] = {
718c243e490SMarcel Moolenaar         { "no_error", false,
719c243e490SMarcel Moolenaar           "Expected true value in errno_ok_stub\\(\\) == -1" },
720c243e490SMarcel Moolenaar         { "errno_ok", true, NULL },
721c243e490SMarcel Moolenaar         { "errno_fail", false,
722c243e490SMarcel Moolenaar           "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
723c243e490SMarcel Moolenaar         { NULL, false, NULL }
724c243e490SMarcel Moolenaar     };
725c243e490SMarcel Moolenaar 
726c243e490SMarcel Moolenaar     const atf::fs::path before("before");
727c243e490SMarcel Moolenaar     const atf::fs::path after("after");
728c243e490SMarcel Moolenaar 
729c243e490SMarcel Moolenaar     for (t = &tests[0]; t->what != NULL; t++) {
730c243e490SMarcel Moolenaar         atf::tests::vars_map config;
731c243e490SMarcel Moolenaar         config["what"] = t->what;
732c243e490SMarcel Moolenaar 
733c243e490SMarcel Moolenaar         ATF_TEST_CASE_USE(h_require_errno);
734c243e490SMarcel Moolenaar         run_h_tc< ATF_TEST_CASE_NAME(h_require_errno) >(config);
735c243e490SMarcel Moolenaar 
736c243e490SMarcel Moolenaar         ATF_REQUIRE(atf::fs::exists(before));
737c243e490SMarcel Moolenaar         if (t->ok) {
738a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
739c243e490SMarcel Moolenaar             ATF_REQUIRE(atf::fs::exists(after));
740c243e490SMarcel Moolenaar         } else {
741c243e490SMarcel Moolenaar             std::string exp_result = "^failed: .*macros_test.cpp:[0-9]+: " +
742c243e490SMarcel Moolenaar                 std::string(t->msg) + "$";
743a18eacbeSJulio Merino             ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
744c243e490SMarcel Moolenaar 
745c243e490SMarcel Moolenaar             ATF_REQUIRE(!atf::fs::exists(after));
746c243e490SMarcel Moolenaar         }
747c243e490SMarcel Moolenaar 
748c243e490SMarcel Moolenaar         atf::fs::remove(before);
749c243e490SMarcel Moolenaar         if (t->ok)
750c243e490SMarcel Moolenaar             atf::fs::remove(after);
751c243e490SMarcel Moolenaar     }
752c243e490SMarcel Moolenaar }
753c243e490SMarcel Moolenaar 
754c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
755c243e490SMarcel Moolenaar // Tests cases for the header file.
756c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
757c243e490SMarcel Moolenaar 
758c243e490SMarcel Moolenaar BUILD_TC(use, "macros_hpp_test.cpp",
759c243e490SMarcel Moolenaar          "Tests that the macros provided by the atf-c++/macros.hpp file "
760c243e490SMarcel Moolenaar          "do not cause syntax errors when used",
761c243e490SMarcel Moolenaar          "Build of macros_hpp_test.cpp failed; some macros in "
762c243e490SMarcel Moolenaar          "atf-c++/macros.hpp are broken");
7631a61beb0SJulio Merino 
7641a61beb0SJulio Merino ATF_TEST_CASE(detect_unused_tests);
ATF_TEST_CASE_HEAD(detect_unused_tests)7651a61beb0SJulio Merino ATF_TEST_CASE_HEAD(detect_unused_tests)
7661a61beb0SJulio Merino {
7671a61beb0SJulio Merino     set_md_var("descr",
7681a61beb0SJulio Merino                "Tests that defining an unused test case raises a warning (and "
7691a61beb0SJulio Merino                "thus an error)");
7701a61beb0SJulio Merino }
ATF_TEST_CASE_BODY(detect_unused_tests)7711a61beb0SJulio Merino ATF_TEST_CASE_BODY(detect_unused_tests)
7721a61beb0SJulio Merino {
7731a61beb0SJulio Merino     const char* validate_compiler =
7741a61beb0SJulio Merino         "class test_class { public: int dummy; };\n"
7751a61beb0SJulio Merino         "#define define_unused static test_class unused\n"
7761a61beb0SJulio Merino         "define_unused;\n";
7771a61beb0SJulio Merino 
7781a61beb0SJulio Merino     atf::utils::create_file("compiler_test.cpp", validate_compiler);
7791a61beb0SJulio Merino     if (build_check_cxx_o("compiler_test.cpp"))
7801a61beb0SJulio Merino         expect_fail("Compiler does not raise a warning on an unused "
7811a61beb0SJulio Merino                     "static global variable declared by a macro");
7821a61beb0SJulio Merino 
7831a61beb0SJulio Merino     if (build_check_cxx_o_srcdir(*this, "unused_test.cpp"))
7841a61beb0SJulio Merino         ATF_FAIL("Build of unused_test.cpp passed; unused test cases are "
7851a61beb0SJulio Merino                  "not properly detected");
7861a61beb0SJulio Merino }
787c243e490SMarcel Moolenaar 
788c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
789c243e490SMarcel Moolenaar // Main.
790c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
791c243e490SMarcel Moolenaar 
ATF_INIT_TEST_CASES(tcs)792c243e490SMarcel Moolenaar ATF_INIT_TEST_CASES(tcs)
793c243e490SMarcel Moolenaar {
794c243e490SMarcel Moolenaar     // Add the test cases for the macros.
795c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, pass);
796c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, fail);
797c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, skip);
798c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, check_errno);
799c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require);
800c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require_eq);
801c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require_in);
802c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require_match);
803c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require_not_in);
804c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require_throw);
805c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require_throw_re);
806c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, require_errno);
807c243e490SMarcel Moolenaar 
808c243e490SMarcel Moolenaar     // Add the test cases for the header file.
809c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, use);
810c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, detect_unused_tests);
811c243e490SMarcel Moolenaar }
812