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