#include #include #include #include "../common/config.h" struct base_service { virtual ~base_service() = default; virtual int invoke(int) = 0; }; struct derived_service: base_service { derived_service(int val) : value{val} {} int invoke(int other) override { return value + other; } private: int value; }; struct ServiceLocator: ::testing::Test { void SetUp() override { entt::locator::reset(); } }; using ServiceLocatorDeathTest = ServiceLocator; TEST_F(ServiceLocator, ValueAndTheLike) { ASSERT_FALSE(entt::locator::has_value()); ASSERT_EQ(entt::locator::value_or(1).invoke(3), 4); ASSERT_TRUE(entt::locator::has_value()); ASSERT_EQ(entt::locator::value().invoke(9), 10); } TEST_F(ServiceLocator, Emplace) { ASSERT_FALSE(entt::locator::has_value()); ASSERT_EQ(entt::locator::emplace(5).invoke(1), 6); ASSERT_TRUE(entt::locator::has_value()); ASSERT_EQ(entt::locator::value().invoke(3), 8); entt::locator::reset(); ASSERT_FALSE(entt::locator::has_value()); ASSERT_EQ(entt::locator::emplace(std::allocator_arg, std::allocator{}, 5).invoke(1), 6); ASSERT_TRUE(entt::locator::has_value()); ASSERT_EQ(entt::locator::value().invoke(3), 8); } TEST_F(ServiceLocator, ResetHandle) { entt::locator::emplace(1); auto handle = entt::locator::handle(); ASSERT_TRUE(entt::locator::has_value()); ASSERT_EQ(entt::locator::value().invoke(3), 4); entt::locator::reset(); ASSERT_FALSE(entt::locator::has_value()); entt::locator::reset(handle); ASSERT_TRUE(entt::locator::has_value()); ASSERT_EQ(entt::locator::value().invoke(3), 4); } TEST_F(ServiceLocator, ElementWithDeleter) { derived_service service{1}; entt::locator::reset(&service, [](base_service *serv) { *static_cast(serv) = derived_service{2}; }); ASSERT_TRUE(entt::locator::has_value()); ASSERT_EQ(entt::locator::value().invoke(1), 2); entt::locator::reset(); ASSERT_EQ(service.invoke(1), 3); } ENTT_DEBUG_TEST_F(ServiceLocatorDeathTest, UninitializedValue) { ASSERT_EQ(entt::locator::value_or(1).invoke(1), 2); entt::locator::reset(); ASSERT_DEATH(entt::locator::value().invoke(42), ""); }