Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 1 | #include "helpers.hpp" |
Krzysztof Grobelny | 7f06f61 | 2020-09-24 13:42:10 +0200 | [diff] [blame] | 2 | #include "mocks/sensor_mock.hpp" |
| 3 | #include "sensor_cache.hpp" |
| 4 | |
| 5 | #include <initializer_list> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | |
| 9 | using namespace testing; |
| 10 | |
| 11 | class TestSensorCache : public Test |
| 12 | { |
| 13 | public: |
| 14 | SensorCache sut; |
| 15 | }; |
| 16 | |
Krzysztof Grobelny | 7f06f61 | 2020-09-24 13:42:10 +0200 | [diff] [blame] | 17 | struct IdParam |
| 18 | { |
| 19 | IdParam() = default; |
| 20 | IdParam(std::string_view service, std::string_view path) : |
| 21 | service(service), path(path) |
| 22 | {} |
| 23 | |
| 24 | std::string service; |
| 25 | std::string path; |
| 26 | }; |
| 27 | |
| 28 | class TestSensorCacheP : |
| 29 | public TestSensorCache, |
| 30 | public WithParamInterface<std::vector<IdParam>> |
| 31 | { |
| 32 | public: |
| 33 | void SetUp() override |
| 34 | { |
| 35 | auto vec = GetParam(); |
| 36 | ASSERT_THAT(vec, SizeIs(param.size())); |
| 37 | std::copy(vec.begin(), vec.end(), param.begin()); |
| 38 | } |
| 39 | |
| 40 | template <size_t index> |
| 41 | const IdParam& id() const |
| 42 | { |
| 43 | static_assert(index < std::tuple_size_v<decltype(param)>); |
| 44 | return param[index]; |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | std::array<IdParam, 2> param; |
| 49 | }; |
| 50 | |
| 51 | INSTANTIATE_TEST_SUITE_P( |
| 52 | UniqueIds, TestSensorCacheP, |
| 53 | Values(std::vector<IdParam>({IdParam("service1", "path1"), |
| 54 | IdParam("service1", "path2")}), |
| 55 | std::vector<IdParam>({IdParam("service1", "path1"), |
| 56 | IdParam("service2", "path1")}), |
| 57 | std::vector<IdParam>({IdParam("service1", "path1"), |
| 58 | IdParam("service2", "path2")}))); |
| 59 | |
| 60 | TEST_P(TestSensorCacheP, shouldReturnDifferentSensorWhenIdsAreDifferent) |
| 61 | { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 62 | auto sensor1 = sut.makeSensor<NiceMock<SensorMock>>(id<0>().service, |
| 63 | id<0>().path); |
| 64 | auto sensor2 = sut.makeSensor<NiceMock<SensorMock>>(id<1>().service, |
| 65 | id<1>().path); |
Krzysztof Grobelny | 7f06f61 | 2020-09-24 13:42:10 +0200 | [diff] [blame] | 66 | |
| 67 | ASSERT_THAT(sensor1.get(), Not(Eq(sensor2.get()))); |
| 68 | ASSERT_THAT(sensor1->mockId, Not(Eq(sensor2->mockId))); |
| 69 | } |
| 70 | |
| 71 | TEST_F(TestSensorCache, shouldReturnSameSensorWhenSensorWithSameIdStillExists) |
| 72 | { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 73 | auto sensor1 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service", |
| 74 | "sensor-path"); |
| 75 | auto sensor2 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service", |
| 76 | "sensor-path"); |
Krzysztof Grobelny | 7f06f61 | 2020-09-24 13:42:10 +0200 | [diff] [blame] | 77 | |
| 78 | ASSERT_THAT(sensor1.get(), Eq(sensor2.get())); |
| 79 | ASSERT_THAT(sensor1->mockId, Eq(sensor2->mockId)); |
| 80 | } |
| 81 | |
| 82 | TEST_F(TestSensorCache, shouldReturnDifferentSensorWhenPreviousSensorExpired) |
| 83 | { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 84 | auto mockId1 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service", |
| 85 | "sensor-path") |
| 86 | ->mockId; |
| 87 | auto mockId2 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service", |
| 88 | "sensor-path") |
| 89 | ->mockId; |
Krzysztof Grobelny | 7f06f61 | 2020-09-24 13:42:10 +0200 | [diff] [blame] | 90 | |
| 91 | ASSERT_THAT(mockId2, Not(Eq(mockId1))); |
| 92 | } |
| 93 | |
| 94 | TEST_F(TestSensorCache, shouldCreateSensorWithCorrespondingId) |
| 95 | { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 96 | auto id = sut.makeSensor<NiceMock<SensorMock>>("sensor-service", |
| 97 | "sensor-path") |
| 98 | ->id(); |
Krzysztof Grobelny | 7f06f61 | 2020-09-24 13:42:10 +0200 | [diff] [blame] | 99 | |
| 100 | auto expected = SensorMock::makeId("sensor-service", "sensor-path"); |
| 101 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 102 | ASSERT_THAT(id, Eq(expected)); |
Krzysztof Grobelny | 7f06f61 | 2020-09-24 13:42:10 +0200 | [diff] [blame] | 103 | } |