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