test: use common types for lib tests (meta)

This commit is contained in:
Michele Caini
2024-02-14 09:52:19 +01:00
parent 1a469a96ce
commit d013c69bf1
9 changed files with 101 additions and 141 deletions

View File

@@ -1,14 +0,0 @@
#ifndef ENTT_LIB_META_COMMON_TYPES_H
#define ENTT_LIB_META_COMMON_TYPES_H
struct position {
int x{};
int y{};
};
struct velocity {
double dx{};
double dy{};
};
#endif

View File

@@ -8,12 +8,12 @@
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
#include "types.h"
#include "userdata.h"
TEST(Lib, Meta) {
using namespace entt::literals;
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("boxed_int"_hs));
userdata ud{entt::locator<entt::meta_ctx>::handle(), entt::meta_any{}};
@@ -23,36 +23,29 @@ TEST(Lib, Meta) {
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
entt::meta<double>().conv<int>();
ASSERT_TRUE(entt::resolve("boxed_int"_hs));
ASSERT_TRUE(entt::resolve("empty"_hs));
ASSERT_TRUE(entt::resolve("position"_hs));
ASSERT_TRUE(entt::resolve("velocity"_hs));
auto boxed_int = entt::resolve("boxed_int"_hs).construct(4.);
auto empty = entt::resolve("empty"_hs).construct();
auto pos = entt::resolve("position"_hs).construct(4., 3.);
auto vel = entt::resolve("velocity"_hs).construct();
ASSERT_TRUE(boxed_int);
ASSERT_TRUE(empty);
ASSERT_TRUE(pos && vel);
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
ASSERT_NE(pos.type().data("y"_hs).get(pos).try_cast<int>(), nullptr);
ASSERT_EQ(pos.type().data("x"_hs).get(pos).cast<int>(), 4);
ASSERT_EQ(pos.type().data("y"_hs).get(pos).cast<int>(), 3);
ASSERT_EQ(vel.type().data("dx"_hs).type(), entt::resolve<double>());
ASSERT_TRUE(vel.type().data("dy"_hs).get(vel).allow_cast<double>());
ASSERT_EQ(vel.type().data("dx"_hs).get(vel).cast<double>(), 0.);
ASSERT_EQ(vel.type().data("dy"_hs).get(vel).cast<double>(), 0.);
ASSERT_EQ(boxed_int.type().data("value"_hs).type(), entt::resolve<int>());
ASSERT_NE(boxed_int.get("value"_hs).try_cast<int>(), nullptr);
ASSERT_EQ(boxed_int.get("value"_hs).cast<int>(), 4);
ASSERT_EQ(ud.any.type(), entt::resolve<int>());
ASSERT_EQ(ud.any.cast<int>(), 4);
// these objects have been initialized from a different context
pos.emplace<void>();
vel.emplace<void>();
boxed_int.emplace<void>();
empty.emplace<void>();
ud.any.emplace<void>();
cr_plugin_close(ctx);
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("velocity"_hs));
ASSERT_FALSE(entt::resolve("boxed_int"_hs));
ASSERT_FALSE(entt::resolve("empty"_hs));
}

View File

