Skip to content

Commit 154bdfd

Browse files
committed
test: add tests for inherited custom registry
1 parent 5591754 commit 154bdfd

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

test/entt/entity/observer.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,34 @@ TEST(Observer, GroupCornerCase) {
369369
ASSERT_FALSE(add_observer.empty());
370370
ASSERT_TRUE(remove_observer.empty());
371371
}
372+
373+
enum class CustomEntity : std::uint32_t
374+
{
375+
};
376+
377+
378+
// This could be used to add custom methods to the registry or, in case of non-public inheritance,
379+
// to restrict the access to the registry.
380+
class CustomRegistry: public entt::basic_registry<CustomEntity> {
381+
};
382+
383+
template<typename Component>
384+
struct entt::storage_type<Component, CustomEntity>
385+
{
386+
using type = entt::sigh_mixin<entt::basic_storage<Component, CustomEntity>, CustomRegistry>;
387+
};
388+
389+
390+
TEST(Observer, CustomInheritedRegistry) {
391+
CustomRegistry registry;
392+
using CustomObserver = entt::basic_observer<CustomRegistry>;
393+
CustomObserver observer(registry, entt::collector.group<int>());
394+
395+
ASSERT_EQ(observer.size(), 0u);
396+
ASSERT_TRUE(observer.empty());
397+
ASSERT_EQ(observer.begin(), observer.end());
398+
399+
const auto entity = registry.create();
400+
registry.emplace<int>(entity);
401+
ASSERT_EQ(observer.size(), 1u);
402+
}

test/entt/entity/sigh_mixin.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,43 @@ TEST(SighMixin, ThrowingComponent) {
689689
ASSERT_EQ(on_construct.value, 2);
690690
ASSERT_EQ(on_destroy.value, 3);
691691
}
692+
693+
enum class CustomEntity : std::uint32_t
694+
{
695+
};
696+
697+
698+
// This could be used to add custom methods to the registry or, in case of non-public inheritance,
699+
// to restrict the access to the registry.
700+
class CustomRegistry: public entt::basic_registry<CustomEntity> {
701+
};
702+
703+
template<typename Component>
704+
struct entt::storage_type<Component, CustomEntity>
705+
{
706+
using type = entt::sigh_mixin<entt::basic_storage<Component, CustomEntity>, CustomRegistry>;
707+
};
708+
709+
void custom_registry_listener(counter &counter, CustomRegistry &, CustomEntity) {
710+
++counter.value;
711+
}
712+
713+
TEST(SighMixin, CustomRegistry) {
714+
CustomRegistry registry;
715+
716+
counter on_construct_int{};
717+
counter on_construct_float{};
718+
719+
registry.on_construct<int>().connect<&custom_registry_listener>(on_construct_int);
720+
registry.on_construct<float>().connect<&custom_registry_listener>(on_construct_float);
721+
722+
const auto e1 = registry.create();
723+
const auto e2 = registry.create();
724+
725+
registry.emplace<int>(e1, 1);
726+
registry.emplace<int>(e2, 2);
727+
registry.emplace<float>(e1, 1.1f);
728+
729+
ASSERT_EQ(on_construct_int.value, 2);
730+
ASSERT_EQ(on_construct_float.value, 1);
731+
}

0 commit comments

Comments
 (0)