Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 1 | #include "sensors/pluggable.hpp" |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 2 | #include "test/readinterface_mock.hpp" |
| 3 | #include "test/writeinterface_mock.hpp" |
| 4 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | #include <chrono> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 10 | using ::testing::Invoke; |
| 11 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 12 | TEST(PluggableSensorTest, BoringConstructorTest) |
| 13 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 14 | // 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 30 | TEST(PluggableSensorTest, TryReadingTest) |
| 31 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 32 | // 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 44 | ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get()); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 45 | |
| 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 52 | EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; })); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 53 | |
| 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 60 | TEST(PluggableSensorTest, TryWritingTest) |
| 61 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 62 | // 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 74 | WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get()); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 75 | |
| 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 | } |