blob: a80e252da545accd0a07ed325d2e4b2d9ba38597 [file] [log] [blame]
Patrick Venture0e2d68d2018-12-19 07:53:45 -08001#include "env_mock.hpp"
Patrick Venture99b95812018-12-19 08:59:15 -08002#include "gpio_mock.hpp"
Patrick Venture0e2d68d2018-12-19 07:53:45 -08003#include "hwmonio_mock.hpp"
4#include "sensor.hpp"
5
Patrick Venture99b95812018-12-19 08:59:15 -08006#include <gpioplus/test/handle.hpp>
Patrick Venture0e2d68d2018-12-19 07:53:45 -08007#include <memory>
8#include <utility>
9
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13class SensorTest : public ::testing::Test
14{
15 protected:
16 void SetUp() override
17 {
18 envIntf = nullptr;
Patrick Venture99b95812018-12-19 08:59:15 -080019 gpioIntf = nullptr;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080020 }
21};
22
23using ::testing::Eq;
Patrick Venture99b95812018-12-19 08:59:15 -080024using ::testing::Invoke;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080025using ::testing::Return;
26using ::testing::StrictMock;
27
28TEST_F(SensorTest, BasicConstructorTest)
29{
30 /* Constructor test with nothing in an rcList or GPIO chip. */
31
32 StrictMock<EnvMock> eMock;
33 envIntf = &eMock;
34
35 auto sensorKey = std::make_pair(std::string("temp"), std::string("5"));
36 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
37 std::make_unique<hwmonio::HwmonIOMock>();
38 std::string path = "/";
39
40 /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
41 EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey)))
42 .WillOnce(Return(""));
43 EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return(""));
44
45 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
46 EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return(""));
47 EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey)))
48 .WillOnce(Return(""));
49 EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey)))
50 .WillOnce(Return(""));
51
52 auto sensor =
53 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
54 EXPECT_FALSE(sensor == nullptr);
55}
Patrick Venture99b95812018-12-19 08:59:15 -080056
57TEST_F(SensorTest, SensorRequiresGpio)
58{
59 /* Constructor test with only the GPIO chip set. */
60
61 StrictMock<EnvMock> eMock;
62 envIntf = &eMock;
63
64 StrictMock<GpioHandleMock> gMock;
65 gpioIntf = &gMock;
66
67 /* The following piece of code can probably be copied above once it's
68 * working.
69 */
70 auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
71
72 auto sensorKey = std::make_pair(std::string("temp"), std::string("5"));
73 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
74 std::make_unique<hwmonio::HwmonIOMock>();
75 std::string path = "/";
76
77 EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey)))
78 .WillOnce(Return("chipA"));
79 EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("5"));
80
81 EXPECT_CALL(gMock, build(Eq("chipA"), Eq("5")))
82 .WillOnce(Invoke([&](const std::string& chip, const std::string& line) {
83 return std::move(handleMock);
84 }));
85
86 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
87 EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return(""));
88 EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey)))
89 .WillOnce(Return(""));
90 EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey)))
91 .WillOnce(Return(""));
92
93 auto sensor =
94 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
95 EXPECT_FALSE(sensor == nullptr);
96}