Rework envMock

Instead of mocking all the functions, only mock std::getenv.
Now each test only needs to provide an EnvImpl that delegate calls to
mockEnv in order to inject dependencies on std::getenv. This for several
reasons:
1. Any call to env::getEnv() will be calling the real implementation of
the C++ code, and testing real code is better than testing mocks.
2. It is easier to write a fake class that takes a config string which
can greatly simplify test cases.
3. We can now write unit tests that ensure the number of times
std::getenv gets called (should be once, but multiple times right now).

Tested: unit tests still pass

Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: I3e5aff7fa5d025de1b8ae798af43b97d31151ab9
diff --git a/test/sensor_unittest.cpp b/test/sensor_unittest.cpp
index 8271b54..3a446b2 100644
--- a/test/sensor_unittest.cpp
+++ b/test/sensor_unittest.cpp
@@ -10,12 +10,24 @@
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
+namespace env
+{
+
+// Delegate all calls to getEnv() to the mock
+const char* EnvImpl::get(const char* key) const
+{
+    return mockEnv.get(key);
+}
+
+EnvImpl env_impl;
+
+} // namespace env
+
 class SensorTest : public ::testing::Test
 {
   protected:
     void SetUp() override
     {
-        envIntf = nullptr;
         gpioIntf = nullptr;
     }
 
@@ -33,27 +45,20 @@
 TEST_F(SensorTest, BasicConstructorTest)
 {
     /* Constructor test with nothing in an rcList or GPIO chip. */
-
-    StrictMock<EnvMock> eMock;
-    envIntf = &eMock;
-
     auto sensorKey = std::make_pair(temp, five);
     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
         std::make_unique<hwmonio::HwmonIOMock>();
     std::string path = "/";
 
     /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
-    EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
+    EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
         .WillOnce(Return(""));
-    EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
-        .WillOnce(Return(""));
+    EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
 
     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
-    EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
-        .WillOnce(Return(""));
-    EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
-        .WillOnce(Return(""));
-    EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
+    EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
+    EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
+    EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
         .WillOnce(Return(""));
 
     auto sensor =
@@ -65,9 +70,6 @@
 {
     /* Constructor test with only the GPIO chip set. */
 
-    StrictMock<EnvMock> eMock;
-    envIntf = &eMock;
-
     StrictMock<GpioHandleMock> gMock;
     gpioIntf = &gMock;
 
@@ -81,10 +83,9 @@
         std::make_unique<hwmonio::HwmonIOMock>();
     std::string path = "/";
 
-    EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
+    EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
         .WillOnce(Return("chipA"));
-    EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
-        .WillOnce(Return("5"));
+    EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5"));
 
     EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
         .WillOnce(Invoke([&](const std::string& chip, const std::string& line) {
@@ -92,11 +93,9 @@
         }));
 
     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
-    EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
-        .WillOnce(Return(""));
-    EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
-        .WillOnce(Return(""));
-    EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
+    EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
+    EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
+    EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
         .WillOnce(Return(""));
 
     auto sensor =
@@ -110,25 +109,20 @@
      * when adjusting the value.
      */
 
-    StrictMock<EnvMock> eMock;
-    envIntf = &eMock;
-
     auto sensorKey = std::make_pair(temp, five);
     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
         std::make_unique<hwmonio::HwmonIOMock>();
     std::string path = "/";
 
-    /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
-    EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
+    /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */
+    EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
         .WillOnce(Return(""));
-    EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
-        .WillOnce(Return(""));
+    EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
 
-    EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
-        .WillOnce(Return("10"));
-    EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
+    EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10"));
+    EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5")))
         .WillOnce(Return("15"));
-    EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
+    EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
         .WillOnce(Return(""));
 
     auto sensor =