blob: a15f06c1354d6caec0a035cff962a7a1d4a0c20a [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 Venturee31ee6d2018-06-08 18:49:27 -070010using ::testing::Invoke;
11
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070012TEST(PluggableSensorTest, BoringConstructorTest)
13{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070014 // 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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030TEST(PluggableSensorTest, TryReadingTest)
31{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070032 // 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
Patrick Venturee2ec0f62018-09-04 12:30:27 -070044 ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get());
Patrick Venturee31ee6d2018-06-08 18:49:27 -070045
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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; }));
Patrick Venturee31ee6d2018-06-08 18:49:27 -070053
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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060TEST(PluggableSensorTest, TryWritingTest)
61{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070062 // 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
Patrick Venturee2ec0f62018-09-04 12:30:27 -070074 WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get());
Patrick Venturee31ee6d2018-06-08 18:49:27 -070075
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}