1 #include "helpers.hpp" 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 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 { 62 auto sensor1 = 63 sut.makeSensor<NiceMock<SensorMock>>(id<0>().service, id<0>().path); 64 auto sensor2 = 65 sut.makeSensor<NiceMock<SensorMock>>(id<1>().service, id<1>().path); 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 { 73 auto sensor1 = 74 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path"); 75 auto sensor2 = 76 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path"); 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 { 84 auto mockId1 = 85 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path") 86 ->mockId; 87 auto mockId2 = 88 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path") 89 ->mockId; 90 91 ASSERT_THAT(mockId2, Not(Eq(mockId1))); 92 } 93 94 TEST_F(TestSensorCache, shouldCreateSensorWithCorrespondingId) 95 { 96 auto id = 97 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path") 98 ->id(); 99 100 auto expected = SensorMock::makeId("sensor-service", "sensor-path"); 101 102 ASSERT_THAT(id, Eq(expected)); 103 } 104