test: added meta_base

This commit is contained in:
Michele Caini
2020-06-10 15:51:10 +02:00
parent cedebf8145
commit 1da07ba60b
5 changed files with 122 additions and 105 deletions

View File

@@ -177,10 +177,12 @@ SETUP_BASIC_TEST(locator entt/locator/locator.cpp)
# Test meta
list(APPEND TEST_META_SOURCES entt/meta/meta.cpp entt/meta/fixture.cpp)
SETUP_BASIC_TEST(meta_any entt/meta/meta_any.cpp)
SETUP_BASIC_TEST(meta_base entt/meta/meta_base.cpp)
SETUP_BASIC_TEST(meta_basic entt/meta/meta_basic.cpp)
SETUP_BASIC_TEST(meta_prop entt/meta/meta_prop.cpp)
list(APPEND TEST_META_SOURCES entt/meta/meta.cpp entt/meta/fixture.cpp)
SETUP_BASIC_TEST(meta "${TEST_META_SOURCES}")
# Test process

View File

@@ -9,16 +9,6 @@
#include <entt/meta/resolve.hpp>
#include "fixture.h"
TEST_F(Meta, MetaBase) {
auto base = entt::resolve<derived_type>().base("base"_hs);
derived_type derived{};
ASSERT_TRUE(base);
ASSERT_EQ(base.parent(), entt::resolve_id("derived"_hs));
ASSERT_EQ(base.type(), entt::resolve<base_type>());
ASSERT_EQ(base.cast(&derived), static_cast<base_type *>(&derived));
}
TEST_F(Meta, MetaConv) {
auto conv = entt::resolve<double>().conv<int>();
double value = 3.;

View File

@@ -4,23 +4,23 @@
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
struct empty_type {
virtual ~empty_type() = default;
static void destroy(empty_type &) {
struct empty_t {
virtual ~empty_t() = default;
static void destroy(empty_t &) {
++counter;
}
inline static int counter = 0;
};
struct fat_type: empty_type {
fat_type() = default;
struct fat_t: empty_t {
fat_t() = default;
fat_type(int *value)
fat_t(int *value)
: foo{value}, bar{value}
{}
bool operator==(const fat_type &other) const {
bool operator==(const fat_t &other) const {
return foo == other.foo && bar == other.bar;
}
@@ -28,27 +28,27 @@ struct fat_type: empty_type {
int *bar{nullptr};
};
struct not_comparable_type {
bool operator==(const not_comparable_type &) const = delete;
struct not_comparable_t {
bool operator==(const not_comparable_t &) 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;
struct unmanageable_t {
unmanageable_t() = default;
unmanageable_t(const unmanageable_t &) = delete;
unmanageable_t(unmanageable_t &&) = delete;
unmanageable_t & operator=(const unmanageable_t &) = delete;
unmanageable_t & operator=(unmanageable_t &&) = delete;
};
struct Meta: ::testing::Test {
static void SetUpTestCase() {
entt::meta<double>().conv<int>();
entt::meta<empty_type>().dtor<&empty_type::destroy>();
entt::meta<fat_type>().base<empty_type>().dtor<&fat_type::destroy>();
entt::meta<empty_t>().dtor<&empty_t::destroy>();
entt::meta<fat_t>().base<empty_t>().dtor<&fat_t::destroy>();
}
void SetUp() override {
empty_type::counter = 0;
empty_t::counter = 0;
}
};
@@ -65,15 +65,15 @@ TEST_F(Meta, MetaAnySBO) {
TEST_F(Meta, MetaAnyNoSBO) {
int value = 42;
fat_type instance{&value};
fat_t instance{&value};
entt::meta_any any{instance};
ASSERT_TRUE(any);
ASSERT_FALSE(any.try_cast<std::size_t>());
ASSERT_EQ(any.cast<fat_type>(), instance);
ASSERT_EQ(any.cast<fat_t>(), instance);
ASSERT_NE(any.data(), nullptr);
ASSERT_EQ(any, entt::meta_any{instance});
ASSERT_NE(fat_type{}, any);
ASSERT_NE(fat_t{}, any);
}
TEST_F(Meta, MetaAnyEmpty) {
@@ -178,49 +178,49 @@ TEST_F(Meta, MetaAnySBODirectAssignment) {
TEST_F(Meta, MetaAnyNoSBOInPlaceTypeConstruction) {
int value = 42;
fat_type instance{&value};
entt::meta_any any{std::in_place_type<fat_type>, instance};
fat_t instance{&value};
entt::meta_any any{std::in_place_type<fat_t>, instance};
ASSERT_TRUE(any);
ASSERT_FALSE(any.try_cast<std::size_t>());
ASSERT_EQ(any.cast<fat_type>(), instance);
ASSERT_EQ(any.cast<fat_t>(), instance);
ASSERT_NE(any.data(), nullptr);
ASSERT_EQ(any, (entt::meta_any{std::in_place_type<fat_type>, instance}));
ASSERT_EQ(any, (entt::meta_any{std::in_place_type<fat_t>, instance}));
ASSERT_EQ(any, entt::meta_any{instance});
ASSERT_NE(entt::meta_any{fat_type{}}, any);
ASSERT_NE(entt::meta_any{fat_t{}}, any);
}
TEST_F(Meta, MetaAnyNoSBOAsAliasConstruction) {
int value = 3;
fat_type instance{&value};
fat_t instance{&value};
entt::meta_any any{std::ref(instance)};
ASSERT_TRUE(any);
ASSERT_FALSE(any.try_cast<std::size_t>());
ASSERT_EQ(any.cast<fat_type>(), instance);
ASSERT_EQ(any.cast<fat_t>(), instance);
ASSERT_NE(any.data(), nullptr);
ASSERT_EQ(any, (entt::meta_any{std::ref(instance)}));
ASSERT_EQ(any, entt::meta_any{instance});
ASSERT_NE(entt::meta_any{fat_type{}}, any);
ASSERT_NE(entt::meta_any{fat_t{}}, any);
}
TEST_F(Meta, MetaAnyNoSBOCopyConstruction) {
int value = 42;
fat_type instance{&value};
fat_t instance{&value};
entt::meta_any any{instance};
entt::meta_any other{any};
ASSERT_TRUE(any);
ASSERT_TRUE(other);
ASSERT_FALSE(other.try_cast<std::size_t>());
ASSERT_EQ(other.cast<fat_type>(), instance);
ASSERT_EQ(other.cast<fat_t>(), instance);
ASSERT_EQ(other, entt::meta_any{instance});
ASSERT_NE(other, fat_type{});
ASSERT_NE(other, fat_t{});
}
TEST_F(Meta, MetaAnyNoSBOCopyAssignment) {
int value = 42;
fat_type instance{&value};
fat_t instance{&value};
entt::meta_any any{instance};
entt::meta_any other{3};
@@ -229,28 +229,28 @@ TEST_F(Meta, MetaAnyNoSBOCopyAssignment) {
ASSERT_TRUE(any);
ASSERT_TRUE(other);
ASSERT_FALSE(other.try_cast<std::size_t>());
ASSERT_EQ(other.cast<fat_type>(), instance);
ASSERT_EQ(other.cast<fat_t>(), instance);
ASSERT_EQ(other, entt::meta_any{instance});
ASSERT_NE(other, fat_type{});
ASSERT_NE(other, fat_t{});
}
TEST_F(Meta, MetaAnyNoSBOMoveConstruction) {
int value = 42;
fat_type instance{&value};
fat_t instance{&value};
entt::meta_any any{instance};
entt::meta_any other{std::move(any)};
ASSERT_FALSE(any);
ASSERT_TRUE(other);
ASSERT_FALSE(other.try_cast<std::size_t>());
ASSERT_EQ(other.cast<fat_type>(), instance);
ASSERT_EQ(other.cast<fat_t>(), instance);
ASSERT_EQ(other, entt::meta_any{instance});
ASSERT_NE(other, fat_type{});
ASSERT_NE(other, fat_t{});
}
TEST_F(Meta, MetaAnyNoSBOMoveAssignment) {
int value = 42;
fat_type instance{&value};
fat_t instance{&value};
entt::meta_any any{instance};
entt::meta_any other{3};
@@ -259,20 +259,20 @@ TEST_F(Meta, MetaAnyNoSBOMoveAssignment) {
ASSERT_FALSE(any);
ASSERT_TRUE(other);
ASSERT_FALSE(other.try_cast<std::size_t>());
ASSERT_EQ(other.cast<fat_type>(), instance);
ASSERT_EQ(other.cast<fat_t>(), instance);
ASSERT_EQ(other, entt::meta_any{instance});
ASSERT_NE(other, fat_type{});
ASSERT_NE(other, fat_t{});
}
TEST_F(Meta, MetaAnyNoSBODirectAssignment) {
int value = 42;
entt::meta_any any{};
any = fat_type{&value};
any = fat_t{&value};
ASSERT_FALSE(any.try_cast<std::size_t>());
ASSERT_EQ(any.cast<fat_type>(), fat_type{&value});
ASSERT_EQ(any, entt::meta_any{fat_type{&value}});
ASSERT_NE(fat_type{}, any);
ASSERT_EQ(any.cast<fat_t>(), fat_t{&value});
ASSERT_EQ(any, entt::meta_any{fat_t{&value}});
ASSERT_NE(fat_t{}, any);
}
TEST_F(Meta, MetaAnyVoidInPlaceTypeConstruction) {
@@ -342,7 +342,7 @@ TEST_F(Meta, MetaAnySBOMoveInvalidate) {
TEST_F(Meta, MetaAnyNoSBOMoveInvalidate) {
int value = 42;
fat_type instance{&value};
fat_t instance{&value};
entt::meta_any any{instance};
entt::meta_any other{std::move(any)};
entt::meta_any valid = std::move(other);
@@ -363,15 +363,15 @@ TEST_F(Meta, MetaAnyVoidMoveInvalidate) {
}
TEST_F(Meta, MetaAnySBODestruction) {
ASSERT_EQ(empty_type::counter, 0);
{ entt::meta_any any{empty_type{}}; }
ASSERT_EQ(empty_type::counter, 1);
ASSERT_EQ(empty_t::counter, 0);
{ entt::meta_any any{empty_t{}}; }
ASSERT_EQ(empty_t::counter, 1);
}
TEST_F(Meta, MetaAnyNoSBODestruction) {
ASSERT_EQ(fat_type::counter, 0);
{ entt::meta_any any{fat_type{}}; }
ASSERT_EQ(fat_type::counter, 1);
ASSERT_EQ(fat_t::counter, 0);
{ entt::meta_any any{fat_t{}}; }
ASSERT_EQ(fat_t::counter, 1);
}
TEST_F(Meta, MetaAnyVoidDestruction) {
@@ -416,13 +416,13 @@ TEST_F(Meta, MetaAnySBOSwap) {
TEST_F(Meta, MetaAnyNoSBOSwap) {
int i, j;
entt::meta_any lhs{fat_type{&i}};
entt::meta_any rhs{fat_type{&j}};
entt::meta_any lhs{fat_t{&i}};
entt::meta_any rhs{fat_t{&j}};
std::swap(lhs, rhs);
ASSERT_EQ(lhs.cast<fat_type>().foo, &j);
ASSERT_EQ(rhs.cast<fat_type>().bar, &i);
ASSERT_EQ(lhs.cast<fat_t>().foo, &j);
ASSERT_EQ(rhs.cast<fat_t>().bar, &i);
}
TEST_F(Meta, MetaAnyVoidSwap) {
@@ -437,16 +437,16 @@ TEST_F(Meta, MetaAnyVoidSwap) {
TEST_F(Meta, MetaAnySBOWithNoSBOSwap) {
int value = 42;
entt::meta_any lhs{fat_type{&value}};
entt::meta_any lhs{fat_t{&value}};
entt::meta_any rhs{'c'};
std::swap(lhs, rhs);
ASSERT_FALSE(lhs.try_cast<fat_type>());
ASSERT_FALSE(lhs.try_cast<fat_t>());
ASSERT_EQ(lhs.cast<char>(), 'c');
ASSERT_FALSE(rhs.try_cast<char>());
ASSERT_EQ(rhs.cast<fat_type>().foo, &value);
ASSERT_EQ(rhs.cast<fat_type>().bar, &value);
ASSERT_EQ(rhs.cast<fat_t>().foo, &value);
ASSERT_EQ(rhs.cast<fat_t>().bar, &value);
}
TEST_F(Meta, MetaAnySBOWithEmptySwap) {
@@ -476,30 +476,30 @@ TEST_F(Meta, MetaAnySBOWithVoidSwap) {
TEST_F(Meta, MetaAnyNoSBOWithEmptySwap) {
int i;
entt::meta_any lhs{fat_type{&i}};
entt::meta_any lhs{fat_t{&i}};
entt::meta_any rhs{};
std::swap(lhs, rhs);
ASSERT_EQ(rhs.cast<fat_type>().bar, &i);
ASSERT_EQ(rhs.cast<fat_t>().bar, &i);
std::swap(lhs, rhs);
ASSERT_EQ(lhs.cast<fat_type>().bar, &i);
ASSERT_EQ(lhs.cast<fat_t>().bar, &i);
}
TEST_F(Meta, MetaAnyNoSBOWithVoidSwap) {
int i;
entt::meta_any lhs{fat_type{&i}};
entt::meta_any lhs{fat_t{&i}};
entt::meta_any rhs{std::in_place_type<void>};
std::swap(lhs, rhs);
ASSERT_EQ(rhs.cast<fat_type>().bar, &i);
ASSERT_EQ(rhs.cast<fat_t>().bar, &i);
std::swap(lhs, rhs);
ASSERT_EQ(lhs.cast<fat_type>().bar, &i);
ASSERT_EQ(lhs.cast<fat_t>().bar, &i);
}
TEST_F(Meta, MetaAnyComparable) {
@@ -518,14 +518,14 @@ TEST_F(Meta, MetaAnyComparable) {
}
TEST_F(Meta, MetaAnyNotComparable) {
entt::meta_any any{not_comparable_type{}};
entt::meta_any any{not_comparable_t{}};
ASSERT_EQ(any, any);
ASSERT_NE(any, entt::meta_any{not_comparable_type{}});
ASSERT_NE(any, entt::meta_any{not_comparable_t{}});
ASSERT_NE(entt::meta_any{}, any);
ASSERT_TRUE(any == any);
ASSERT_FALSE(any == entt::meta_any{not_comparable_type{}});
ASSERT_FALSE(any == entt::meta_any{not_comparable_t{}});
ASSERT_TRUE(entt::meta_any{} != any);
}
@@ -545,27 +545,27 @@ TEST_F(Meta, MetaAnyCompareVoid) {
}
TEST_F(Meta, MetaAnyTryCast) {
entt::meta_any any{fat_type{}};
entt::meta_any any{fat_t{}};
ASSERT_TRUE(any);
ASSERT_EQ(any.type(), entt::resolve<fat_type>());
ASSERT_EQ(any.type(), entt::resolve<fat_t>());
ASSERT_EQ(any.try_cast<void>(), nullptr);
ASSERT_NE(any.try_cast<empty_type>(), nullptr);
ASSERT_EQ(any.try_cast<fat_type>(), any.data());
ASSERT_EQ(std::as_const(any).try_cast<empty_type>(), any.try_cast<empty_type>());
ASSERT_EQ(std::as_const(any).try_cast<fat_type>(), any.data());
ASSERT_NE(any.try_cast<empty_t>(), nullptr);
ASSERT_EQ(any.try_cast<fat_t>(), any.data());
ASSERT_EQ(std::as_const(any).try_cast<empty_t>(), any.try_cast<empty_t>());
ASSERT_EQ(std::as_const(any).try_cast<fat_t>(), any.data());
}
TEST_F(Meta, MetaAnyCast) {
entt::meta_any any{fat_type{}};
entt::meta_any any{fat_t{}};
ASSERT_TRUE(any);
ASSERT_EQ(any.type(), entt::resolve<fat_type>());
ASSERT_EQ(any.type(), entt::resolve<fat_t>());
ASSERT_EQ(any.try_cast<std::size_t>(), nullptr);
ASSERT_NE(any.try_cast<empty_type>(), nullptr);
ASSERT_EQ(any.try_cast<fat_type>(), any.data());
ASSERT_EQ(std::as_const(any).try_cast<empty_type>(), any.try_cast<empty_type>());
ASSERT_EQ(std::as_const(any).try_cast<fat_type>(), any.data());
ASSERT_NE(any.try_cast<empty_t>(), nullptr);
ASSERT_EQ(any.try_cast<fat_t>(), any.data());
ASSERT_EQ(std::as_const(any).try_cast<empty_t>(), any.try_cast<empty_t>());
ASSERT_EQ(std::as_const(any).try_cast<fat_t>(), any.data());
}
TEST_F(Meta, MetaAnyConvert) {
@@ -601,7 +601,7 @@ TEST_F(Meta, MetaAnyConstConvert) {
}
TEST_F(Meta, MetaAnyUnmanageableType) {
unmanageable_type instance;
unmanageable_t instance;
entt::meta_any any{std::ref(instance)};
entt::meta_any other = any;
@@ -610,14 +610,14 @@ TEST_F(Meta, MetaAnyUnmanageableType) {
ASSERT_TRUE(any);
ASSERT_TRUE(other);
ASSERT_EQ(any.type(), entt::resolve<unmanageable_type>());
ASSERT_EQ(any.type(), entt::resolve<unmanageable_t>());
ASSERT_NE(any.data(), nullptr);
ASSERT_EQ(any.try_cast<int>(), nullptr);
ASSERT_NE(any.try_cast<unmanageable_type>(), nullptr);
ASSERT_NE(any.try_cast<unmanageable_t>(), nullptr);
ASSERT_TRUE(any.convert<unmanageable_type>());
ASSERT_TRUE(any.convert<unmanageable_t>());
ASSERT_FALSE(any.convert<int>());
ASSERT_TRUE(std::as_const(any).convert<unmanageable_type>());
ASSERT_TRUE(std::as_const(any).convert<unmanageable_t>());
ASSERT_FALSE(std::as_const(any).convert<int>());
}

View File

@@ -0,0 +1,25 @@
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
struct base_t {};
struct derived_t: base_t {};
struct Meta: ::testing::Test {
static void SetUpTestCase() {
entt::meta<base_t>().type("base"_hs);
entt::meta<derived_t>().type("derived"_hs).base<base_t>();
}
};
TEST_F(Meta, MetaBase) {
auto base = entt::resolve<derived_t>().base("base"_hs);
derived_t derived{};
ASSERT_TRUE(base);
ASSERT_EQ(base.parent(), entt::resolve_id("derived"_hs));
ASSERT_EQ(base.type(), entt::resolve<base_t>());
ASSERT_EQ(base.cast(&derived), static_cast<base_t *>(&derived));
}

View File

@@ -4,20 +4,20 @@
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
struct base_1 {};
struct base_2 {};
struct derived: base_1, base_2 {};
struct base_1_t {};
struct base_2_t {};
struct derived_t: base_1_t, base_2_t {};
struct Meta: ::testing::Test {
static void SetUpTestCase() {
entt::meta<base_1>().prop("int"_hs, 42);
entt::meta<base_2>().prop("bool"_hs, false);
entt::meta<derived>().base<base_1>().base<base_2>();
entt::meta<base_1_t>().prop("int"_hs, 42);
entt::meta<base_2_t>().prop("bool"_hs, false);
entt::meta<derived_t>().base<base_1_t>().base<base_2_t>();
}
};
TEST_F(Meta, MetaProp) {
auto prop = entt::resolve<base_1>().prop("int"_hs);
auto prop = entt::resolve<base_1_t>().prop("int"_hs);
ASSERT_TRUE(prop);
ASSERT_EQ(prop.key(), "int"_hs);
@@ -25,7 +25,7 @@ TEST_F(Meta, MetaProp) {
}
TEST_F(Meta, MetaPropFromBase) {
auto type = entt::resolve<derived>();
auto type = entt::resolve<derived_t>();
auto prop_bool = type.prop("bool"_hs);
auto prop_int = type.prop("int"_hs);