test: updated tests for meta

This commit is contained in:
Michele Caini
2020-06-11 10:46:40 +02:00
parent 6ac582a771
commit c29b9cc937
5 changed files with 113 additions and 542 deletions

View File

@@ -187,9 +187,6 @@ SETUP_BASIC_TEST(meta_func entt/meta/meta_func.cpp)
SETUP_BASIC_TEST(meta_prop entt/meta/meta_prop.cpp)
SETUP_BASIC_TEST(meta_type entt/meta/meta_type.cpp)
list(APPEND TEST_META_SOURCES entt/meta/meta.cpp entt/meta/fixture.cpp)
SETUP_BASIC_TEST(meta "${TEST_META_SOURCES}")
# Test process
SETUP_BASIC_TEST(process entt/process/process.cpp)

View File

@@ -1,242 +0,0 @@
#include <entt/core/hashed_string.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
#include "fixture.h"
void empty_type::destroy(empty_type &) {
++counter;
}
fat_type::fat_type(int *value)
: foo{value}, bar{value}
{}
bool fat_type::operator==(const fat_type &other) const {
return foo == other.foo && bar == other.bar;
}
derived_type::derived_type(const base_type &, int value, char character)
: i{value}, c{character}
{}
int derived_type::f() const {
return i;
}
char derived_type::g(const derived_type &type) {
return type.c;
}
derived_type derived_factory(const base_type &, int value) {
return {derived_type{}, value, 'c'};
}
int func_type::f(const base_type &, int a, int b) {
return f(a, b);
}
int func_type::f(int a, int b) {
value = a;
return b*b;
}
int func_type::f(int v) const {
return v*v;
}
void func_type::g(int v) {
value = v*v;
}
int func_type::h(int &v) {
return (v *= value);
}
void func_type::k(int v) {
value = v;
}
int func_type::v(int v) const {
return (value = v);
}
int & func_type::a() const {
return value;
}
int setter_getter_type::setter(int val) {
return value = val;
}
int setter_getter_type::getter() {
return value;
}
int setter_getter_type::setter_with_ref(const int &val) {
return value = val;
}
const int & setter_getter_type::getter_with_ref() {
return value;
}
int setter_getter_type::static_setter(setter_getter_type &type, int value) {
return type.value = value;
}
int setter_getter_type::static_getter(const setter_getter_type &type) {
return type.value;
}
void an_abstract_type::f(int v) {
i = v;
}
void concrete_type::f(int v) {
i = v*v;
}
void concrete_type::g(int v) {
i = -v;
}
void concrete_type::h(char c) {
j = c;
}
void Meta::SetUpTestCase() {
entt::meta<double>().conv<int>();
entt::meta<char>()
.type("char"_hs)
.prop(props::prop_int, 42)
.data<&set<char>, &get<char>>("value"_hs);
entt::meta<props>()
.data<props::prop_bool>("prop_bool"_hs)
.prop(props::prop_int, 0)
.prop(props::prop_value, 3)
.data<props::prop_int>("prop_int"_hs)
.prop(std::make_tuple(std::make_pair(props::prop_bool, true), std::make_pair(props::prop_int, 0), std::make_pair(props::prop_value, 3)))
.prop(props::key_only)
.data<props::key_only>("key_only"_hs)
.prop([]() { return props::key_only; })
.data<&set<props>, &get<props>>("value"_hs)
.data<props::prop_list>("prop_list"_hs)
.props(std::make_pair(props::prop_bool, false), std::make_pair(props::prop_int, 0), std::make_pair(props::prop_value, 3), props::key_only);
entt::meta<unsigned int>().data<0u>("min"_hs).data<100u>("max"_hs);
entt::meta<base_type>()
.type("base"_hs);
entt::meta<derived_type>()
.type("derived"_hs)
.prop(props::prop_int, 99)
.base<base_type>()
.ctor<const base_type &, int, char>()
.prop(props::prop_bool, false)
.ctor<&derived_factory>()
.prop(props::prop_int, 42)
.conv<&derived_type::f>()
.conv<&derived_type::g>();
entt::meta<empty_type>()
.ctor<>()
.type("empty"_hs)
.dtor<&empty_type::destroy>();
entt::meta<fat_type>()
.type("fat"_hs)
.base<empty_type>()
.dtor<&fat_type::destroy>();
entt::meta<data_type>()
.type("data"_hs)
.data<&data_type::i, entt::as_ref_t>("i"_hs)
.prop(props::prop_int, 0)
.data<&data_type::j>("j"_hs)
.prop(props::prop_int, 1)
.data<&data_type::h>("h"_hs)
.prop(props::prop_int, 2)
.data<&data_type::k>("k"_hs)
.prop(props::prop_int, 3)
.data<&data_type::empty>("empty"_hs)
.data<&data_type::v, entt::as_void_t>("v"_hs);
entt::meta<array_type>()
.type("array"_hs)
.data<&array_type::global>("global"_hs)
.data<&array_type::local>("local"_hs);
entt::meta<func_type>()
.type("func"_hs)
.func<entt::overload<int(const base_type &, int, int)>(&func_type::f)>("f3"_hs)
.func<entt::overload<int(int, int)>(&func_type::f)>("f2"_hs)
.prop(props::prop_bool, false)
.func<entt::overload<int(int) const>(&func_type::f)>("f1"_hs)
.prop(props::prop_bool, false)
.func<&func_type::g>("g"_hs)
.prop(props::prop_bool, false)
.func<&func_type::h>("h"_hs)
.prop(props::prop_bool, false)
.func<&func_type::k>("k"_hs)
.prop(props::prop_bool, false)
.func<&func_type::v, entt::as_void_t>("v"_hs)
.func<&func_type::a, entt::as_ref_t>("a"_hs);
entt::meta<setter_getter_type>()
.type("setter_getter"_hs)
.data<&setter_getter_type::static_setter, &setter_getter_type::static_getter>("x"_hs)
.data<&setter_getter_type::setter, &setter_getter_type::getter>("y"_hs)
.data<&setter_getter_type::static_setter, &setter_getter_type::getter>("z"_hs)
.data<&setter_getter_type::setter_with_ref, &setter_getter_type::getter_with_ref>("w"_hs)
.data<nullptr, &setter_getter_type::getter>("z_ro"_hs)
.data<nullptr, &setter_getter_type::value>("value"_hs);
entt::meta<an_abstract_type>()
.type("an_abstract_type"_hs)
.prop(props::prop_bool, false)
.data<&an_abstract_type::i>("i"_hs)
.func<&an_abstract_type::f>("f"_hs)
.func<&an_abstract_type::g>("g"_hs);
entt::meta<another_abstract_type>()
.type("another_abstract_type"_hs)
.prop(props::prop_int, 42)
.data<&another_abstract_type::j>("j"_hs)
.func<&another_abstract_type::h>("h"_hs);
entt::meta<concrete_type>()
.type("concrete"_hs)
.base<an_abstract_type>()
.base<another_abstract_type>()
.func<&concrete_type::f>("f"_hs);
}
void Meta::SetUpAfterUnregistration() {
entt::meta<double>().conv<float>();
entt::meta<props>()
.data<props::prop_bool>("prop_bool"_hs)
.prop(props::prop_int, 0)
.prop(props::prop_value, 3);
entt::meta<derived_type>()
.type("my_type"_hs)
.prop(props::prop_bool, false)
.ctor<>();
entt::meta<another_abstract_type>()
.type("your_type"_hs)
.data<&another_abstract_type::j>("a_data_member"_hs)
.func<&another_abstract_type::h>("a_member_function"_hs);
}
void Meta::SetUp() {
empty_type::counter = 0;
func_type::value = 0;
}

