main.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #define CR_HOST
  2. #include <gtest/gtest.h>
  3. #include <cr.h>
  4. #include <entt/core/hashed_string.hpp>
  5. #include <entt/locator/locator.hpp>
  6. #include <entt/meta/factory.hpp>
  7. #include <entt/meta/meta.hpp>
  8. #include <entt/meta/resolve.hpp>
  9. #include "types.h"
  10. TEST(Lib, Meta) {
  11. using namespace entt::literals;
  12. ASSERT_FALSE(entt::resolve("position"_hs));
  13. userdata ud{entt::locator<entt::meta_ctx>::handle(), entt::meta_any{}};
  14. cr_plugin ctx;
  15. ctx.userdata = &ud;
  16. cr_plugin_load(ctx, PLUGIN);
  17. cr_plugin_update(ctx);
  18. entt::meta<double>().conv<int>();
  19. ASSERT_TRUE(entt::resolve("position"_hs));
  20. ASSERT_TRUE(entt::resolve("velocity"_hs));
  21. auto pos = entt::resolve("position"_hs).construct(42., 3.);
  22. auto vel = entt::resolve("velocity"_hs).construct();
  23. ASSERT_TRUE(pos && vel);
  24. ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
  25. ASSERT_NE(pos.type().data("y"_hs).get(pos).try_cast<int>(), nullptr);
  26. ASSERT_EQ(pos.type().data("x"_hs).get(pos).cast<int>(), 42);
  27. ASSERT_EQ(pos.type().data("y"_hs).get(pos).cast<int>(), 3);
  28. ASSERT_EQ(vel.type().data("dx"_hs).type(), entt::resolve<double>());
  29. ASSERT_TRUE(vel.type().data("dy"_hs).get(vel).allow_cast<double>());
  30. ASSERT_EQ(vel.type().data("dx"_hs).get(vel).cast<double>(), 0.);
  31. ASSERT_EQ(vel.type().data("dy"_hs).get(vel).cast<double>(), 0.);
  32. ASSERT_EQ(ud.any.type(), entt::resolve<int>());
  33. ASSERT_EQ(ud.any.cast<int>(), 42);
  34. // these objects have been initialized from a different context
  35. pos.emplace<void>();
  36. vel.emplace<void>();
  37. ud.any.emplace<void>();
  38. cr_plugin_close(ctx);
  39. ASSERT_FALSE(entt::resolve("position"_hs));
  40. ASSERT_FALSE(entt::resolve("velocity"_hs));
  41. }