/******************************************************************************* * This file is part of the "https://github.com/blackmatov/meta.hpp" * For conditions of distribution and use, see copyright notice in LICENSE.md * Copyright (C) 2021-2022, by Matvey Cherevko (blackmatov@gmail.com) ******************************************************************************/ #include "../meta_untests.hpp" namespace { enum class color { red, green, blue, }; struct ivec2 { int x{}; int y{}; }; struct ivec3 { int x{}; int y{}; int z{}; }; ivec2 iadd2(const ivec2& l, const ivec2& r) noexcept { return {l.x + r.x, l.y + r.y}; } ivec3 iadd3(const ivec3& l, const ivec3& r) noexcept { return {l.x + r.x, l.y + r.y, l.z + r.z}; } ivec2 static_ivec2 = ivec2{1, 0}; const ivec3 static_const_ivec3 = ivec3{1, 0}; } TEST_CASE("meta/meta_states/scope") { namespace meta = meta_hpp; meta::static_scope_("meta/meta_states/scope/math") .typedef_("color") .typedef_("ivec2") .typedef_("ivec3") .function_("iadd2", &iadd2, {"l", "r"}) .function_("iadd3", &iadd3, {"l"}) .variable_("static_ivec2", &static_ivec2) .variable_("static_const_ivec3", &static_const_ivec3); const meta::scope math_scope = meta::resolve_scope("meta/meta_states/scope/math"); REQUIRE(math_scope); REQUIRE(math_scope.is_valid()); CHECK(math_scope.get_name() == "meta/meta_states/scope/math"); CHECK(math_scope.get_variables().size() == 2); CHECK(math_scope.get_typedefs().size() == 3); SUBCASE("") { const meta::scope scope; CHECK_FALSE(scope); CHECK_FALSE(scope.is_valid()); } SUBCASE("operators") { const meta::scope math1_s = meta::resolve_scope("meta/meta_states/scope/math1"); const meta::scope math2_s = meta::resolve_scope("meta/meta_states/scope/math2"); CHECK(math1_s == math1_s); CHECK(math1_s != math2_s); CHECK((math1_s < math2_s || math2_s < math1_s)); } SUBCASE("classes") { CHECK_FALSE(math_scope.get_typedef("non-existent-class")); const meta::any_type ivec2_type = math_scope.get_typedef("ivec2"); CHECK(ivec2_type == meta::resolve_type()); const meta::any_type ivec3_type = math_scope.get_typedef("ivec3"); CHECK(ivec3_type == meta::resolve_type()); } SUBCASE("enums") { CHECK_FALSE(math_scope.get_typedef("non-existent-enum")); const meta::any_type color_type = math_scope.get_typedef("color"); CHECK(color_type == meta::resolve_type()); } SUBCASE("functions") { CHECK_FALSE(math_scope.get_function("non-existent-function")); const meta::function iadd2_func = math_scope.get_function("iadd2"); REQUIRE(iadd2_func); { CHECK(iadd2_func.get_arguments().size() == 2); REQUIRE(iadd2_func.get_argument(0)); CHECK(iadd2_func.get_argument(0).get_type() == meta::resolve_type()); CHECK(iadd2_func.get_argument(0).get_position() == 0); CHECK(iadd2_func.get_argument(0).get_name() == "l"); REQUIRE(iadd2_func.get_argument(1)); CHECK(iadd2_func.get_argument(1).get_type() == meta::resolve_type()); CHECK(iadd2_func.get_argument(1).get_position() == 1); CHECK(iadd2_func.get_argument(1).get_name() == "r"); CHECK_FALSE(iadd2_func.get_argument(2)); } const meta::function iadd3_func = math_scope.get_function("iadd3"); REQUIRE(iadd3_func); { CHECK(iadd3_func.get_arguments().size() == 2); REQUIRE(iadd3_func.get_argument(0)); CHECK(iadd3_func.get_argument(0).get_type() == meta::resolve_type()); CHECK(iadd3_func.get_argument(0).get_position() == 0); CHECK(iadd3_func.get_argument(0).get_name() == "l"); REQUIRE(iadd3_func.get_argument(1)); CHECK(iadd3_func.get_argument(1).get_type() == meta::resolve_type()); CHECK(iadd3_func.get_argument(1).get_position() == 1); CHECK(iadd3_func.get_argument(1).get_name() == ""); CHECK_FALSE(iadd3_func.get_argument(2)); } } SUBCASE("variables") { CHECK_FALSE(math_scope.get_variable("non-existent-variable")); const meta::variable static_ivec2_var = math_scope.get_variable("static_ivec2"); REQUIRE(static_ivec2_var); CHECK(static_ivec2_var.get_type().get_data_type() == meta::resolve_type()); const meta::variable static_const_ivec3_var = math_scope.get_variable("static_const_ivec3"); REQUIRE(static_const_ivec3_var); CHECK(static_const_ivec3_var.get_type().get_data_type() == meta::resolve_type()); } }