From 034cd404a129103a8dd7747e6bd00ffd5550da93 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Mon, 30 Jun 2025 07:51:23 +0100 Subject: refactoring --- nihil.posix/getenv.test.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 nihil.posix/getenv.test.cc (limited to 'nihil.posix/getenv.test.cc') diff --git a/nihil.posix/getenv.test.cc b/nihil.posix/getenv.test.cc new file mode 100644 index 0000000..9e10c16 --- /dev/null +++ b/nihil.posix/getenv.test.cc @@ -0,0 +1,50 @@ +/* + * This source code is released into the public domain. + */ + +#include +#include +#include + +#include + +#include + +import nihil.error; +import nihil.posix; + +TEST_CASE("getenv: existing value", "[getenv]") +{ + auto constexpr *name = "NIHIL_TEST_VAR"; + auto constexpr *value = "test is a test"; + + REQUIRE(::setenv(name, value, 1) == 0); + + auto const s = nihil::getenv(name); + REQUIRE(s); + REQUIRE(*s == value); +} + +TEST_CASE("getenv: non-existing value", "[getenv]") +{ + auto constexpr *name = "NIHIL_TEST_VAR"; + + REQUIRE(::unsetenv(name) == 0); + + auto const s = nihil::getenv(name); + REQUIRE(!s); + REQUIRE(s.error() == std::errc::no_such_file_or_directory); +} + +// Force the call to getenv_r() to reallocate. +TEST_CASE("getenv: long value") +{ + auto constexpr *name = "NIHIL_TEST_VAR"; + auto const value = std::string(4096, 'a'); + + REQUIRE(::setenv(name, value.c_str(), 1) == 0); + + auto const s = nihil::getenv(name); + REQUIRE(s); + REQUIRE(*s == value); +} -- cgit v1.2.3