From fabce6a4937433dbe589a30d5154c9a7c78b0a16 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Tue, 21 Sep 2021 21:23:05 +0200 Subject: [PATCH] Add tests for env_util --- tests/CMakeLists.txt | 1 + tests/unit_tests/utils/env.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/unit_tests/utils/env.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 464b29c8..ea722197 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -50,6 +50,7 @@ add_unit_test(utils/actions) add_unit_test(utils/action_router) add_unit_test(utils/color) add_unit_test(utils/command) +add_unit_test(utils/env) add_unit_test(utils/math) add_unit_test(utils/memory) add_unit_test(utils/scope) diff --git a/tests/unit_tests/utils/env.cpp b/tests/unit_tests/utils/env.cpp new file mode 100644 index 00000000..7c9af106 --- /dev/null +++ b/tests/unit_tests/utils/env.cpp @@ -0,0 +1,27 @@ +#include "utils/env.hpp" + +#include "common/test.hpp" +#include "stdlib.h" + +using namespace polybar; + +static constexpr auto INEXISTENT_ENV = "POLYBAR_INEXISTENT"; + +TEST(Env, has) { + EXPECT_EQ(true, env_util::has("HOME")); + unsetenv(INEXISTENT_ENV); + EXPECT_EQ(false, env_util::has(INEXISTENT_ENV)); + setenv(INEXISTENT_ENV, "123", false); + EXPECT_EQ(true, env_util::has(INEXISTENT_ENV)); + unsetenv(INEXISTENT_ENV); + EXPECT_EQ(false, env_util::has(INEXISTENT_ENV)); +} + +TEST(Env, get) { + unsetenv(INEXISTENT_ENV); + EXPECT_EQ("fallback", env_util::get(INEXISTENT_ENV, "fallback")); + setenv(INEXISTENT_ENV, "123", false); + EXPECT_EQ("123", env_util::get(INEXISTENT_ENV, "fallback")); + unsetenv(INEXISTENT_ENV); + EXPECT_EQ("fallback", env_util::get(INEXISTENT_ENV, "fallback")); +}