Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 1 | #include "env.hpp" |
| 2 | #include "env_mock.hpp" |
| 3 | #include "util.hpp" |
| 4 | |
| 5 | #include <gmock/gmock.h> |
| 6 | #include <gtest/gtest.h> |
| 7 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 8 | using ::testing::_; |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 9 | using ::testing::Return; |
| 10 | using ::testing::StrEq; |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 11 | |
| 12 | namespace env |
| 13 | { |
| 14 | |
| 15 | // Delegate all calls to getEnv() to the mock |
| 16 | const char* EnvImpl::get(const char* key) const |
| 17 | { |
| 18 | return mockEnv.get(key); |
| 19 | } |
| 20 | |
| 21 | EnvImpl env_impl; |
| 22 | |
| 23 | } // namespace env |
| 24 | |
| 25 | TEST(EnvTest, NonExistingEnvReturnsEmptyString) |
| 26 | { |
| 27 | EXPECT_CALL(env::mockEnv, get(_)).WillOnce(Return(nullptr)); |
| 28 | EXPECT_EQ(std::string(), env::getEnv("NonExistingKey")); |
| 29 | } |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 30 | |
| 31 | TEST(EnvTest, EmptyEnv) |
| 32 | { |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 33 | EXPECT_CALL(env::mockEnv, get(StrEq("AVERAGE_power1"))) |
| 34 | .WillOnce(Return(nullptr)); |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 35 | EXPECT_FALSE( |
| 36 | phosphor::utility::isAverageEnvSet(std::make_pair("power", "1"))); |
| 37 | } |
| 38 | |
| 39 | TEST(EnvTest, ValidAverageEnv) |
| 40 | { |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 41 | std::string power = "power"; |
| 42 | std::string one = "1"; |
| 43 | std::string two = "2"; |
| 44 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 45 | EXPECT_CALL(env::mockEnv, get(StrEq("AVERAGE_power1"))) |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 46 | .WillOnce(Return("true")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 47 | EXPECT_CALL(env::mockEnv, get(StrEq("AVERAGE_power2"))) |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 48 | .WillOnce(Return("bar")); |
| 49 | |
| 50 | EXPECT_TRUE(phosphor::utility::isAverageEnvSet(std::make_pair(power, one))); |
| 51 | EXPECT_FALSE( |
| 52 | phosphor::utility::isAverageEnvSet(std::make_pair(power, two))); |
| 53 | } |