View File

@@ -1,143 +0,0 @@
#ifndef ENTT_META_FIXTURE_H
#define ENTT_META_FIXTURE_H
#include <gtest/gtest.h>
template<typename Type>
void set(Type &prop, Type value) {
prop = value;
}
template<typename Type>
Type get(Type &prop) {
return prop;
}
enum class props {
prop_int,
prop_value,
prop_bool,
key_only,
prop_list
};
struct empty_type {
virtual ~empty_type() = default;
static void destroy(empty_type &);
inline static int counter = 0;
};
struct fat_type: empty_type {
fat_type() = default;
fat_type(int *);
bool operator==(const fat_type &) const;
int *foo{nullptr};
int *bar{nullptr};
};
union union_type {
int i;
double d;
};
struct base_type {
virtual ~base_type() = default;
};
struct derived_type: base_type {
derived_type() = default;
derived_type(const base_type &, int, char);
int f() const;
static char g(const derived_type &);
const int i{};
const char c{};
};
derived_type derived_factory(const base_type &, int);
struct data_type {
int i{0};
const int j{1};
inline static int h{2};
inline static const int k{3};
empty_type empty{};
int v{0};
};
struct array_type {
static inline int global[3];
int local[3];
};
struct func_type {
int f(const base_type &, int, int);
int f(int, int);
int f(int) const;
void g(int);
static int h(int &);
static void k(int);
int v(int) const;
int & a() const;
inline static int value = 0;
};
struct setter_getter_type {
int value{};
int setter(int);
int getter();
int setter_with_ref(const int &);
const int & getter_with_ref();
static int static_setter(setter_getter_type &, int);
static int static_getter(const setter_getter_type &);
};
struct not_comparable_type {
bool operator==(const not_comparable_type &) const = delete;
};
struct unmanageable_type {
unmanageable_type() = default;
unmanageable_type(const unmanageable_type &) = delete;
unmanageable_type(unmanageable_type &&) = delete;
unmanageable_type & operator=(const unmanageable_type &) = delete;
unmanageable_type & operator=(unmanageable_type &&) = delete;
};
bool operator!=(const not_comparable_type &, const not_comparable_type &) = delete;
struct an_abstract_type {
virtual ~an_abstract_type() = default;
void f(int v);
virtual void g(int) = 0;
int i{};
};
struct another_abstract_type {
virtual ~another_abstract_type() = default;
virtual void h(char) = 0;
char j{};
};
struct concrete_type: an_abstract_type, another_abstract_type {
void f(int v); // hide, it's ok :-)
void g(int v) override;
void h(char c) override;
};
struct Meta: ::testing::Test {
static void SetUpTestCase();
static void SetUpAfterUnregistration();
void SetUp() override;
};
#endif

