diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4fb7429be..11db4eb43 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 $ 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 $ 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 diff --git a/test/entt/meta/fixture.cpp b/test/entt/meta/fixture.cpp new file mode 100644 index 000000000..4d34f88fd --- /dev/null +++ b/test/entt/meta/fixture.cpp @@ -0,0 +1,242 @@ +#include +#include +#include +#include +#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().conv(); + + entt::meta() + .type("char"_hs) + .prop(props::prop_int, 42) + .data<&set, &get>("value"_hs); + + entt::meta() + .data("prop_bool"_hs) + .prop(props::prop_int, 0) + .prop(props::prop_value, 3) + .data("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("key_only"_hs) + .prop([]() { return props::key_only; }) + .data<&set, &get>("value"_hs) + .data("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().data<0u>("min"_hs).data<100u>("max"_hs); + + entt::meta() + .type("base"_hs); + + entt::meta() + .type("derived"_hs) + .prop(props::prop_int, 99) + .base() + .ctor() + .prop(props::prop_bool, false) + .ctor<&derived_factory>() + .prop(props::prop_int, 42) + .conv<&derived_type::f>() + .conv<&derived_type::g>(); + + entt::meta() + .ctor<>() + .type("empty"_hs) + .dtor<&empty_type::destroy>(); + + entt::meta() + .type("fat"_hs) + .base() + .dtor<&fat_type::destroy>(); + + entt::meta() + .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() + .type("array"_hs) + .data<&array_type::global>("global"_hs) + .data<&array_type::local>("local"_hs); + + entt::meta() + .type("func"_hs) + .func(&func_type::f)>("f3"_hs) + .func(&func_type::f)>("f2"_hs) + .prop(props::prop_bool, false) + .func(&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() + .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("z_ro"_hs) + .data("value"_hs); + + entt::meta() + .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() + .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() + .type("concrete"_hs) + .base() + .base() + .func<&concrete_type::f>("f"_hs); +} + + +void Meta::SetUpAfterUnregistration() { + entt::meta().conv(); + + entt::meta() + .data("prop_bool"_hs) + .prop(props::prop_int, 0) + .prop(props::prop_value, 3); + + entt::meta() + .type("my_type"_hs) + .prop(props::prop_bool, false) + .ctor<>(); + + entt::meta() + .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; +} diff --git a/test/entt/meta/fixture.h b/test/entt/meta/fixture.h new file mode 100644 index 000000000..f555dda5a --- /dev/null +++ b/test/entt/meta/fixture.h @@ -0,0 +1,143 @@ +#ifndef ENTT_META_FIXTURE_H +#define ENTT_META_FIXTURE_H + +#include + +template +void set(Type &prop, Type value) { + prop = value; +} + +template +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 diff --git a/test/entt/meta/meta.cpp b/test/entt/meta/meta.cpp index d12cd8530..7d05c1f84 100644 --- a/test/entt/meta/meta.cpp +++ b/test/entt/meta/meta.cpp @@ -4,291 +4,10 @@ #include #include #include -#include #include #include #include - -template -void set(Type &prop, Type value) { - prop = value; -} - -template -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().conv(); - - entt::meta() - .type("char"_hs) - .prop(props::prop_int, 42) - .data<&set, &get>("value"_hs); - - entt::meta() - .data("prop_bool"_hs) - .prop(props::prop_int, 0) - .prop(props::prop_value, 3) - .data("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("key_only"_hs) - .prop([]() { return props::key_only; }) - .data<&set, &get>("value"_hs) - .data("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().data<0u>("min"_hs).data<100u>("max"_hs); - - entt::meta() - .type("base"_hs); - - entt::meta() - .type("derived"_hs) - .prop(props::prop_int, 99) - .base() - .ctor() - .prop(props::prop_bool, false) - .ctor<&derived_factory>() - .prop(props::prop_int, 42) - .conv<&derived_type::f>() - .conv<&derived_type::g>(); - - entt::meta() - .ctor<>() - .type("empty"_hs) - .dtor<&empty_type::destroy>(); - - entt::meta() - .type("fat"_hs) - .base() - .dtor<&fat_type::destroy>(); - - entt::meta() - .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() - .type("array"_hs) - .data<&array_type::global>("global"_hs) - .data<&array_type::local>("local"_hs); - - entt::meta() - .type("func"_hs) - .func(&func_type::f)>("f3"_hs) - .func(&func_type::f)>("f2"_hs) - .prop(props::prop_bool, false) - .func(&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() - .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("z_ro"_hs) - .data("value"_hs); - - entt::meta() - .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() - .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() - .type("concrete"_hs) - .base() - .base() - .func<&concrete_type::f>("f"_hs); - } - - static void SetUpAfterUnregistration() { - entt::meta().conv(); - - entt::meta() - .data("prop_bool"_hs) - .prop(props::prop_int, 0) - .prop(props::prop_value, 3); - - entt::meta() - .type("my_type"_hs) - .prop(props::prop_bool, false) - .ctor<>(); - - entt::meta() - .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(), entt::resolve_id("derived"_hs)); @@ -1768,7 +1487,7 @@ TEST_F(Meta, MetaTypeDetach) { ASSERT_EQ(entt::resolve().prop(props::prop_int).value().cast(), 42); ASSERT_TRUE(entt::resolve().data("value"_hs)); - entt::meta_factory().type("char"_hs); + entt::meta().type("char"_hs); ASSERT_TRUE(entt::resolve_id("char"_hs)); }