Browse Source

tests for meta policies

Michele Caini 6 years ago
parent
commit
73ef0222ac
3 changed files with 59 additions and 18 deletions
  1. 1 0
      TODO
  2. 3 11
      src/entt/meta/policy.hpp
  3. 55 7
      test/entt/meta/meta.cpp

+ 1 - 0
TODO

@@ -30,3 +30,4 @@ TODO
   - clone is just clone, creating new entity. But yeah, both "clone-all", and "clone-components"
   - for "apply", again both All and Components, and maybe an enum of what kind of apply "dont overwrite, overwrite, add-only"
 * test meta policies
+* doc meta policies

+ 3 - 11
src/entt/meta/policy.hpp

@@ -5,14 +5,6 @@
 namespace entt {
 
 
-/*! @brief Empty class type used for disambiguation. */
-struct as_is_t {};
-
-
-/*! @brief Disambiguation tag. */
-constexpr as_is_t as_is;
-
-
 /*! @brief Empty class type used for disambiguation. */
 struct as_alias_t {};
 
@@ -22,11 +14,11 @@ constexpr as_alias_t as_alias;
 
 
 /*! @brief Empty class type used for disambiguation. */
-struct as_void_t {};
+struct as_is_t {};
 
 
-/*! @brief Disambiguation tag. */
-constexpr as_void_t as_void;
+/*! @brief Empty class type used for disambiguation. */
+struct as_void_t {};
 
 
 }

+ 55 - 7
test/entt/meta/meta.cpp

