xref: /freebsd/contrib/atf/atf-c++/detail/env_test.cpp (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 *c243e490SMarcel Moolenaar //
2 *c243e490SMarcel Moolenaar // Automated Testing Framework (atf)
3 *c243e490SMarcel Moolenaar //
4 *c243e490SMarcel Moolenaar // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 *c243e490SMarcel Moolenaar // All rights reserved.
6 *c243e490SMarcel Moolenaar //
7 *c243e490SMarcel Moolenaar // Redistribution and use in source and binary forms, with or without
8 *c243e490SMarcel Moolenaar // modification, are permitted provided that the following conditions
9 *c243e490SMarcel Moolenaar // are met:
10 *c243e490SMarcel Moolenaar // 1. Redistributions of source code must retain the above copyright
11 *c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer.
12 *c243e490SMarcel Moolenaar // 2. Redistributions in binary form must reproduce the above copyright
13 *c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer in the
14 *c243e490SMarcel Moolenaar //    documentation and/or other materials provided with the distribution.
15 *c243e490SMarcel Moolenaar //
16 *c243e490SMarcel Moolenaar // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 *c243e490SMarcel Moolenaar // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 *c243e490SMarcel Moolenaar // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 *c243e490SMarcel Moolenaar // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 *c243e490SMarcel Moolenaar // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 *c243e490SMarcel Moolenaar // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 *c243e490SMarcel Moolenaar // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 *c243e490SMarcel Moolenaar // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 *c243e490SMarcel Moolenaar // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 *c243e490SMarcel Moolenaar // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 *c243e490SMarcel Moolenaar // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 *c243e490SMarcel Moolenaar // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *c243e490SMarcel Moolenaar //
29 *c243e490SMarcel Moolenaar 
30 *c243e490SMarcel Moolenaar #include "../macros.hpp"
31 *c243e490SMarcel Moolenaar 
32 *c243e490SMarcel Moolenaar #include "env.hpp"
33 *c243e490SMarcel Moolenaar 
34 *c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
35 *c243e490SMarcel Moolenaar // Test cases for the free functions.
36 *c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
37 *c243e490SMarcel Moolenaar 
38 *c243e490SMarcel Moolenaar ATF_TEST_CASE(has_get);
39 *c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(has_get)
40 *c243e490SMarcel Moolenaar {
41 *c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the has and get functions");
42 *c243e490SMarcel Moolenaar }
43 *c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(has_get)
44 *c243e490SMarcel Moolenaar {
45 *c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::has("PATH"));
46 *c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::get("PATH").empty());
47 *c243e490SMarcel Moolenaar 
48 *c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::has("_UNDEFINED_VARIABLE_"));
49 *c243e490SMarcel Moolenaar }
50 *c243e490SMarcel Moolenaar 
51 *c243e490SMarcel Moolenaar ATF_TEST_CASE(set);
52 *c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(set)
53 *c243e490SMarcel Moolenaar {
54 *c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the set function");
55 *c243e490SMarcel Moolenaar }
56 *c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(set)
57 *c243e490SMarcel Moolenaar {
58 *c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::has("PATH"));
59 *c243e490SMarcel Moolenaar     const std::string& oldval = atf::env::get("PATH");
60 *c243e490SMarcel Moolenaar     atf::env::set("PATH", "foo-bar");
61 *c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::get("PATH") != oldval);
62 *c243e490SMarcel Moolenaar     ATF_REQUIRE_EQ(atf::env::get("PATH"), "foo-bar");
63 *c243e490SMarcel Moolenaar 
64 *c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::has("_UNDEFINED_VARIABLE_"));
65 *c243e490SMarcel Moolenaar     atf::env::set("_UNDEFINED_VARIABLE_", "foo2-bar2");
66 *c243e490SMarcel Moolenaar     ATF_REQUIRE_EQ(atf::env::get("_UNDEFINED_VARIABLE_"), "foo2-bar2");
67 *c243e490SMarcel Moolenaar }
68 *c243e490SMarcel Moolenaar 
69 *c243e490SMarcel Moolenaar ATF_TEST_CASE(unset);
70 *c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(unset)
71 *c243e490SMarcel Moolenaar {
72 *c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the unset function");
73 *c243e490SMarcel Moolenaar }
74 *c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(unset)
75 *c243e490SMarcel Moolenaar {
76 *c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::has("PATH"));
77 *c243e490SMarcel Moolenaar     atf::env::unset("PATH");
78 *c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::has("PATH"));
79 *c243e490SMarcel Moolenaar }
80 *c243e490SMarcel Moolenaar 
81 *c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
82 *c243e490SMarcel Moolenaar // Main.
83 *c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
84 *c243e490SMarcel Moolenaar 
85 *c243e490SMarcel Moolenaar ATF_INIT_TEST_CASES(tcs)
86 *c243e490SMarcel Moolenaar {
87 *c243e490SMarcel Moolenaar     // Add the test cases for the free functions.
88 *c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, has_get);
89 *c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, set);
90 *c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, unset);
91 *c243e490SMarcel Moolenaar }
92