View File

@@ -1,150 +0,0 @@
#include <utility>
#include <functional>
#include <type_traits>
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
#include "fixture.h"
TEST_F(Meta, EnumAndNamedConstants) {
auto type = entt::resolve<props>();
ASSERT_TRUE(type.data("prop_bool"_hs));
ASSERT_TRUE(type.data("prop_int"_hs));
ASSERT_EQ(type.data("prop_bool"_hs).type(), type);
ASSERT_EQ(type.data("prop_int"_hs).type(), type);
ASSERT_FALSE(type.data("prop_bool"_hs).set({}, props::prop_int));
ASSERT_FALSE(type.data("prop_int"_hs).set({}, props::prop_bool));
ASSERT_EQ(type.data("prop_bool"_hs).get({}).cast<props>(), props::prop_bool);
ASSERT_EQ(type.data("prop_int"_hs).get({}).cast<props>(), props::prop_int);
}
TEST_F(Meta, ArithmeticTypeAndNamedConstants) {
auto type = entt::resolve<unsigned int>();
ASSERT_TRUE(type.data("min"_hs));
ASSERT_TRUE(type.data("max"_hs));
ASSERT_EQ(type.data("min"_hs).type(), type);
ASSERT_EQ(type.data("max"_hs).type(), type);
ASSERT_FALSE(type.data("min"_hs).set({}, 100u));
ASSERT_FALSE(type.data("max"_hs).set({}, 0u));
ASSERT_EQ(type.data("min"_hs).get({}).cast<unsigned int>(), 0u);
ASSERT_EQ(type.data("max"_hs).get({}).cast<unsigned int>(), 100u);
}
TEST_F(Meta, Variables) {
auto p_data = entt::resolve<props>().data("value"_hs);
auto c_data = entt::resolve_id("char"_hs).data("value"_hs);
props prop{props::prop_int};
char c = 'c';
p_data.set(prop, props::prop_bool);
c_data.set(c, 'x');
ASSERT_EQ(p_data.get(prop).cast<props>(), props::prop_bool);
ASSERT_EQ(c_data.get(c).cast<char>(), 'x');
ASSERT_EQ(prop, props::prop_bool);
ASSERT_EQ(c, 'x');
}
TEST_F(Meta, PropertiesAndCornerCases) {
auto type = entt::resolve<props>();
ASSERT_EQ(type.data("prop_bool"_hs).prop(props::prop_int).value().cast<int>(), 0);
ASSERT_EQ(type.data("prop_bool"_hs).prop(props::prop_value).value().cast<int>(), 3);
ASSERT_EQ(type.data("prop_int"_hs).prop(props::prop_bool).value().cast<bool>(), true);
ASSERT_EQ(type.data("prop_int"_hs).prop(props::prop_int).value().cast<int>(), 0);
ASSERT_EQ(type.data("prop_int"_hs).prop(props::prop_value).value().cast<int>(), 3);
ASSERT_TRUE(type.data("prop_int"_hs).prop(props::key_only));
ASSERT_FALSE(type.data("prop_int"_hs).prop(props::key_only).value());
ASSERT_EQ(type.data("prop_list"_hs).prop(props::prop_bool).value().cast<bool>(), false);
ASSERT_EQ(type.data("prop_list"_hs).prop(props::prop_int).value().cast<int>(), 0);
ASSERT_EQ(type.data("prop_list"_hs).prop(props::prop_value).value().cast<int>(), 3);
ASSERT_TRUE(type.data("prop_list"_hs).prop(props::key_only));
ASSERT_FALSE(type.data("prop_list"_hs).prop(props::key_only).value());
}
TEST_F(Meta, Reset) {
ASSERT_NE(*entt::internal::meta_context::global(), nullptr);
entt::meta<char>().reset();
entt::meta<concrete_type>().reset();
entt::meta<setter_getter_type>().reset();
entt::meta<fat_type>().reset();
entt::meta<data_type>().reset();
entt::meta<func_type>().reset();
entt::meta<array_type>().reset();
entt::meta<double>().reset();
entt::meta<props>().reset();
entt::meta<base_type>().reset();
entt::meta<derived_type>().reset();
entt::meta<empty_type>().reset();
entt::meta<an_abstract_type>().reset();
entt::meta<another_abstract_type>().reset();
entt::meta<unsigned int>().reset();
ASSERT_FALSE(entt::resolve_id("char"_hs));
ASSERT_FALSE(entt::resolve_id("base"_hs));
ASSERT_FALSE(entt::resolve_id("derived"_hs));
ASSERT_FALSE(entt::resolve_id("empty"_hs));
ASSERT_FALSE(entt::resolve_id("fat"_hs));
ASSERT_FALSE(entt::resolve_id("data"_hs));
ASSERT_FALSE(entt::resolve_id("func"_hs));
ASSERT_FALSE(entt::resolve_id("setter_getter"_hs));
ASSERT_FALSE(entt::resolve_id("an_abstract_type"_hs));
ASSERT_FALSE(entt::resolve_id("another_abstract_type"_hs));
ASSERT_FALSE(entt::resolve_id("concrete"_hs));
ASSERT_EQ(*entt::internal::meta_context::global(), nullptr);
Meta::SetUpAfterUnregistration();
entt::meta_any any{42.};
ASSERT_TRUE(any);
ASSERT_FALSE(any.convert<int>());
ASSERT_TRUE(any.convert<float>());
ASSERT_FALSE(entt::resolve_id("derived"_hs));
ASSERT_TRUE(entt::resolve_id("my_type"_hs));
entt::resolve<derived_type>().prop([](auto prop) {
ASSERT_EQ(prop.key(), props::prop_bool);
ASSERT_FALSE(prop.value().template cast<bool>());
});
ASSERT_FALSE((entt::resolve<derived_type>().ctor<const base_type &, int, char>()));
ASSERT_TRUE((entt::resolve<derived_type>().ctor<>()));
ASSERT_TRUE(entt::resolve_id("your_type"_hs).data("a_data_member"_hs));
ASSERT_FALSE(entt::resolve_id("your_type"_hs).data("another_data_member"_hs));
ASSERT_TRUE(entt::resolve_id("your_type"_hs).func("a_member_function"_hs));
ASSERT_FALSE(entt::resolve_id("your_type"_hs).func("another_member_function"_hs));
}
TEST_F(Meta, ReRegistrationAfterReset) {
ASSERT_TRUE(entt::resolve<props>().data("prop_bool"_hs).prop(props::prop_int));
ASSERT_TRUE(entt::resolve<props>().data("prop_bool"_hs).prop(props::prop_value));
entt::meta<double>().reset();
entt::meta<props>().reset();
entt::meta<derived_type>().reset();
entt::meta<another_abstract_type>().reset();
Meta::SetUpAfterUnregistration();
ASSERT_TRUE(entt::resolve<props>().data("prop_bool"_hs).prop(props::prop_int));
ASSERT_TRUE(entt::resolve<props>().data("prop_bool"_hs).prop(props::prop_value));
}

