test: use common types for lib tests (locator)

This commit is contained in:
Michele Caini
2024-02-14 09:52:07 +01:00
parent 4f1e0805db
commit 1a469a96ce
7 changed files with 36 additions and 45 deletions

View File

@@ -1,8 +0,0 @@
#ifndef ENTT_LIB_LOCATOR_COMMON_TYPES_H
#define ENTT_LIB_LOCATOR_COMMON_TYPES_H
struct service {
int value{};
};
#endif

View File

@@ -1,17 +1,17 @@
#define CR_HOST
#include <gtest/gtest.h>
#include <common/boxed_type.h>
#include <cr.h>
#include <entt/locator/locator.hpp>
#include "../common/types.h"
#include "types.h"
#include "userdata.h"
TEST(Lib, Locator) {
entt::locator<service>::emplace().value = 4;
entt::locator<test::boxed_int>::emplace().value = 4;
ASSERT_EQ(entt::locator<service>::value().value, 4);
ASSERT_EQ(entt::locator<test::boxed_int>::value().value, 4);
userdata ud{entt::locator<service>::handle(), 3};
userdata ud{entt::locator<test::boxed_int>::handle(), 3};
cr_plugin ctx;
ctx.userdata = &ud;
@@ -19,13 +19,13 @@ TEST(Lib, Locator) {
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
ASSERT_EQ(entt::locator<service>::value().value, ud.value);
ASSERT_EQ(entt::locator<test::boxed_int>::value().value, ud.value);
// service updates do not propagate across boundaries
entt::locator<service>::emplace().value = 4;
entt::locator<test::boxed_int>::emplace().value = 4;
cr_plugin_update(ctx);
ASSERT_NE(entt::locator<service>::value().value, ud.value);
ASSERT_NE(entt::locator<test::boxed_int>::value().value, ud.value);
cr_plugin_close(ctx);
}

View File

@@ -1,15 +1,15 @@
#include <common/boxed_type.h>
#include <cr.h>
#include <entt/locator/locator.hpp>
#include "../common/types.h"
#include "types.h"
#include "userdata.h"
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
switch(operation) {
case CR_LOAD:
entt::locator<service>::reset(static_cast<userdata *>(ctx->userdata)->handle);
entt::locator<test::boxed_int>::reset(static_cast<userdata *>(ctx->userdata)->handle);
break;
case CR_STEP:
entt::locator<service>::value().value = static_cast<userdata *>(ctx->userdata)->value;
entt::locator<test::boxed_int>::value().value = static_cast<userdata *>(ctx->userdata)->value;
break;
case CR_UNLOAD:
case CR_CLOSE:

View File

@@ -1,13 +0,0 @@
#ifndef ENTT_LIB_LOCATOR_PLUGIN_TYPES_H
#define ENTT_LIB_LOCATOR_PLUGIN_TYPES_H
#include <entt/locator/locator.hpp>
struct service;
struct userdata {
typename entt::locator<service>::node_type handle{};
int value{};
};
#endif

View File

@@ -0,0 +1,12 @@
#ifndef ENTT_LIB_LOCATOR_PLUGIN_USERDATA_H
#define ENTT_LIB_LOCATOR_PLUGIN_USERDATA_H
#include <common/boxed_type.h>
#include <entt/locator/locator.hpp>
struct userdata {
typename entt::locator<test::boxed_int>::node_type handle{};
int value{};
};
#endif

View File

@@ -1,11 +1,11 @@
#include <common/boxed_type.h>
#include <entt/core/attribute.h>
#include <entt/locator/locator.hpp>
#include "../common/types.h"
ENTT_API void set_up(const entt::locator<service>::node_type &handle) {
entt::locator<service>::reset(handle);
ENTT_API void set_up(const entt::locator<test::boxed_int>::node_type &handle) {
entt::locator<test::boxed_int>::reset(handle);
}
ENTT_API void use_service(int value) {
entt::locator<service>::value().value = value;
entt::locator<test::boxed_int>::value().value = value;
}

View File

@@ -1,24 +1,24 @@
#include <gtest/gtest.h>
#include <common/boxed_type.h>
#include <entt/core/attribute.h>
#include <entt/locator/locator.hpp>
#include "../common/types.h"
ENTT_API void set_up(const entt::locator<service>::node_type &);
ENTT_API void set_up(const entt::locator<test::boxed_int>::node_type &);
ENTT_API void use_service(int);
TEST(Lib, Locator) {
entt::locator<service>::emplace().value = 4;
entt::locator<test::boxed_int>::emplace().value = 4;
ASSERT_EQ(entt::locator<service>::value().value, 4);
ASSERT_EQ(entt::locator<test::boxed_int>::value().value, 4);
set_up(entt::locator<service>::handle());
set_up(entt::locator<test::boxed_int>::handle());
use_service(3);
ASSERT_EQ(entt::locator<service>::value().value, 3);
ASSERT_EQ(entt::locator<test::boxed_int>::value().value, 3);
// service updates do not propagate across boundaries
entt::locator<service>::emplace().value = 4;
entt::locator<test::boxed_int>::emplace().value = 4;
use_service(3);
ASSERT_EQ(entt::locator<service>::value().value, 4);
ASSERT_EQ(entt::locator<test::boxed_int>::value().value, 4);
}