getenv_test.cc
Go to the documentation of this file.
1 #ifndef _POSIX_C_SOURCE
2 /* For setenv() */
3 #define _POSIX_C_SOURCE 200112L
4 #endif
5 
6 #include "catch2/catch.hpp"
7 
8 #include "cetlib/getenv.h"
9 #include "cetlib_except/exception.h"
10 #include "cetlib_except/exception_category_matcher.h"
11 #include "cetlib_except/exception_message_matcher.h"
12 
13 #include <cstdlib>
14 #include <string>
15 
16 namespace {
17  auto const VAR = "GETENV_TEST";
18 }
19 
20 TEST_CASE("getenv_value")
21 {
22  std::string const val{"TEST"};
23  setenv(VAR, val.c_str(), 1);
24  SECTION("Throw") { REQUIRE(val == cet::getenv(VAR)); }
25  SECTION("NoThrow") { REQUIRE(val == cet::getenv(VAR, std::nothrow)); }
26 }
27 
28 TEST_CASE("getenv_empty")
29 {
30  std::string const val; // Empty
31  setenv(VAR, val.c_str(), 1);
32  SECTION("Throw") { REQUIRE(val == cet::getenv(VAR)); }
33  SECTION("NoThrow") { REQUIRE(val == cet::getenv(VAR, std::nothrow)); }
34 }
35 
36 TEST_CASE("getenv_unset")
37 {
38  unsetenv(VAR);
39 
40  SECTION("Throw")
41  {
42  using namespace std::string_literals;
43  REQUIRE_THROWS_MATCHES(
44  cet::getenv(VAR),
46  cet::exception_category_matcher("getenv") &&
47  cet::exception_message_matcher(Catch::Matchers::Contains(
48  "Can't find an environment variable named \""s + VAR + "\"\n")));
49  }
50 
51  SECTION("No Throw") { CHECK(cet::getenv(VAR, std::nothrow).empty()); }
52 }
std::string string
Definition: nybbler.cc:12
TEST_CASE("getenv_value")
Definition: getenv_test.cc:20
std::string getenv(std::string const &name)
Definition: getenv.cc:15
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97