meta: split fixture and tests

This commit is contained in:
Michele Caini
2020-06-10 14:56:01 +02:00
parent b3a2f0e3a3
commit 9f18401581
4 changed files with 391 additions and 286 deletions

View File

@@ -70,7 +70,7 @@ function(SETUP_BASIC_TEST TEST_NAME TEST_SOURCES)
endfunction()
function(SETUP_LIB_TEST TEST_NAME)
add_library(_${TEST_NAME} SHARED lib/${TEST_NAME}/lib.cpp)
add_library(_${TEST_NAME} SHARED $<TARGET_OBJECTS:odr> lib/${TEST_NAME}/lib.cpp)
SETUP_TARGET(_${TEST_NAME})
SETUP_BASIC_TEST(lib_${TEST_NAME} lib/${TEST_NAME}/main.cpp)
target_compile_definitions(_${TEST_NAME} PRIVATE ENTT_API_EXPORT ${ARGV1})
@@ -79,7 +79,7 @@ function(SETUP_LIB_TEST TEST_NAME)
endfunction()
function(SETUP_PLUGIN_TEST TEST_NAME)
add_library(_${TEST_NAME} MODULE lib/${TEST_NAME}/plugin.cpp)
add_library(_${TEST_NAME} MODULE $<TARGET_OBJECTS:odr> lib/${TEST_NAME}/plugin.cpp)
SETUP_TARGET(_${TEST_NAME})
SETUP_BASIC_TEST(lib_${TEST_NAME} lib/${TEST_NAME}/main.cpp)
target_include_directories(_${TEST_NAME} PRIVATE ${cr_INCLUDE_DIR})
@@ -177,7 +177,8 @@ SETUP_BASIC_TEST(locator entt/locator/locator.cpp)
# Test meta
SETUP_BASIC_TEST(meta entt/meta/meta.cpp)
list(APPEND TEST_META_SOURCES entt/meta/meta.cpp entt/meta/fixture.cpp)
SETUP_BASIC_TEST(meta "${TEST_META_SOURCES}")
# Test process

242
test/entt/meta/fixture.cpp Normal file
View File

@@ -0,0 +1,242 @@
#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;
}

143
test/entt/meta/fixture.h Normal file
View File

@@ -0,0 +1,143 @@
#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

@@ -4,291 +4,10 @@
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
#include <entt/core/utility.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
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 &) {
++counter;
}
inline static int counter = 0;
};
struct fat_type: empty_type {
fat_type() = default;
fat_type(int *value)
: foo{value}, bar{value}
{}
int *foo{nullptr};
int *bar{nullptr};
bool operator==(const fat_type &other) const {
return foo == other.foo && bar == other.bar;
}
};
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 value, char character)
: i{value}, c{character}
{}
int f() const { return i; }
static char g(const derived_type &type) { return type.c; }
const int i{};
const char c{};
};
derived_type derived_factory(const base_type &, int value) {
return {derived_type{}, value, 'c'};
}
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 a, int b) { return f(a, b); }
int f(int a, int b) { value = a; return b*b; }
int f(int v) const { return v*v; }
void g(int v) { value = v*v; }
static int h(int &v) { return (v *= value); }
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;
};
struct setter_getter_type {
int value{};
int setter(int val) { return value = val; }
int getter() { return value; }
int setter_with_ref(const int &val) { return value = val; }
const int & getter_with_ref() { return value; }
static int static_setter(setter_getter_type &type, int value) { return type.value = value; }
static int static_getter(const setter_getter_type &type) { return type.value; }
};
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) { i = 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) { i = v*v; } // hide, it's ok :-)
void g(int v) override { i = -v; }
void h(char c) override { j = c; }
};
struct Meta: ::testing::Test {
static void 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);
}
static void 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 SetUp() override {
empty_type::counter = 0;
func_type::value = 0;
}
};
#include "fixture.h"
TEST_F(Meta, Resolve) {
ASSERT_EQ(entt::resolve<derived_type>(), entt::resolve_id("derived"_hs));
@@ -1768,7 +1487,7 @@ TEST_F(Meta, MetaTypeDetach) {
ASSERT_EQ(entt::resolve<char>().prop(props::prop_int).value().cast<int>(), 42);
ASSERT_TRUE(entt::resolve<char>().data("value"_hs));
entt::meta_factory<char>().type("char"_hs);
entt::meta<char>().type("char"_hs);
ASSERT_TRUE(entt::resolve_id("char"_hs));
}