19 #include "gtest/gtest.h" 26 std::string GetEnvVar(
const char*
name) {
29 auto get_res = GetEnvironmentVariableA(name, buf,
sizeof(buf));
30 if (get_res >=
sizeof(buf)) {
38 return std::string(buf, get_res);
40 const char* val = ::getenv(name);
49 TEST(ScopedSetEnvTest, SetNonExistingVarToString) {
50 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"UNSET");
53 ScopedSetEnv scoped_set(
"SCOPED_SET_ENV_TEST_VAR",
"value");
55 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"value");
58 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"UNSET");
61 TEST(ScopedSetEnvTest, SetNonExistingVarToNull) {
62 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"UNSET");
65 ScopedSetEnv scoped_set(
"SCOPED_SET_ENV_TEST_VAR",
nullptr);
67 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"UNSET");
70 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"UNSET");
73 TEST(ScopedSetEnvTest, SetExistingVarToString) {
74 ScopedSetEnv scoped_set(
"SCOPED_SET_ENV_TEST_VAR",
"value");
75 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"value");
78 ScopedSetEnv scoped_set(
"SCOPED_SET_ENV_TEST_VAR",
"new_value");
80 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"new_value");
83 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"value");
86 TEST(ScopedSetEnvTest, SetExistingVarToNull) {
87 ScopedSetEnv scoped_set(
"SCOPED_SET_ENV_TEST_VAR",
"value");
88 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"value");
91 ScopedSetEnv scoped_set(
"SCOPED_SET_ENV_TEST_VAR",
nullptr);
93 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"UNSET");
96 EXPECT_EQ(GetEnvVar(
"SCOPED_SET_ENV_TEST_VAR"),
"value");
TEST(Symbolize, Unimplemented)