locator.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <gtest/gtest.h>
  2. #include <entt/locator/locator.hpp>
  3. struct a_service {};
  4. struct another_service {
  5. virtual ~another_service() = default;
  6. virtual void f(bool) = 0;
  7. bool check{false};
  8. };
  9. struct derived_service: another_service {
  10. derived_service(int)
  11. : another_service{} {}
  12. void f(bool b) override {
  13. check = b;
  14. }
  15. };
  16. TEST(ServiceLocator, Functionalities) {
  17. ASSERT_TRUE(entt::service_locator<a_service>::empty());
  18. ASSERT_TRUE(entt::service_locator<another_service>::empty());
  19. entt::service_locator<a_service>::set();
  20. ASSERT_FALSE(entt::service_locator<a_service>::empty());
  21. ASSERT_TRUE(entt::service_locator<another_service>::empty());
  22. entt::service_locator<a_service>::reset();
  23. ASSERT_TRUE(entt::service_locator<a_service>::empty());
  24. ASSERT_TRUE(entt::service_locator<another_service>::empty());
  25. entt::service_locator<a_service>::set(std::make_shared<a_service>());
  26. ASSERT_FALSE(entt::service_locator<a_service>::empty());
  27. ASSERT_TRUE(entt::service_locator<another_service>::empty());
  28. entt::service_locator<another_service>::set<derived_service>(42);
  29. ASSERT_FALSE(entt::service_locator<a_service>::empty());
  30. ASSERT_FALSE(entt::service_locator<another_service>::empty());
  31. entt::service_locator<another_service>::get().lock()->f(!entt::service_locator<another_service>::get().lock()->check);
  32. ASSERT_TRUE(entt::service_locator<another_service>::get().lock()->check);
  33. entt::service_locator<another_service>::ref().f(!entt::service_locator<another_service>::get().lock()->check);
  34. ASSERT_FALSE(entt::service_locator<another_service>::get().lock()->check);
  35. }