@@ -73,6 +73,7 @@ struct data_type {
     inline static int h{2};
     inline static const int k{3};
     empty_type empty{};
+    int v{0};
 };
 
 struct array_type {
@@ -89,6 +90,9 @@ struct func_type {
     static int h(int v) { return v; }
     static void k(int v) { value = v; }
 
+    int v(int v) const { return (value = v); }
+    int & a() const { return value; }
+
     inline static int value = 0;
 };
 
@@ -159,11 +163,12 @@ struct Meta: public ::testing::Test {
                 .dtor<&fat_type::destroy>();
 
         entt::reflect<data_type>("data"_hs)
-                .data<&data_type::i>("i"_hs, std::make_pair(properties::prop_int, 0))
+                .data<&data_type::i, entt::as_alias_t>("i"_hs, std::make_pair(properties::prop_int, 0))
                 .data<&data_type::j>("j"_hs, std::make_pair(properties::prop_int, 1))
                 .data<&data_type::h>("h"_hs, std::make_pair(properties::prop_int, 2))
                 .data<&data_type::k>("k"_hs, std::make_pair(properties::prop_int, 3))
-                .data<&data_type::empty>("empty"_hs);
+                .data<&data_type::empty>("empty"_hs)
+                .data<&data_type::v, entt::as_void_t>("v"_hs);
 
         entt::reflect<array_type>("array"_hs)
                 .data<&array_type::global>("global"_hs)
@@ -175,7 +180,9 @@ struct Meta: public ::testing::Test {
                 .func<entt::overload<int(int) const>(&func_type::f)>("f1"_hs, std::make_pair(properties::prop_bool, false))
                 .func<&func_type::g>("g"_hs, std::make_pair(properties::prop_bool, false))
                 .func<&func_type::h>("h"_hs, std::make_pair(properties::prop_bool, false))
-                .func<&func_type::k>("k"_hs, std::make_pair(properties::prop_bool, false));
+                .func<&func_type::k>("k"_hs, std::make_pair(properties::prop_bool, false))
+                .func<&func_type::v, entt::as_void_t>("v"_hs)
+                .func<&func_type::a, entt::as_alias_t>("a"_hs);
 
         entt::reflect<setter_getter_type>("setter_getter"_hs)
                 .data<&setter_getter_type::static_setter, &setter_getter_type::static_getter>("x"_hs)
@@ -285,7 +292,7 @@ TEST_F(Meta, MetaAnySBOInPlaceTypeConstruction) {
     ASSERT_NE(entt::meta_any{3}, any);
 }
 
-TEST_F(Meta, MetaAnySBOInPlaceConstruction) {
+TEST_F(Meta, MetaAnySBOAsAliasConstruction) {
     int value = 3;
     int other = 42;
     entt::meta_any any{entt::as_alias, value};
@@ -392,7 +399,7 @@ TEST_F(Meta, MetaAnyNoSBOInPlaceTypeConstruction) {
     ASSERT_NE(entt::meta_any{fat_type{}}, any);
 }
 
-TEST_F(Meta, MetaAnyNoSBOInPlaceConstruction) {
+TEST_F(Meta, MetaAnyNoSBOAsAliasConstruction) {
     int value = 3;
     fat_type instance{&value};
     entt::meta_any any{entt::as_alias, instance};
@@ -1377,6 +1384,29 @@ TEST_F(Meta, MetaDataArray) {
     ASSERT_EQ(data.get(instance, 2).cast<int>(), 9);
 }
 
+TEST_F(Meta, MetaDataAsVoid) {
+    auto data = entt::resolve<data_type>().data("v"_hs);
+    data_type instance{};
+
+    ASSERT_TRUE(data.set(instance, 42));
+    ASSERT_EQ(instance.v, 42);
+    ASSERT_EQ(data.get(instance), entt::meta_any{std::in_place_type<void>});
+}
+
+TEST_F(Meta, MetaDataAsAlias) {
+    data_type instance{};
+    auto h_data = entt::resolve<data_type>().data("h"_hs);
+    auto i_data = entt::resolve<data_type>().data("i"_hs);
+
+    h_data.get(instance).cast<int>() = 3;
+    i_data.get(instance).cast<int>() = 3;
+
+    ASSERT_EQ(h_data.type(), entt::resolve<int>());
+    ASSERT_EQ(i_data.type(), entt::resolve<int>());
+    ASSERT_NE(instance.h, 3);
+    ASSERT_EQ(instance.i, 3);
+}
+
 TEST_F(Meta, MetaFunc) {
     auto func = entt::resolve<func_type>().func("f2"_hs);
     func_type instance{};
@@ -1588,6 +1618,24 @@ TEST_F(Meta, MetaFuncCastAndConvert) {
     ASSERT_EQ(any.cast<int>(), 9);
 }
 
+TEST_F(Meta, MetaFuncAsVoid) {
+    auto func = entt::resolve<func_type>().func("v"_hs);
+    func_type instance{};
+
+    ASSERT_EQ(func.invoke(instance, 42), entt::meta_any{std::in_place_type<void>});
+    ASSERT_EQ(func.ret(), entt::resolve<void>());
+    ASSERT_EQ(instance.value, 42);
+}
+
+TEST_F(Meta, MetaFuncAsAlias) {
+    func_type instance{};
+    auto func = entt::resolve<func_type>().func("a"_hs);
+    func.invoke(instance).cast<int>() = 3;
+
+    ASSERT_EQ(func.ret(), entt::resolve<int>());
+    ASSERT_EQ(instance.value, 3);
+}
+
 TEST_F(Meta, MetaType) {
     auto type = entt::resolve<derived_type>();
 
@@ -1685,7 +1733,7 @@ TEST_F(Meta, MetaTypeData) {
         ++counter;
     });
 
-    ASSERT_EQ(counter, 5);
+    ASSERT_EQ(counter, 6);
     ASSERT_TRUE(type.data("i"_hs));
 }
 
@@ -1697,7 +1745,7 @@ TEST_F(Meta, MetaTypeFunc) {
         ++counter;
     });
 
-    ASSERT_EQ(counter, 6);
+    ASSERT_EQ(counter, 8);
     ASSERT_TRUE(type.func("f1"_hs));
 }