1 #include "sensors/pluggable.hpp"
2 #include "test/readinterface_mock.hpp"
3 #include "test/writeinterface_mock.hpp"
4 
5 #include <chrono>
6 
7 #include <gmock/gmock.h>
8 #include <gtest/gtest.h>
9 
10 using ::testing::Invoke;
11 
12 TEST(PluggableSensorTest, BoringConstructorTest)
13 {
14     // Build a boring Pluggable Sensor.
15 
16     int64_t min = 0;
17     int64_t max = 255;
18 
19     std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
20     std::unique_ptr<WriteInterface> wi =
21         std::make_unique<WriteInterfaceMock>(min, max);
22 
23     std::string name = "name";
24     int64_t timeout = 1;
25 
26     PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
27     // Successfully created it.
28 }
29 
30 TEST(PluggableSensorTest, TryReadingTest)
31 {
32     // Verify calling read, calls the ReadInterface.
33 
34     int64_t min = 0;
35     int64_t max = 255;
36 
37     std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
38     std::unique_ptr<WriteInterface> wi =
39         std::make_unique<WriteInterfaceMock>(min, max);
40 
41     std::string name = "name";
42     int64_t timeout = 1;
43 
44     ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get());
45 
46     PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
47 
48     ReadReturn r;
49     r.value = 0.1;
50     r.updated = std::chrono::high_resolution_clock::now();
51 
52     EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; }));
53 
54     // TODO(venture): Implement comparison operator for ReadReturn.
55     ReadReturn v = p.read();
56     EXPECT_EQ(r.value, v.value);
57     EXPECT_EQ(r.updated, v.updated);
58 }
59 
60 TEST(PluggableSensorTest, TryWritingTest)
61 {
62     // Verify calling write, calls the WriteInterface.
63 
64     int64_t min = 0;
65     int64_t max = 255;
66 
67     std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
68     std::unique_ptr<WriteInterface> wi =
69         std::make_unique<WriteInterfaceMock>(min, max);
70 
71     std::string name = "name";
72     int64_t timeout = 1;
73 
74     WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get());
75 
76     PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
77 
78     double value = 0.303;
79 
80     EXPECT_CALL(*wip, write(value));
81     p.write(value);
82 }
83