tomato-testing/test/entt/meta/meta_conv.cpp
Green Sky 5c7231b7a3 Squashed 'external/entt/entt/' content from commit fef92113
git-subtree-dir: external/entt/entt
git-subtree-split: fef921132cae7588213d0f9bcd2fb9c8ffd8b7fc
2023-07-25 11:29:51 +02:00

72 lines
1.8 KiB
C++

#include <utility>
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
#include <entt/locator/locator.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/node.hpp>
#include <entt/meta/resolve.hpp>
struct clazz_t {
clazz_t() = default;
operator int() const {
return value;
}
bool to_bool() const {
return (value != 0);
}
int value{};
};
double conv_to_double(const clazz_t &instance) {
return instance.value * 2.;
}
struct MetaConv: ::testing::Test {
void SetUp() override {
using namespace entt::literals;
entt::meta<clazz_t>()
.type("clazz"_hs)
.conv<int>()
.conv<&clazz_t::to_bool>()
.conv<conv_to_double>();
}
void TearDown() override {
entt::meta_reset();
}
};
TEST_F(MetaConv, Functionalities) {
auto any = entt::resolve<clazz_t>().construct();
any.cast<clazz_t &>().value = 42;
const auto as_int = std::as_const(any).allow_cast<int>();
const auto as_bool = std::as_const(any).allow_cast<bool>();
const auto as_double = std::as_const(any).allow_cast<double>();
ASSERT_FALSE(any.allow_cast<char>());
ASSERT_TRUE(as_int);
ASSERT_TRUE(as_bool);
ASSERT_TRUE(as_double);
ASSERT_EQ(as_int.cast<int>(), any.cast<clazz_t &>().operator int());
ASSERT_EQ(as_bool.cast<bool>(), any.cast<clazz_t &>().to_bool());
ASSERT_EQ(as_double.cast<double>(), conv_to_double(any.cast<clazz_t &>()));
}
TEST_F(MetaConv, ReRegistration) {
SetUp();
auto &&node = entt::internal::resolve<clazz_t>(entt::internal::meta_context::from(entt::locator<entt::meta_ctx>::value_or()));
ASSERT_TRUE(node.details);
ASSERT_FALSE(node.details->conv.empty());
ASSERT_EQ(node.details->conv.size(), 3u);
}