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