blob: 8e12e0e2c74212ace4fd3f1be91df9c09058c908 [file] [log] [blame]
Carol Wang9bbe6022019-08-01 17:31:30 +08001#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 Yibd9bc002019-08-21 10:05:27 -07008using ::testing::_;
Carol Wang9bbe6022019-08-01 17:31:30 +08009using ::testing::Return;
10using ::testing::StrEq;
Kun Yibd9bc002019-08-21 10:05:27 -070011
12namespace env
13{
14
15// Delegate all calls to getEnv() to the mock
16const char* EnvImpl::get(const char* key) const
17{
18 return mockEnv.get(key);
19}
20
21EnvImpl env_impl;
22
23} // namespace env
24
25TEST(EnvTest, NonExistingEnvReturnsEmptyString)
26{
27 EXPECT_CALL(env::mockEnv, get(_)).WillOnce(Return(nullptr));
28 EXPECT_EQ(std::string(), env::getEnv("NonExistingKey"));
29}
Carol Wang9bbe6022019-08-01 17:31:30 +080030
31TEST(EnvTest, EmptyEnv)
32{
Kun Yibd9bc002019-08-21 10:05:27 -070033 EXPECT_CALL(env::mockEnv, get(StrEq("AVERAGE_power1")))
34 .WillOnce(Return(nullptr));
Carol Wang9bbe6022019-08-01 17:31:30 +080035 EXPECT_FALSE(
36 phosphor::utility::isAverageEnvSet(std::make_pair("power", "1")));
37}
38
39TEST(EnvTest, ValidAverageEnv)
40{
Carol Wang9bbe6022019-08-01 17:31:30 +080041 std::string power = "power";
42 std::string one = "1";
43 std::string two = "2";
44
Kun Yibd9bc002019-08-21 10:05:27 -070045 EXPECT_CALL(env::mockEnv, get(StrEq("AVERAGE_power1")))
Carol Wang9bbe6022019-08-01 17:31:30 +080046 .WillOnce(Return("true"));
Kun Yibd9bc002019-08-21 10:05:27 -070047 EXPECT_CALL(env::mockEnv, get(StrEq("AVERAGE_power2")))
Carol Wang9bbe6022019-08-01 17:31:30 +080048 .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}