diff --git a/core/tests/CMakeLists.txt b/core/tests/CMakeLists.txt index 86dc01b77..c7012f7b6 100644 --- a/core/tests/CMakeLists.txt +++ b/core/tests/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable( reflection/type.cpp reflection/traits/constructible.cpp reflection/traits/fields.cpp + reflection/traits/nullable.cpp reflection/external/primitives.cpp reflection/external/string.cpp reflection/external/uuid.cpp diff --git a/core/tests/reflection/traits/nullable.cpp b/core/tests/reflection/traits/nullable.cpp new file mode 100644 index 000000000..58a79c830 --- /dev/null +++ b/core/tests/reflection/traits/nullable.cpp @@ -0,0 +1,46 @@ +#include + +#include +#include + +using cubos::core::reflection::NullableTrait; +using cubos::core::reflection::reflect; +using cubos::core::reflection::Type; + +namespace +{ + struct MyEntity + { + CUBOS_REFLECT; + uint32_t idx; + uint32_t generation; + }; +} // namespace + +CUBOS_REFLECT_IMPL(MyEntity) +{ + return Type::create("MyEntity") + .with(NullableTrait{[](const void* instance) { + const auto* ent = static_cast(instance); + return ent->idx == UINT32_MAX && ent->generation == UINT32_MAX; + }, + [](void* instance) { + auto* ent = static_cast(instance); + ent->idx = UINT32_MAX; + ent->generation = UINT32_MAX; + }}); +} + +TEST_CASE("reflection::NullableTrait") +{ + const auto& entityType = reflect(); + REQUIRE(entityType.has()); + + const auto& nullableTrait = entityType.get(); + + MyEntity ent{1, 1}; + REQUIRE(!nullableTrait.isNull(&ent)); + + nullableTrait.setToNull(&ent); + REQUIRE(nullableTrait.isNull(&ent)); +}