Skip to content

Commit

Permalink
tests(core): reflection nullable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Nov 18, 2023
1 parent 836e4f1 commit 206eb01
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions core/tests/reflection/traits/nullable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <doctest/doctest.h>

#include <cubos/core/reflection/traits/nullable.hpp>
#include <cubos/core/reflection/type.hpp>

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<const MyEntity*>(instance);
return ent->idx == UINT32_MAX && ent->generation == UINT32_MAX;
},
[](void* instance) {
auto* ent = static_cast<MyEntity*>(instance);
ent->idx = UINT32_MAX;
ent->generation = UINT32_MAX;
}});
}

TEST_CASE("reflection::NullableTrait")
{
const auto& entityType = reflect<MyEntity>();
REQUIRE(entityType.has<NullableTrait>());

const auto& nullableTrait = entityType.get<NullableTrait>();

MyEntity ent{1, 1};
REQUIRE(!nullableTrait.isNull(&ent));

nullableTrait.setToNull(&ent);
REQUIRE(nullableTrait.isNull(&ent));
}

0 comments on commit 206eb01

Please sign in to comment.