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