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