View File

@@ -57,7 +57,8 @@ union union_t {
struct Meta: ::testing::Test {
static void SetUpTestCase() {
entt::meta<double>().conv<int>();
entt::meta<double>().type("double"_hs).conv<int>().data<&set<double>, &get<double>>("var"_hs);
entt::meta<unsigned int>().data<0u>("min"_hs).data<100u>("max"_hs);
entt::meta<base_t>().type("base"_hs).data<&base_t::value>("value"_hs);
entt::meta<derived_t>().type("derived"_hs).base<base_t>();
entt::meta<abstract_t>().func<&abstract_t::func>("func"_hs);
@@ -68,13 +69,13 @@ struct Meta: ::testing::Test {
.prop(property_t::random, 0)
.prop(property_t::value, 3)
.data<property_t::value>("value"_hs)
.prop(std::make_tuple(std::make_pair(property_t::random, true), std::make_pair(property_t::value, 0), std::make_pair(property_t::key_only, 3)))
.prop(std::make_tuple(std::make_pair(property_t::random, true), std::make_pair(property_t::value, 0), property_t::key_only))
.prop(property_t::list)
.data<property_t::key_only>("key_only"_hs)
.prop([]() { return property_t::key_only; })
.data<&set<property_t>, &get<property_t>>("wrap"_hs)
.data<property_t::list>("list"_hs)
.props(std::make_pair(property_t::random, false), std::make_pair(property_t::value, 0), property_t::key_only);
.props(std::make_pair(property_t::random, false), std::make_pair(property_t::value, 0), property_t::key_only)
.data<&set<property_t>, &get<property_t>>("var"_hs);
entt::meta<clazz_t>()
.type("clazz"_hs)
@@ -271,3 +272,111 @@ TEST_F(Meta, AbstractClass) {
ASSERT_EQ(instance.base_t::value, 'c');
ASSERT_EQ(instance.value, 42);
}
TEST_F(Meta, EnumAndNamedConstants) {
auto type = entt::resolve<property_t>();
ASSERT_TRUE(type.data("random"_hs));
ASSERT_TRUE(type.data("value"_hs));
ASSERT_EQ(type.data("random"_hs).type(), type);
ASSERT_EQ(type.data("value"_hs).type(), type);
ASSERT_FALSE(type.data("random"_hs).set({}, property_t::value));
ASSERT_FALSE(type.data("value"_hs).set({}, property_t::random));
ASSERT_EQ(type.data("random"_hs).get({}).cast<property_t>(), property_t::random);
ASSERT_EQ(type.data("value"_hs).get({}).cast<property_t>(), property_t::value);
}
TEST_F(Meta, ArithmeticTypeAndNamedConstants) {
auto type = entt::resolve<unsigned int>();
ASSERT_TRUE(type.data("min"_hs));
ASSERT_TRUE(type.data("max"_hs));
ASSERT_EQ(type.data("min"_hs).type(), type);
ASSERT_EQ(type.data("max"_hs).type(), type);
ASSERT_FALSE(type.data("min"_hs).set({}, 100u));
ASSERT_FALSE(type.data("max"_hs).set({}, 0u));
ASSERT_EQ(type.data("min"_hs).get({}).cast<unsigned int>(), 0u);
ASSERT_EQ(type.data("max"_hs).get({}).cast<unsigned int>(), 100u);
}
TEST_F(Meta, Variables) {
auto p_data = entt::resolve<property_t>().data("var"_hs);
auto d_data = entt::resolve_id("double"_hs).data("var"_hs);
property_t prop{property_t::key_only};
double d = 3.;
p_data.set(prop, property_t::random);
d_data.set(d, 42.);
ASSERT_EQ(p_data.get(prop).cast<property_t>(), property_t::random);
ASSERT_EQ(d_data.get(d).cast<double>(), 42.);
ASSERT_EQ(prop, property_t::random);
ASSERT_EQ(d, 42.);
}
TEST_F(Meta, PropertiesAndCornerCases) {
auto type = entt::resolve<property_t>();
ASSERT_EQ(type.data("random"_hs).prop(property_t::random).value().cast<int>(), 0);
ASSERT_EQ(type.data("random"_hs).prop(property_t::value).value().cast<int>(), 3);
ASSERT_EQ(type.data("value"_hs).prop(property_t::random).value().cast<bool>(), true);
ASSERT_EQ(type.data("value"_hs).prop(property_t::value).value().cast<int>(), 0);
ASSERT_TRUE(type.data("value"_hs).prop(property_t::key_only));
ASSERT_FALSE(type.data("value"_hs).prop(property_t::key_only).value());
ASSERT_TRUE(type.data("key_only"_hs).prop(property_t::key_only));
ASSERT_FALSE(type.data("key_only"_hs).prop(property_t::key_only).value());
ASSERT_EQ(type.data("list"_hs).prop(property_t::random).value().cast<bool>(), false);
ASSERT_EQ(type.data("list"_hs).prop(property_t::value).value().cast<int>(), 0);
ASSERT_TRUE(type.data("list"_hs).prop(property_t::key_only));
ASSERT_FALSE(type.data("list"_hs).prop(property_t::key_only).value());
}
TEST_F(Meta, ResetAndReRegistrationAfterReset) {
ASSERT_NE(*entt::internal::meta_context::global(), nullptr);
entt::meta<double>().reset();
entt::meta<unsigned int>().reset();
entt::meta<base_t>().reset();
entt::meta<derived_t>().reset();
entt::meta<abstract_t>().reset();
entt::meta<concrete_t>().reset();
entt::meta<property_t>().reset();
entt::meta<clazz_t>().reset();
ASSERT_FALSE(entt::resolve_id("double"_hs));
ASSERT_FALSE(entt::resolve_id("base"_hs));
ASSERT_FALSE(entt::resolve_id("derived"_hs));
ASSERT_FALSE(entt::resolve_id("clazz"_hs));
ASSERT_EQ(*entt::internal::meta_context::global(), nullptr);
ASSERT_FALSE(entt::resolve<clazz_t>().prop(property_t::value));
ASSERT_FALSE(entt::resolve<clazz_t>().ctor());
ASSERT_FALSE(entt::resolve<clazz_t>().data("value"_hs));
ASSERT_FALSE(entt::resolve<clazz_t>().func("member"_hs));
entt::meta<double>().type("double"_hs).conv<float>();
entt::meta_any any{42.};
ASSERT_TRUE(any);
ASSERT_FALSE(any.convert<int>());
ASSERT_TRUE(any.convert<float>());
ASSERT_FALSE(entt::resolve_id("derived"_hs));
ASSERT_TRUE(entt::resolve_id("double"_hs));
entt::meta<property_t>().data<property_t::random>("rand"_hs).prop(property_t::value, 42).prop(property_t::random, 3);
ASSERT_TRUE(entt::resolve<property_t>().data("rand"_hs).prop(property_t::value));
ASSERT_TRUE(entt::resolve<property_t>().data("rand"_hs).prop(property_t::random));
}