xref: /freebsd/contrib/atf/atf-c++/detail/env_test.cpp (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1c243e490SMarcel Moolenaar // Copyright (c) 2007 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.
25c243e490SMarcel Moolenaar 
26*0677dfd1SJulio Merino #include "atf-c++/detail/env.hpp"
27c243e490SMarcel Moolenaar 
28*0677dfd1SJulio Merino #include <atf-c++.hpp>
29c243e490SMarcel Moolenaar 
30c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
31c243e490SMarcel Moolenaar // Test cases for the free functions.
32c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
33c243e490SMarcel Moolenaar 
34c243e490SMarcel Moolenaar ATF_TEST_CASE(has_get);
ATF_TEST_CASE_HEAD(has_get)35c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(has_get)
36c243e490SMarcel Moolenaar {
37c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the has and get functions");
38c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(has_get)39c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(has_get)
40c243e490SMarcel Moolenaar {
41c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::has("PATH"));
42c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::get("PATH").empty());
43c243e490SMarcel Moolenaar 
44c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::has("_UNDEFINED_VARIABLE_"));
45c243e490SMarcel Moolenaar }
46c243e490SMarcel Moolenaar 
47*0677dfd1SJulio Merino ATF_TEST_CASE(get_with_default);
ATF_TEST_CASE_HEAD(get_with_default)48*0677dfd1SJulio Merino ATF_TEST_CASE_HEAD(get_with_default)
49*0677dfd1SJulio Merino {
50*0677dfd1SJulio Merino     set_md_var("descr", "Tests the get function with a default value");
51*0677dfd1SJulio Merino }
ATF_TEST_CASE_BODY(get_with_default)52*0677dfd1SJulio Merino ATF_TEST_CASE_BODY(get_with_default)
53*0677dfd1SJulio Merino {
54*0677dfd1SJulio Merino     ATF_REQUIRE(atf::env::has("PATH"));
55*0677dfd1SJulio Merino     ATF_REQUIRE(atf::env::get("PATH", "default value") != "default value");
56*0677dfd1SJulio Merino 
57*0677dfd1SJulio Merino     ATF_REQUIRE_EQ(atf::env::get("_UNDEFINED_VARIABLE_", "foo bar"), "foo bar");
58*0677dfd1SJulio Merino }
59*0677dfd1SJulio Merino 
60c243e490SMarcel Moolenaar ATF_TEST_CASE(set);
ATF_TEST_CASE_HEAD(set)61c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(set)
62c243e490SMarcel Moolenaar {
63c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the set function");
64c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(set)65c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(set)
66c243e490SMarcel Moolenaar {
67c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::has("PATH"));
68c243e490SMarcel Moolenaar     const std::string& oldval = atf::env::get("PATH");
69c243e490SMarcel Moolenaar     atf::env::set("PATH", "foo-bar");
70c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::get("PATH") != oldval);
71c243e490SMarcel Moolenaar     ATF_REQUIRE_EQ(atf::env::get("PATH"), "foo-bar");
72c243e490SMarcel Moolenaar 
73c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::has("_UNDEFINED_VARIABLE_"));
74c243e490SMarcel Moolenaar     atf::env::set("_UNDEFINED_VARIABLE_", "foo2-bar2");
75c243e490SMarcel Moolenaar     ATF_REQUIRE_EQ(atf::env::get("_UNDEFINED_VARIABLE_"), "foo2-bar2");
76c243e490SMarcel Moolenaar }
77c243e490SMarcel Moolenaar 
78c243e490SMarcel Moolenaar ATF_TEST_CASE(unset);
ATF_TEST_CASE_HEAD(unset)79c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(unset)
80c243e490SMarcel Moolenaar {
81c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the unset function");
82c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(unset)83c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(unset)
84c243e490SMarcel Moolenaar {
85c243e490SMarcel Moolenaar     ATF_REQUIRE(atf::env::has("PATH"));
86c243e490SMarcel Moolenaar     atf::env::unset("PATH");
87c243e490SMarcel Moolenaar     ATF_REQUIRE(!atf::env::has("PATH"));
88c243e490SMarcel Moolenaar }
89c243e490SMarcel Moolenaar 
90c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
91c243e490SMarcel Moolenaar // Main.
92c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
93c243e490SMarcel Moolenaar 
ATF_INIT_TEST_CASES(tcs)94c243e490SMarcel Moolenaar ATF_INIT_TEST_CASES(tcs)
95c243e490SMarcel Moolenaar {
96c243e490SMarcel Moolenaar     // Add the test cases for the free functions.
97c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, has_get);
98*0677dfd1SJulio Merino     ATF_ADD_TEST_CASE(tcs, get_with_default);
99c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, set);
100c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, unset);
101c243e490SMarcel Moolenaar }
102