#include "mocks/sensor_mock.hpp" #include "sensor_cache.hpp" #include "utils/sensor_id_eq.hpp" #include #include using namespace testing; class TestSensorCache : public Test { public: SensorCache sut; }; struct IdParam { IdParam() = default; IdParam(std::string_view service, std::string_view path) : service(service), path(path) {} std::string service; std::string path; }; class TestSensorCacheP : public TestSensorCache, public WithParamInterface> { public: void SetUp() override { auto vec = GetParam(); ASSERT_THAT(vec, SizeIs(param.size())); std::copy(vec.begin(), vec.end(), param.begin()); } template const IdParam& id() const { static_assert(index < std::tuple_size_v); return param[index]; } private: std::array param; }; INSTANTIATE_TEST_SUITE_P( UniqueIds, TestSensorCacheP, Values(std::vector({IdParam("service1", "path1"), IdParam("service1", "path2")}), std::vector({IdParam("service1", "path1"), IdParam("service2", "path1")}), std::vector({IdParam("service1", "path1"), IdParam("service2", "path2")}))); TEST_P(TestSensorCacheP, shouldReturnDifferentSensorWhenIdsAreDifferent) { auto sensor1 = sut.makeSensor>(id<0>().service, id<0>().path); auto sensor2 = sut.makeSensor>(id<1>().service, id<1>().path); ASSERT_THAT(sensor1.get(), Not(Eq(sensor2.get()))); ASSERT_THAT(sensor1->mockId, Not(Eq(sensor2->mockId))); } TEST_F(TestSensorCache, shouldReturnSameSensorWhenSensorWithSameIdStillExists) { auto sensor1 = sut.makeSensor>("sensor-service", "sensor-path"); auto sensor2 = sut.makeSensor>("sensor-service", "sensor-path"); ASSERT_THAT(sensor1.get(), Eq(sensor2.get())); ASSERT_THAT(sensor1->mockId, Eq(sensor2->mockId)); } TEST_F(TestSensorCache, shouldReturnDifferentSensorWhenPreviousSensorExpired) { auto mockId1 = sut.makeSensor>("sensor-service", "sensor-path") ->mockId; auto mockId2 = sut.makeSensor>("sensor-service", "sensor-path") ->mockId; ASSERT_THAT(mockId2, Not(Eq(mockId1))); } TEST_F(TestSensorCache, shouldCreateSensorWithCorrespondingId) { auto id = sut.makeSensor>("sensor-service", "sensor-path") ->id(); auto expected = SensorMock::makeId("sensor-service", "sensor-path"); ASSERT_THAT(id, sensorIdEq(expected)); }