test: meta_custom

This commit is contained in:
Michele Caini
2024-07-23 11:40:14 +02:00
parent a8f988893e
commit ba63284d7a
3 changed files with 114 additions and 0 deletions

1
TODO
View File

@@ -42,3 +42,4 @@ TODO:
* fix cmake warning about FetchContent_Populate
* deprecate meta properties in favor of custom data
* make meta objects safe to use with null nodes
* natvis for meta_custom

View File

@@ -272,6 +272,7 @@ SETUP_BASIC_TEST(meta_container entt/meta/meta_container.cpp)
SETUP_BASIC_TEST(meta_context entt/meta/meta_context.cpp)
SETUP_BASIC_TEST(meta_conv entt/meta/meta_conv.cpp)
SETUP_BASIC_TEST(meta_ctor entt/meta/meta_ctor.cpp)
SETUP_BASIC_TEST(meta_custom entt/meta/meta_custom.cpp)
SETUP_BASIC_TEST(meta_data entt/meta/meta_data.cpp)
SETUP_BASIC_TEST(meta_dtor entt/meta/meta_dtor.cpp)
SETUP_BASIC_TEST(meta_func entt/meta/meta_func.cpp)

View File

@@ -0,0 +1,112 @@
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
#include <entt/locator/locator.hpp>
#include <entt/meta/context.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
#include "../../common/config.h"
struct clazz {
int i{0};
char j{1};
void f(int) {}
void g(char) {}
};
struct MetaCustom: ::testing::Test {
void SetUp() override {
using namespace entt::literals;
entt::meta<clazz>()
.type("clazz"_hs)
.custom<char>('c')
.data<&clazz::i>("i"_hs)
.custom<int>(2)
.data<&clazz::j>("j"_hs)
.func<&clazz::f>("f"_hs)
.custom<int>(3)
.func<&clazz::g>("g"_hs);
}
void TearDown() override {
entt::meta_reset();
}
};
using MetaCustomDeathTest = MetaCustom;
TEST_F(MetaCustom, Functionalities) {
entt::meta_custom custom{};
ASSERT_EQ(static_cast<const char *>(custom), nullptr);
custom = entt::resolve<clazz>().custom();
ASSERT_NE(static_cast<const char *>(custom), nullptr);
ASSERT_EQ(*static_cast<const char *>(custom), 'c');
ASSERT_EQ(static_cast<const char &>(custom), 'c');
}
ENTT_DEBUG_TEST_F(MetaCustomDeathTest, Functionalities) {
entt::meta_custom custom{};
ASSERT_DEATH([[maybe_unused]] const char &value = custom, "");
custom = entt::resolve<clazz>().custom();
ASSERT_DEATH([[maybe_unused]] const int &value = custom, "");
}
TEST_F(MetaCustom, Type) {
ASSERT_NE(static_cast<const char *>(entt::resolve<clazz>().custom()), nullptr);
ASSERT_EQ(*static_cast<const char *>(entt::resolve<clazz>().custom()), 'c');
ASSERT_EQ(static_cast<const char &>(entt::resolve<clazz>().custom()), 'c');
ASSERT_EQ(static_cast<const int *>(entt::resolve<clazz>().custom()), nullptr);
ASSERT_EQ(static_cast<const char *>(entt::resolve<int>().custom()), nullptr);
}
TEST_F(MetaCustom, Data) {
using namespace entt::literals;
ASSERT_NE(static_cast<const int *>(entt::resolve<clazz>().data("i"_hs).custom()), nullptr);
ASSERT_EQ(*static_cast<const int *>(entt::resolve<clazz>().data("i"_hs).custom()), 2);
ASSERT_EQ(static_cast<const int &>(entt::resolve<clazz>().data("i"_hs).custom()), 2);
ASSERT_EQ(static_cast<const char *>(entt::resolve<clazz>().data("i"_hs).custom()), nullptr);
ASSERT_EQ(static_cast<const int *>(entt::resolve<clazz>().data("j"_hs).custom()), nullptr);
}
TEST_F(MetaCustom, Func) {
using namespace entt::literals;
ASSERT_NE(static_cast<const int *>(entt::resolve<clazz>().func("f"_hs).custom()), nullptr);
ASSERT_EQ(*static_cast<const int *>(entt::resolve<clazz>().func("f"_hs).custom()), 3);
ASSERT_EQ(static_cast<const int &>(entt::resolve<clazz>().func("f"_hs).custom()), 3);
ASSERT_EQ(static_cast<const char *>(entt::resolve<clazz>().func("f"_hs).custom()), nullptr);
ASSERT_EQ(static_cast<const int *>(entt::resolve<clazz>().func("g"_hs).custom()), nullptr);
}
TEST_F(MetaCustom, ReRegistration) {
using namespace entt::literals;
SetUp();
auto type = entt::resolve<clazz>();
ASSERT_EQ(static_cast<const int *>(type.custom()), nullptr);
ASSERT_NE(static_cast<const char *>(type.custom()), nullptr);
ASSERT_EQ(*static_cast<const char *>(type.custom()), 'c');
entt::meta<clazz>().custom<int>(1);
type = entt::resolve<clazz>();
ASSERT_NE(static_cast<const int *>(type.custom()), nullptr);
ASSERT_EQ(static_cast<const char *>(type.custom()), nullptr);
ASSERT_EQ(*static_cast<const int *>(type.custom()), 1);
}