blob: ffd08cd6f7ae4e4dc0b8d7b1613a4188bba6ba7c [file] [log] [blame]
Patrick Venturee31ee6d2018-06-08 18:49:27 -07001#include "sensors/pluggable.hpp"
Patrick Venturee31ee6d2018-06-08 18:49:27 -07002#include "test/readinterface_mock.hpp"
3#include "test/writeinterface_mock.hpp"
4
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07005#include <chrono>
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
Patrick Venturea0764872020-08-08 07:48:43 -070010namespace pid_control
11{
12namespace
13{
14
Patrick Venturee31ee6d2018-06-08 18:49:27 -070015using ::testing::Invoke;
16
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017TEST(PluggableSensorTest, BoringConstructorTest)
18{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070019 // 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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070035TEST(PluggableSensorTest, TryReadingTest)
36{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070037 // 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
Patrick Venturee2ec0f62018-09-04 12:30:27 -070049 ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get());
Patrick Venturee31ee6d2018-06-08 18:49:27 -070050
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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; }));
Patrick Venturee31ee6d2018-06-08 18:49:27 -070058
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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065TEST(PluggableSensorTest, TryWritingTest)
66{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070067 // 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
Patrick Venturee2ec0f62018-09-04 12:30:27 -070079 WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get());
Patrick Venturee31ee6d2018-06-08 18:49:27 -070080
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}
Patrick Venturea0764872020-08-08 07:48:43 -070088
89} // namespace
90} // namespace pid_control