100 lines
4 KiB
C++
100 lines
4 KiB
C++
#include "ikarus/scopes/object_scope.h"
|
|
|
|
#include <initializer_list>
|
|
#include <utility>
|
|
#include <variant>
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <cppbase/templates.hpp>
|
|
|
|
#include <ikarus/scopes/blueprint_scope.h>
|
|
#include <ikarus/scopes/entity_scope.h>
|
|
#include <ikarus/scopes/property_scope.h>
|
|
|
|
#include <scopes/object_scope.hpp>
|
|
|
|
IkarusObjectScopeType ikarus_object_scope_get_type(IkarusObjectScope * scope) {
|
|
return std::visit(
|
|
cppbase::overloaded{
|
|
[]([[maybe_unused]] IkarusBlueprintScope const& scope) { return IkarusObjectScopeType_Blueprint; },
|
|
[]([[maybe_unused]] IkarusPropertyScope const& scope) { return IkarusObjectScopeType_Property; },
|
|
[]([[maybe_unused]] IkarusEntityScope const& scope) { return IkarusObjectScopeType_Entity; }},
|
|
scope->data
|
|
);
|
|
}
|
|
|
|
void ikarus_object_scope_visit(
|
|
IkarusObjectScope * scope,
|
|
void(blueprint_visitor)(IkarusBlueprintScope *, void *),
|
|
void(property_visitor)(IkarusPropertyScope *, void *),
|
|
void(entity_visitor)(IkarusEntityScope *, void *),
|
|
void * data
|
|
) {
|
|
std::visit(
|
|
cppbase::overloaded{
|
|
[blueprint_visitor, data](IkarusBlueprintScope& scope) { blueprint_visitor(&scope, data); },
|
|
[property_visitor, data](IkarusPropertyScope& scope) { property_visitor(&scope, data); },
|
|
[entity_visitor, data](IkarusEntityScope& scope) { entity_visitor(&scope, data); }},
|
|
scope->data
|
|
);
|
|
}
|
|
|
|
TEST_CASE("blueprint_object_scope_conversion", "[object_scope]") {
|
|
auto * blueprint_scope = ikarus_blueprint_scope_create();
|
|
auto * blueprint_object_scope = ikarus_blueprint_scope_to_object_scope(blueprint_scope);
|
|
REQUIRE(ikarus_object_scope_get_type(blueprint_object_scope) == IkarusObjectScopeType_Blueprint);
|
|
}
|
|
|
|
TEST_CASE("property_object_scope_conversion", "[object_scope]") {
|
|
auto * blueprint = ikarus_blueprint_create();
|
|
auto * entity = ikarus_entity_create();
|
|
|
|
auto * property_blueprint_scope = ikarus_property_scope_create_blueprint(blueprint);
|
|
auto * property_blueprint_object_scope = ikarus_property_scope_to_object_scope(property_blueprint_scope);
|
|
|
|
REQUIRE(ikarus_object_scope_get_type(property_blueprint_object_scope) == IkarusObjectScopeType_Property);
|
|
|
|
auto * property_entity_scope = ikarus_property_scope_create_entity(entity);
|
|
auto * property_entity_object_scope = ikarus_property_scope_to_object_scope(property_entity_scope);
|
|
|
|
REQUIRE(ikarus_object_scope_get_type(property_entity_object_scope) == IkarusObjectScopeType_Property);
|
|
}
|
|
|
|
TEST_CASE("entity_object_scope_conversion", "[object_scope]") {
|
|
auto * entity_scope = ikarus_entity_scope_create();
|
|
auto * entity_object_scope = ikarus_entity_scope_to_object_scope(entity_scope);
|
|
REQUIRE(ikarus_object_scope_get_type(entity_object_scope) == IkarusObjectScopeType_Entity);
|
|
}
|
|
|
|
TEST_CASE("object_scope_visiting", "[object_scope]") {
|
|
auto * blueprint = ikarus_blueprint_create();
|
|
|
|
auto * blueprint_scope = ikarus_blueprint_scope_create();
|
|
auto * property_scope = ikarus_property_scope_create_blueprint(blueprint);
|
|
auto * entity_scope = ikarus_entity_scope_create();
|
|
|
|
auto * blueprint_object_scope = ikarus_blueprint_scope_to_object_scope(blueprint_scope);
|
|
auto * property_object_scope = ikarus_property_scope_to_object_scope(property_scope);
|
|
auto * entity_object_scope = ikarus_entity_scope_to_object_scope(entity_scope);
|
|
|
|
auto scopes = {
|
|
std::make_pair(blueprint_object_scope, 1),
|
|
std::make_pair(property_object_scope, 2),
|
|
std::make_pair(entity_object_scope, 3),
|
|
};
|
|
|
|
for (auto [scope, value] : scopes) {
|
|
int test = 0;
|
|
|
|
ikarus_object_scope_visit(
|
|
scope,
|
|
[](IkarusBlueprintScope *, void * data) { *static_cast<decltype(test) *>(data) = 1; },
|
|
[](IkarusPropertyScope *, void * data) { *static_cast<decltype(test) *>(data) = 2; },
|
|
[](IkarusEntityScope *, void * data) { *static_cast<decltype(test) *>(data) = 3; },
|
|
&test
|
|
);
|
|
|
|
REQUIRE(test == value);
|
|
}
|
|
}
|