@@ -1,35 +1,33 @@
#include <common/boxed_type.h>
#include <common/empty.h>
#include <cr.h>
#include <entt/core/hashed_string.hpp>
#include <entt/locator/locator.hpp>
#include <entt/meta/context.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "../common/types.h"
#include "types.h"
#include "userdata.h"
position create_position(int x, int y) {
return position{x, y};
test::boxed_int create_boxed_int(int value) {
return test::boxed_int{value};
}
void set_up() {
using namespace entt::literals;
entt::meta<position>()
.type("position"_hs)
.ctor<&create_position>()
.data<&position::x>("x"_hs)
.data<&position::y>("y"_hs);
entt::meta<test::boxed_int>()
.type("boxed_int"_hs)
.ctor<&create_boxed_int>()
.data<&test::boxed_int::value>("value"_hs);
entt::meta<velocity>()
.type("velocity"_hs)
.ctor<>()
.data<&velocity::dx>("dx"_hs)
.data<&velocity::dy>("dy"_hs);
entt::meta<test::empty>()
.type("empty"_hs)
.ctor<>();
}
void tear_down() {
entt::meta_reset<position>();
entt::meta_reset<velocity>();
entt::meta_reset<test::boxed_int>();
entt::meta_reset<test::empty>();
}
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {

View File

@@ -1,5 +1,5 @@
#ifndef ENTT_LIB_META_PLUGIN_TYPES_H
#define ENTT_LIB_META_PLUGIN_TYPES_H
#ifndef ENTT_LIB_META_PLUGIN_USERDATA_H
#define ENTT_LIB_META_PLUGIN_USERDATA_H
#include <entt/locator/locator.hpp>
#include <entt/meta/context.hpp>

View File

@@ -8,12 +8,12 @@
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
#include "types.h"
#include "userdata.h"
TEST(Lib, Meta) {
using namespace entt::literals;
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("boxed_int"_hs));
userdata ud{};
ud.ctx = entt::locator<entt::meta_ctx>::handle();
@@ -24,36 +24,29 @@ TEST(Lib, Meta) {
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
entt::meta<double>().conv<int>();
ASSERT_TRUE(entt::resolve("boxed_int"_hs));
ASSERT_TRUE(entt::resolve("empty"_hs));
ASSERT_TRUE(entt::resolve("position"_hs));
ASSERT_TRUE(entt::resolve("velocity"_hs));
auto boxed_int = entt::resolve("boxed_int"_hs).construct(4.);
auto empty = entt::resolve("empty"_hs).construct();
auto pos = entt::resolve("position"_hs).construct(4., 3.);
auto vel = entt::resolve("velocity"_hs).construct();
ASSERT_TRUE(boxed_int);
ASSERT_TRUE(empty);
ASSERT_TRUE(pos && vel);
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
ASSERT_NE(pos.type().data("y"_hs).get(pos).try_cast<int>(), nullptr);
ASSERT_EQ(pos.type().data("x"_hs).get(pos).cast<int>(), 4);
ASSERT_EQ(pos.type().data("y"_hs).get(pos).cast<int>(), 3);
ASSERT_EQ(vel.type().data("dx"_hs).type(), entt::resolve<double>());
ASSERT_TRUE(vel.type().data("dy"_hs).get(vel).allow_cast<double>());
ASSERT_EQ(vel.type().data("dx"_hs).get(vel).cast<double>(), 0.);
ASSERT_EQ(vel.type().data("dy"_hs).get(vel).cast<double>(), 0.);
ASSERT_EQ(boxed_int.type().data("value"_hs).type(), entt::resolve<int>());
ASSERT_NE(boxed_int.get("value"_hs).try_cast<int>(), nullptr);
ASSERT_EQ(boxed_int.get("value"_hs).cast<int>(), 4);
ASSERT_EQ(ud.any.type(), entt::resolve<int>());
ASSERT_EQ(ud.any.cast<int>(), 4);
// these objects have been initialized from a different context
pos.emplace<void>();
vel.emplace<void>();
boxed_int.emplace<void>();
empty.emplace<void>();
ud.any.emplace<void>();
cr_plugin_close(ctx);
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("velocity"_hs));
ASSERT_FALSE(entt::resolve("boxed_int"_hs));
ASSERT_FALSE(entt::resolve("empty"_hs));
}

View File

@@ -1,35 +1,33 @@
#include <common/boxed_type.h>
#include <common/empty.h>
#include <cr.h>
#include <entt/core/hashed_string.hpp>
#include <entt/locator/locator.hpp>
#include <entt/meta/context.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "../common/types.h"
#include "types.h"
#include "userdata.h"
position create_position(int x, int y) {
return position{x, y};
test::boxed_int create_boxed_int(int value) {
return test::boxed_int{value};
}
void set_up() {
using namespace entt::literals;
entt::meta<position>()
.type("position"_hs)
.ctor<&create_position>()
.data<&position::x>("x"_hs)
.data<&position::y>("y"_hs);
entt::meta<test::boxed_int>()
.type("boxed_int"_hs)
.ctor<&create_boxed_int>()
.data<&test::boxed_int::value>("value"_hs);
entt::meta<velocity>()
.type("velocity"_hs)
.ctor<>()
.data<&velocity::dx>("dx"_hs)
.data<&velocity::dy>("dy"_hs);
entt::meta<test::empty>()
.type("empty"_hs)
.ctor<>();
}
void tear_down() {
entt::meta_reset<position>();
entt::meta_reset<velocity>();
entt::meta_reset<test::boxed_int>();
entt::meta_reset<test::empty>();
}
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {

View File

@@ -1,15 +1,14 @@
#ifndef ENTT_LIB_META_PLUGIN_STD_TYPES_H
#define ENTT_LIB_META_PLUGIN_STD_TYPES_H
#ifndef ENTT_LIB_META_PLUGIN_STD_USERDATA_H
#define ENTT_LIB_META_PLUGIN_STD_USERDATA_H
#include <type_traits>
#include <common/boxed_type.h>
#include <common/empty.h>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
#include <entt/meta/context.hpp>
#include <entt/meta/meta.hpp>
template<typename>
struct custom_type_hash;
#define ASSIGN_TYPE_ID(clazz) \
template<> \
struct entt::type_hash<clazz> { \
@@ -18,9 +17,6 @@ struct custom_type_hash;
} \
}
struct position;
struct velocity;
struct userdata {
entt::locator<entt::meta_ctx>::node_type ctx;
entt::meta_any any;
@@ -28,8 +24,8 @@ struct userdata {
ASSIGN_TYPE_ID(void);
ASSIGN_TYPE_ID(std::size_t);
ASSIGN_TYPE_ID(position);
ASSIGN_TYPE_ID(velocity);
ASSIGN_TYPE_ID(test::boxed_int);
ASSIGN_TYPE_ID(test::empty);
ASSIGN_TYPE_ID(double);
ASSIGN_TYPE_ID(int);

View File

@@ -1,13 +1,17 @@
#include <common/boxed_type.h>
#include <common/empty.h>
#include <entt/core/attribute.h>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
#include <entt/locator/locator.hpp>
#include <entt/meta/context.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "../common/types.h"
position create_position(int x, int y) {
return position{x, y};
template const entt::type_info &entt::type_id<double>() noexcept;
test::boxed_int create_boxed_int(int value) {
return test::boxed_int{value};
}
ENTT_API void share(const entt::locator<entt::meta_ctx>::node_type &handle) {
@@ -17,22 +21,19 @@ ENTT_API void share(const entt::locator<entt::meta_ctx>::node_type &handle) {
ENTT_API void set_up() {
using namespace entt::literals;
entt::meta<position>()
.type("position"_hs)
.ctor<&create_position>()
.data<&position::x>("x"_hs)
.data<&position::y>("y"_hs);
entt::meta<test::boxed_int>()
.type("boxed_int"_hs)
.ctor<&create_boxed_int>()
.data<&test::boxed_int::value>("value"_hs);
entt::meta<velocity>()
.type("velocity"_hs)
.ctor<>()
.data<&velocity::dx>("dx"_hs)
.data<&velocity::dy>("dy"_hs);
entt::meta<test::empty>()
.type("empty"_hs)
.ctor<>();
}
ENTT_API void tear_down() {
entt::meta_reset<position>();
entt::meta_reset<velocity>();
entt::meta_reset<test::boxed_int>();
entt::meta_reset<test::empty>();
}
ENTT_API entt::meta_any wrap_int(int value) {

View File

@@ -1,4 +1,6 @@
#include <gtest/gtest.h>
#include <common/boxed_type.h>
#include <common/empty.h>
#include <entt/core/attribute.h>
#include <entt/core/hashed_string.hpp>
#include <entt/locator/locator.hpp>
@@ -6,7 +8,6 @@
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
#include "../common/types.h"
ENTT_API void share(const entt::locator<entt::meta_ctx>::node_type &);
ENTT_API void set_up();
@@ -16,42 +17,36 @@ ENTT_API entt::meta_any wrap_int(int);
TEST(Lib, Meta) {
using namespace entt::literals;
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("velocity"_hs));
ASSERT_FALSE(entt::resolve("boxed_int"_hs));
ASSERT_FALSE(entt::resolve("empty"_hs));
share(entt::locator<entt::meta_ctx>::handle());
set_up();
entt::meta<double>().conv<int>();
ASSERT_TRUE(entt::resolve("position"_hs));
ASSERT_TRUE(entt::resolve("velocity"_hs));
ASSERT_TRUE(entt::resolve("boxed_int"_hs));
ASSERT_TRUE(entt::resolve("empty"_hs));
ASSERT_EQ(entt::resolve<position>(), entt::resolve("position"_hs));
ASSERT_EQ(entt::resolve<velocity>(), entt::resolve("velocity"_hs));
ASSERT_EQ(entt::resolve<test::boxed_int>(), entt::resolve("boxed_int"_hs));
ASSERT_EQ(entt::resolve<test::empty>(), entt::resolve("empty"_hs));
auto pos = entt::resolve("position"_hs).construct(4., 3.);
auto vel = entt::resolve("velocity"_hs).construct();
auto boxed_int = entt::resolve("boxed_int"_hs).construct(4.);
auto empty = entt::resolve("empty"_hs).construct();
ASSERT_TRUE(pos && vel);
ASSERT_TRUE(boxed_int);
ASSERT_TRUE(empty);
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
ASSERT_NE(pos.type().data("y"_hs).get(pos).try_cast<int>(), nullptr);
ASSERT_EQ(pos.type().data("x"_hs).get(pos).cast<int>(), 4);
ASSERT_EQ(pos.type().data("y"_hs).get(pos).cast<int>(), 3);
ASSERT_EQ(boxed_int.type().data("value"_hs).type(), entt::resolve<int>());
ASSERT_NE(boxed_int.get("value"_hs).try_cast<int>(), nullptr);
ASSERT_EQ(boxed_int.get("value"_hs).cast<int>(), 4);
ASSERT_EQ(vel.type().data("dx"_hs).type(), entt::resolve<double>());
ASSERT_TRUE(vel.type().data("dy"_hs).get(vel).allow_cast<double>());
ASSERT_EQ(vel.type().data("dx"_hs).get(vel).cast<double>(), 0.);
ASSERT_EQ(vel.type().data("dy"_hs).get(vel).cast<double>(), 0.);
pos.reset();
vel.reset();
boxed_int.reset();
empty.reset();
ASSERT_EQ(wrap_int(4).type(), entt::resolve<int>());
ASSERT_EQ(wrap_int(4).cast<int>(), 4);
tear_down();
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("velocity"_hs));
ASSERT_FALSE(entt::resolve("boxed_int"_hs));
ASSERT_FALSE(entt::resolve("empty"_hs));
}