diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_basis.cpp | 325 | ||||
-rw-r--r-- | tests/test_basis.h | 252 | ||||
-rw-r--r-- | tests/test_class_db.cpp | 882 | ||||
-rw-r--r-- | tests/test_class_db.h | 798 | ||||
-rw-r--r-- | tests/test_expression.h | 431 | ||||
-rw-r--r-- | tests/test_gradient.h | 152 | ||||
-rw-r--r-- | tests/test_macros.h | 57 | ||||
-rw-r--r-- | tests/test_main.cpp | 2 | ||||
-rw-r--r-- | tests/test_ordered_hash_map.cpp | 175 | ||||
-rw-r--r-- | tests/test_ordered_hash_map.h | 102 | ||||
-rw-r--r-- | tests/test_validate_testing.h | 131 |
11 files changed, 1919 insertions, 1388 deletions
diff --git a/tests/test_basis.cpp b/tests/test_basis.cpp deleted file mode 100644 index 5904fc386a..0000000000 --- a/tests/test_basis.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/*************************************************************************/ -/* test_basis.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "test_basis.h" - -#include "core/math/random_number_generator.h" -#include "core/os/os.h" -#include "core/ustring.h" - -namespace TestBasis { - -enum RotOrder { - EulerXYZ, - EulerXZY, - EulerYZX, - EulerYXZ, - EulerZXY, - EulerZYX -}; - -Vector3 deg2rad(const Vector3 &p_rotation) { - return p_rotation / 180.0 * Math_PI; -} - -Vector3 rad2deg(const Vector3 &p_rotation) { - return p_rotation / Math_PI * 180.0; -} - -Basis EulerToBasis(RotOrder mode, const Vector3 &p_rotation) { - Basis ret; - switch (mode) { - case EulerXYZ: - ret.set_euler_xyz(p_rotation); - break; - - case EulerXZY: - ret.set_euler_xzy(p_rotation); - break; - - case EulerYZX: - ret.set_euler_yzx(p_rotation); - break; - - case EulerYXZ: - ret.set_euler_yxz(p_rotation); - break; - - case EulerZXY: - ret.set_euler_zxy(p_rotation); - break; - - case EulerZYX: - ret.set_euler_zyx(p_rotation); - break; - - default: - // If you land here, Please integrate all rotation orders. - CRASH_NOW_MSG("This is not unreachable."); - } - - return ret; -} - -Vector3 BasisToEuler(RotOrder mode, const Basis &p_rotation) { - switch (mode) { - case EulerXYZ: - return p_rotation.get_euler_xyz(); - - case EulerXZY: - return p_rotation.get_euler_xzy(); - - case EulerYZX: - return p_rotation.get_euler_yzx(); - - case EulerYXZ: - return p_rotation.get_euler_yxz(); - - case EulerZXY: - return p_rotation.get_euler_zxy(); - - case EulerZYX: - return p_rotation.get_euler_zyx(); - - default: - // If you land here, Please integrate all rotation orders. - CRASH_NOW_MSG("This is not unreachable."); - return Vector3(); - } -} - -String get_rot_order_name(RotOrder ro) { - switch (ro) { - case EulerXYZ: - return "XYZ"; - case EulerXZY: - return "XZY"; - case EulerYZX: - return "YZX"; - case EulerYXZ: - return "YXZ"; - case EulerZXY: - return "ZXY"; - case EulerZYX: - return "ZYX"; - default: - return "[Not supported]"; - } -} - -bool test_rotation(Vector3 deg_original_euler, RotOrder rot_order) { - // This test: - // 1. Converts the rotation vector from deg to rad. - // 2. Converts euler to basis. - // 3. Converts the above basis back into euler. - // 4. Converts the above euler into basis again. - // 5. Compares the basis obtained in step 2 with the basis of step 4 - // - // The conversion "basis to euler", done in the step 3, may be different from - // the original euler, even if the final rotation are the same. - // This happens because there are more ways to represents the same rotation, - // both valid, using eulers. - // For this reason is necessary to convert that euler back to basis and finally - // compares it. - // - // In this way we can assert that both functions: basis to euler / euler to basis - // are correct. - - bool pass = true; - - // Euler to rotation - const Vector3 original_euler = deg2rad(deg_original_euler); - const Basis to_rotation = EulerToBasis(rot_order, original_euler); - - // Euler from rotation - const Vector3 euler_from_rotation = BasisToEuler(rot_order, to_rotation); - const Basis rotation_from_computed_euler = EulerToBasis(rot_order, euler_from_rotation); - - Basis res = to_rotation.inverse() * rotation_from_computed_euler; - - if ((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() > 0.1) { - OS::get_singleton()->print("Fail due to X %ls\n", String(res.get_axis(0)).c_str()); - pass = false; - } - if ((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() > 0.1) { - OS::get_singleton()->print("Fail due to Y %ls\n", String(res.get_axis(1)).c_str()); - pass = false; - } - if ((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() > 0.1) { - OS::get_singleton()->print("Fail due to Z %ls\n", String(res.get_axis(2)).c_str()); - pass = false; - } - - if (pass) { - // Double check `to_rotation` decomposing with XYZ rotation order. - const Vector3 euler_xyz_from_rotation = to_rotation.get_euler_xyz(); - Basis rotation_from_xyz_computed_euler; - rotation_from_xyz_computed_euler.set_euler_xyz(euler_xyz_from_rotation); - - res = to_rotation.inverse() * rotation_from_xyz_computed_euler; - - if ((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() > 0.1) { - OS::get_singleton()->print("Double check with XYZ rot order failed, due to X %ls\n", String(res.get_axis(0)).c_str()); - pass = false; - } - if ((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() > 0.1) { - OS::get_singleton()->print("Double check with XYZ rot order failed, due to Y %ls\n", String(res.get_axis(1)).c_str()); - pass = false; - } - if ((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() > 0.1) { - OS::get_singleton()->print("Double check with XYZ rot order failed, due to Z %ls\n", String(res.get_axis(2)).c_str()); - pass = false; - } - } - - if (pass == false) { - // Print phase only if not pass. - OS *os = OS::get_singleton(); - os->print("Rotation order: %ls\n.", get_rot_order_name(rot_order).c_str()); - os->print("Original Rotation: %ls\n", String(deg_original_euler).c_str()); - os->print("Quaternion to rotation order: %ls\n", String(rad2deg(euler_from_rotation)).c_str()); - } - - return pass; -} - -void test_euler_conversion() { - Vector<RotOrder> rotorder_to_test; - rotorder_to_test.push_back(EulerXYZ); - rotorder_to_test.push_back(EulerXZY); - rotorder_to_test.push_back(EulerYZX); - rotorder_to_test.push_back(EulerYXZ); - rotorder_to_test.push_back(EulerZXY); - rotorder_to_test.push_back(EulerZYX); - - Vector<Vector3> vectors_to_test; - - // Test the special cases. - vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0)); - vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5)); - vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5)); - vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0)); - vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0)); - vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0)); - vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0)); - vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0)); - vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0)); - vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0)); - vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0)); - vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0)); - vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0)); - vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0)); - vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0)); - vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0)); - vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0)); - vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0)); - vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0)); - vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0)); - vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0)); - vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0)); - vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.0)); - vectors_to_test.push_back(Vector3(-90.0, 60.0, -90.0)); - vectors_to_test.push_back(Vector3(90.0, 60.0, -90.0)); - vectors_to_test.push_back(Vector3(90.0, -60.0, -90.0)); - vectors_to_test.push_back(Vector3(-90.0, -60.0, -90.0)); - vectors_to_test.push_back(Vector3(-90.0, 60.0, 90.0)); - vectors_to_test.push_back(Vector3(90.0, 60.0, 90.0)); - vectors_to_test.push_back(Vector3(90.0, -60.0, 90.0)); - vectors_to_test.push_back(Vector3(-90.0, -60.0, 90.0)); - vectors_to_test.push_back(Vector3(60.0, 90.0, -40.0)); - vectors_to_test.push_back(Vector3(60.0, -90.0, -40.0)); - vectors_to_test.push_back(Vector3(-60.0, -90.0, -40.0)); - vectors_to_test.push_back(Vector3(-60.0, 90.0, 40.0)); - vectors_to_test.push_back(Vector3(60.0, 90.0, 40.0)); - vectors_to_test.push_back(Vector3(60.0, -90.0, 40.0)); - vectors_to_test.push_back(Vector3(-60.0, -90.0, 40.0)); - vectors_to_test.push_back(Vector3(-90.0, 90.0, -90.0)); - vectors_to_test.push_back(Vector3(90.0, 90.0, -90.0)); - vectors_to_test.push_back(Vector3(90.0, -90.0, -90.0)); - vectors_to_test.push_back(Vector3(-90.0, -90.0, -90.0)); - vectors_to_test.push_back(Vector3(-90.0, 90.0, 90.0)); - vectors_to_test.push_back(Vector3(90.0, 90.0, 90.0)); - vectors_to_test.push_back(Vector3(90.0, -90.0, 90.0)); - vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0)); - vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0)); - vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.0)); - vectors_to_test.push_back(Vector3(-120.0, -150.0, -130.0)); - vectors_to_test.push_back(Vector3(120.0, -150.0, -130.0)); - vectors_to_test.push_back(Vector3(120.0, 150.0, -130.0)); - vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0)); - - // Add 1000 random vectors with weirds numbers. - RandomNumberGenerator rng; - for (int _ = 0; _ < 1000; _ += 1) { - vectors_to_test.push_back(Vector3( - rng.randf_range(-1800, 1800), - rng.randf_range(-1800, 1800), - rng.randf_range(-1800, 1800))); - } - - bool success = true; - for (int h = 0; h < rotorder_to_test.size(); h += 1) { - int passed = 0; - int failed = 0; - for (int i = 0; i < vectors_to_test.size(); i += 1) { - if (test_rotation(vectors_to_test[i], rotorder_to_test[h])) { - //OS::get_singleton()->print("Success. \n\n"); - passed += 1; - } else { - OS::get_singleton()->print("FAILED FAILED FAILED. \n\n"); - OS::get_singleton()->print("------------>\n"); - OS::get_singleton()->print("------------>\n"); - failed += 1; - success = false; - } - } - - if (failed == 0) { - OS::get_singleton()->print("%i passed tests for rotation order: %ls.\n", passed, get_rot_order_name(rotorder_to_test[h]).c_str()); - } else { - OS::get_singleton()->print("%i FAILED tests for rotation order: %ls.\n", failed, get_rot_order_name(rotorder_to_test[h]).c_str()); - } - } - - if (success) { - OS::get_singleton()->print("Euler conversion checks passed.\n"); - } else { - OS::get_singleton()->print("Euler conversion checks FAILED.\n"); - } -} - -MainLoop *test() { - OS::get_singleton()->print("Start euler conversion checks.\n"); - test_euler_conversion(); - - return NULL; -} - -} // namespace TestBasis diff --git a/tests/test_basis.h b/tests/test_basis.h index 63297bd3b8..05efe33788 100644 --- a/tests/test_basis.h +++ b/tests/test_basis.h @@ -31,10 +31,258 @@ #ifndef TEST_BASIS_H #define TEST_BASIS_H -#include "core/os/main_loop.h" +#include "core/math/random_number_generator.h" +#include "core/os/os.h" +#include "core/ustring.h" + +#include "tests/test_macros.h" namespace TestBasis { -MainLoop *test(); + +enum RotOrder { + EulerXYZ, + EulerXZY, + EulerYZX, + EulerYXZ, + EulerZXY, + EulerZYX +}; + +Vector3 deg2rad(const Vector3 &p_rotation) { + return p_rotation / 180.0 * Math_PI; +} + +Vector3 rad2deg(const Vector3 &p_rotation) { + return p_rotation / Math_PI * 180.0; +} + +Basis EulerToBasis(RotOrder mode, const Vector3 &p_rotation) { + Basis ret; + switch (mode) { + case EulerXYZ: + ret.set_euler_xyz(p_rotation); + break; + + case EulerXZY: + ret.set_euler_xzy(p_rotation); + break; + + case EulerYZX: + ret.set_euler_yzx(p_rotation); + break; + + case EulerYXZ: + ret.set_euler_yxz(p_rotation); + break; + + case EulerZXY: + ret.set_euler_zxy(p_rotation); + break; + + case EulerZYX: + ret.set_euler_zyx(p_rotation); + break; + + default: + // If you land here, Please integrate all rotation orders. + FAIL("This is not unreachable."); + } + + return ret; +} + +Vector3 BasisToEuler(RotOrder mode, const Basis &p_rotation) { + switch (mode) { + case EulerXYZ: + return p_rotation.get_euler_xyz(); + + case EulerXZY: + return p_rotation.get_euler_xzy(); + + case EulerYZX: + return p_rotation.get_euler_yzx(); + + case EulerYXZ: + return p_rotation.get_euler_yxz(); + + case EulerZXY: + return p_rotation.get_euler_zxy(); + + case EulerZYX: + return p_rotation.get_euler_zyx(); + + default: + // If you land here, Please integrate all rotation orders. + FAIL("This is not unreachable."); + return Vector3(); + } } +String get_rot_order_name(RotOrder ro) { + switch (ro) { + case EulerXYZ: + return "XYZ"; + case EulerXZY: + return "XZY"; + case EulerYZX: + return "YZX"; + case EulerYXZ: + return "YXZ"; + case EulerZXY: + return "ZXY"; + case EulerZYX: + return "ZYX"; + default: + return "[Not supported]"; + } +} + +void test_rotation(Vector3 deg_original_euler, RotOrder rot_order) { + // This test: + // 1. Converts the rotation vector from deg to rad. + // 2. Converts euler to basis. + // 3. Converts the above basis back into euler. + // 4. Converts the above euler into basis again. + // 5. Compares the basis obtained in step 2 with the basis of step 4 + // + // The conversion "basis to euler", done in the step 3, may be different from + // the original euler, even if the final rotation are the same. + // This happens because there are more ways to represents the same rotation, + // both valid, using eulers. + // For this reason is necessary to convert that euler back to basis and finally + // compares it. + // + // In this way we can assert that both functions: basis to euler / euler to basis + // are correct. + + // Euler to rotation + const Vector3 original_euler = deg2rad(deg_original_euler); + const Basis to_rotation = EulerToBasis(rot_order, original_euler); + + // Euler from rotation + const Vector3 euler_from_rotation = BasisToEuler(rot_order, to_rotation); + const Basis rotation_from_computed_euler = EulerToBasis(rot_order, euler_from_rotation); + + Basis res = to_rotation.inverse() * rotation_from_computed_euler; + + CHECK_MESSAGE((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() <= 0.1, vformat("Fail due to X %s\n", String(res.get_axis(0))).utf8().ptr()); + CHECK_MESSAGE((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() <= 0.1, vformat("Fail due to Y %s\n", String(res.get_axis(1))).utf8().ptr()); + CHECK_MESSAGE((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() <= 0.1, vformat("Fail due to Z %s\n", String(res.get_axis(2))).utf8().ptr()); + + // Double check `to_rotation` decomposing with XYZ rotation order. + const Vector3 euler_xyz_from_rotation = to_rotation.get_euler_xyz(); + Basis rotation_from_xyz_computed_euler; + rotation_from_xyz_computed_euler.set_euler_xyz(euler_xyz_from_rotation); + + res = to_rotation.inverse() * rotation_from_xyz_computed_euler; + + CHECK_MESSAGE((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() <= 0.1, vformat("Double check with XYZ rot order failed, due to X %s\n", String(res.get_axis(0))).utf8().ptr()); + CHECK_MESSAGE((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() <= 0.1, vformat("Double check with XYZ rot order failed, due to Y %s\n", String(res.get_axis(1))).utf8().ptr()); + CHECK_MESSAGE((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() <= 0.1, vformat("Double check with XYZ rot order failed, due to Z %s\n", String(res.get_axis(2))).utf8().ptr()); + + INFO(vformat("Rotation order: %s\n.", get_rot_order_name(rot_order)).utf8().ptr()); + INFO(vformat("Original Rotation: %s\n", String(deg_original_euler)).utf8().ptr()); + INFO(vformat("Quaternion to rotation order: %s\n", String(rad2deg(euler_from_rotation))).utf8().ptr()); +} + +TEST_CASE("[Basis] Euler conversions") { + Vector<RotOrder> rotorder_to_test; + rotorder_to_test.push_back(EulerXYZ); + rotorder_to_test.push_back(EulerXZY); + rotorder_to_test.push_back(EulerYZX); + rotorder_to_test.push_back(EulerYXZ); + rotorder_to_test.push_back(EulerZXY); + rotorder_to_test.push_back(EulerZYX); + + Vector<Vector3> vectors_to_test; + + // Test the special cases. + vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0)); + vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5)); + vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5)); + vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0)); + vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0)); + vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0)); + vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0)); + vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0)); + vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0)); + vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0)); + vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0)); + vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0)); + vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0)); + vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0)); + vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0)); + vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0)); + vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0)); + vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0)); + vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0)); + vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0)); + vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0)); + vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0)); + vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.0)); + vectors_to_test.push_back(Vector3(-90.0, 60.0, -90.0)); + vectors_to_test.push_back(Vector3(90.0, 60.0, -90.0)); + vectors_to_test.push_back(Vector3(90.0, -60.0, -90.0)); + vectors_to_test.push_back(Vector3(-90.0, -60.0, -90.0)); + vectors_to_test.push_back(Vector3(-90.0, 60.0, 90.0)); + vectors_to_test.push_back(Vector3(90.0, 60.0, 90.0)); + vectors_to_test.push_back(Vector3(90.0, -60.0, 90.0)); + vectors_to_test.push_back(Vector3(-90.0, -60.0, 90.0)); + vectors_to_test.push_back(Vector3(60.0, 90.0, -40.0)); + vectors_to_test.push_back(Vector3(60.0, -90.0, -40.0)); + vectors_to_test.push_back(Vector3(-60.0, -90.0, -40.0)); + vectors_to_test.push_back(Vector3(-60.0, 90.0, 40.0)); + vectors_to_test.push_back(Vector3(60.0, 90.0, 40.0)); + vectors_to_test.push_back(Vector3(60.0, -90.0, 40.0)); + vectors_to_test.push_back(Vector3(-60.0, -90.0, 40.0)); + vectors_to_test.push_back(Vector3(-90.0, 90.0, -90.0)); + vectors_to_test.push_back(Vector3(90.0, 90.0, -90.0)); + vectors_to_test.push_back(Vector3(90.0, -90.0, -90.0)); + vectors_to_test.push_back(Vector3(-90.0, -90.0, -90.0)); + vectors_to_test.push_back(Vector3(-90.0, 90.0, 90.0)); + vectors_to_test.push_back(Vector3(90.0, 90.0, 90.0)); + vectors_to_test.push_back(Vector3(90.0, -90.0, 90.0)); + vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0)); + vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0)); + vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.0)); + vectors_to_test.push_back(Vector3(-120.0, -150.0, -130.0)); + vectors_to_test.push_back(Vector3(120.0, -150.0, -130.0)); + vectors_to_test.push_back(Vector3(120.0, 150.0, -130.0)); + vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0)); + + for (int h = 0; h < rotorder_to_test.size(); h += 1) { + for (int i = 0; i < vectors_to_test.size(); i += 1) { + test_rotation(vectors_to_test[i], rotorder_to_test[h]); + } + } +} + +TEST_CASE("[Stress][Basis] Euler conversions") { + Vector<RotOrder> rotorder_to_test; + rotorder_to_test.push_back(EulerXYZ); + rotorder_to_test.push_back(EulerXZY); + rotorder_to_test.push_back(EulerYZX); + rotorder_to_test.push_back(EulerYXZ); + rotorder_to_test.push_back(EulerZXY); + rotorder_to_test.push_back(EulerZYX); + + Vector<Vector3> vectors_to_test; + // Add 1000 random vectors with weirds numbers. + RandomNumberGenerator rng; + for (int _ = 0; _ < 1000; _ += 1) { + vectors_to_test.push_back(Vector3( + rng.randf_range(-1800, 1800), + rng.randf_range(-1800, 1800), + rng.randf_range(-1800, 1800))); + } + + for (int h = 0; h < rotorder_to_test.size(); h += 1) { + for (int i = 0; i < vectors_to_test.size(); i += 1) { + test_rotation(vectors_to_test[i], rotorder_to_test[h]); + } + } +} + +} // namespace TestBasis + #endif diff --git a/tests/test_class_db.cpp b/tests/test_class_db.cpp deleted file mode 100644 index 3171091402..0000000000 --- a/tests/test_class_db.cpp +++ /dev/null @@ -1,882 +0,0 @@ -/*************************************************************************/ -/* test_class_db.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "test_class_db.h" - -#include "core/global_constants.h" -#include "core/ordered_hash_map.h" -#include "core/os/os.h" -#include "core/string_name.h" -#include "core/ustring.h" -#include "core/variant.h" - -namespace TestClassDB { - -enum class [[nodiscard]] TestResult{ - FAILED, - PASS -}; - -#define TEST_FAIL_COND(m_cond, m_msg) \ - if (unlikely(m_cond)) { \ - ERR_PRINT(m_msg); \ - return TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_COND(m_cond, m_msg) \ - if (unlikely(m_cond)) { \ - ERR_PRINT(m_msg); \ - __test_result__ = TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_FAIL_CHECK(m_test_expr) \ - if (unlikely((m_test_expr) == TestResult::FAILED)) { \ - return TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_CHECK(m_test_expr) \ - if (unlikely((m_test_expr) == TestResult::FAILED)) { \ - __test_result__ = TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_START() \ - TestResult __test_result__ = TestResult::PASS; \ - ((void)0) - -#define TEST_END() return __test_result__; - -struct TypeReference { - StringName name; - bool is_enum = false; -}; - -struct ConstantData { - String name; - int value = 0; -}; - -struct EnumData { - StringName name; - List<ConstantData> constants; - - _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const { - return p_enum.name == name; - } -}; - -struct PropertyData { - StringName name; - int index = 0; - - StringName getter; - StringName setter; -}; - -struct ArgumentData { - TypeReference type; - String name; - bool has_defval = false; - Variant defval; -}; - -struct MethodData { - StringName name; - TypeReference return_type; - List<ArgumentData> arguments; - bool is_virtual = false; - bool is_vararg = false; -}; - -struct SignalData { - StringName name; - List<ArgumentData> arguments; -}; - -struct ExposedClass { - StringName name; - StringName base; - - bool is_singleton = false; - bool is_instantiable = false; - bool is_reference = false; - - ClassDB::APIType api_type; - - List<ConstantData> constants; - List<EnumData> enums; - List<PropertyData> properties; - List<MethodData> methods; - List<SignalData> signals_; - - const PropertyData *find_property_by_name(const StringName &p_name) const { - for (const List<PropertyData>::Element *E = properties.front(); E; E = E->next()) { - if (E->get().name == p_name) { - return &E->get(); - } - } - - return nullptr; - } - - const MethodData *find_method_by_name(const StringName &p_name) const { - for (const List<MethodData>::Element *E = methods.front(); E; E = E->next()) { - if (E->get().name == p_name) { - return &E->get(); - } - } - - return nullptr; - } -}; - -struct NamesCache { - StringName variant_type = StaticCString::create("Variant"); - StringName object_class = StaticCString::create("Object"); - StringName reference_class = StaticCString::create("Reference"); - StringName string_type = StaticCString::create("String"); - StringName string_name_type = StaticCString::create("StringName"); - StringName node_path_type = StaticCString::create("NodePath"); - StringName bool_type = StaticCString::create("bool"); - StringName int_type = StaticCString::create("int"); - StringName float_type = StaticCString::create("float"); - StringName void_type = StaticCString::create("void"); - StringName vararg_stub_type = StaticCString::create("@VarArg@"); - StringName vector2_type = StaticCString::create("Vector2"); - StringName rect2_type = StaticCString::create("Rect2"); - StringName vector3_type = StaticCString::create("Vector3"); - - // Object not included as it must be checked for all derived classes - static constexpr int nullable_types_count = 17; - StringName nullable_types[nullable_types_count] = { - string_type, - string_name_type, - node_path_type, - - StaticCString::create(_STR(Array)), - StaticCString::create(_STR(Dictionary)), - StaticCString::create(_STR(Callable)), - StaticCString::create(_STR(Signal)), - - StaticCString::create(_STR(PackedByteArray)), - StaticCString::create(_STR(PackedInt32Array)), - StaticCString::create(_STR(PackedInt64rray)), - StaticCString::create(_STR(PackedFloat32Array)), - StaticCString::create(_STR(PackedFloat64Array)), - StaticCString::create(_STR(PackedStringArray)), - StaticCString::create(_STR(PackedVector2Array)), - StaticCString::create(_STR(PackedVector3Array)), - StaticCString::create(_STR(PackedColorArray)), - }; - - bool is_nullable_type(const StringName &p_type) const { - for (int i = 0; i < nullable_types_count; i++) { - if (p_type == nullable_types[i]) { - return true; - } - } - - return false; - } -}; - -typedef OrderedHashMap<StringName, ExposedClass> ExposedClasses; - -struct Context { - Vector<StringName> enum_types; - Vector<StringName> builtin_types; - ExposedClasses exposed_classes; - List<EnumData> global_enums; - NamesCache names_cache; - - const ExposedClass *find_exposed_class(const StringName &p_name) const { - ExposedClasses::ConstElement elem = exposed_classes.find(p_name); - return elem ? &elem.value() : nullptr; - } - - const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const { - ExposedClasses::ConstElement elem = exposed_classes.find(p_type_ref.name); - return elem ? &elem.value() : nullptr; - } - - bool has_type(const TypeReference &p_type_ref) const { - if (builtin_types.find(p_type_ref.name) >= 0) { - return true; - } - - if (p_type_ref.is_enum) { - if (enum_types.find(p_type_ref.name) >= 0) { - return true; - } - - // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead. - return builtin_types.find(names_cache.int_type); - } - - return false; - } -}; - -bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type) { - if (p_arg_type.name == p_context.names_cache.variant_type) { - // Variant can take anything - return true; - } - - switch (p_val.get_type()) { - case Variant::NIL: - return p_context.find_exposed_class(p_arg_type) || - p_context.names_cache.is_nullable_type(p_arg_type.name); - case Variant::BOOL: - return p_arg_type.name == p_context.names_cache.bool_type; - case Variant::INT: - return p_arg_type.name == p_context.names_cache.int_type || - p_arg_type.name == p_context.names_cache.float_type || - p_arg_type.is_enum; - case Variant::FLOAT: - return p_arg_type.name == p_context.names_cache.float_type; - case Variant::STRING: - case Variant::STRING_NAME: - return p_arg_type.name == p_context.names_cache.string_type || - p_arg_type.name == p_context.names_cache.string_name_type || - p_arg_type.name == p_context.names_cache.node_path_type; - case Variant::NODE_PATH: - return p_arg_type.name == p_context.names_cache.node_path_type; - case Variant::TRANSFORM: - case Variant::TRANSFORM2D: - case Variant::BASIS: - case Variant::QUAT: - case Variant::PLANE: - case Variant::AABB: - case Variant::COLOR: - case Variant::VECTOR2: - case Variant::RECT2: - case Variant::VECTOR3: - case Variant::_RID: - case Variant::ARRAY: - case Variant::DICTIONARY: - case Variant::PACKED_BYTE_ARRAY: - case Variant::PACKED_INT32_ARRAY: - case Variant::PACKED_INT64_ARRAY: - case Variant::PACKED_FLOAT32_ARRAY: - case Variant::PACKED_FLOAT64_ARRAY: - case Variant::PACKED_STRING_ARRAY: - case Variant::PACKED_VECTOR2_ARRAY: - case Variant::PACKED_VECTOR3_ARRAY: - case Variant::PACKED_COLOR_ARRAY: - case Variant::CALLABLE: - case Variant::SIGNAL: - return p_arg_type.name == Variant::get_type_name(p_val.get_type()); - case Variant::OBJECT: - return p_context.find_exposed_class(p_arg_type); - case Variant::VECTOR2I: - return p_arg_type.name == p_context.names_cache.vector2_type || - p_arg_type.name == Variant::get_type_name(p_val.get_type()); - case Variant::RECT2I: - return p_arg_type.name == p_context.names_cache.rect2_type || - p_arg_type.name == Variant::get_type_name(p_val.get_type()); - case Variant::VECTOR3I: - return p_arg_type.name == p_context.names_cache.vector3_type || - p_arg_type.name == Variant::get_type_name(p_val.get_type()); - default: - ERR_PRINT("Unexpected Variant type: " + itos(p_val.get_type())); - break; - } - - return false; -} - -TestResult validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) { - TEST_START(); - - const MethodData *setter = p_class.find_method_by_name(p_prop.setter); - - // Search it in base classes too - const ExposedClass *top = &p_class; - while (!setter && top->base != StringName()) { - top = p_context.find_exposed_class(top->base); - TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'."); - setter = top->find_method_by_name(p_prop.setter); - } - - const MethodData *getter = p_class.find_method_by_name(p_prop.getter); - - // Search it in base classes too - top = &p_class; - while (!getter && top->base != StringName()) { - top = p_context.find_exposed_class(top->base); - TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'."); - getter = top->find_method_by_name(p_prop.getter); - } - - TEST_FAIL_COND(!setter && !getter, - "Couldn't find neither the setter nor the getter for property: '" + p_class.name + "." + String(p_prop.name) + "'."); - - if (setter) { - int setter_argc = p_prop.index != -1 ? 2 : 1; - TEST_FAIL_COND(setter->arguments.size() != setter_argc, - "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - - if (getter) { - int getter_argc = p_prop.index != -1 ? 1 : 0; - TEST_FAIL_COND(getter->arguments.size() != getter_argc, - "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - - if (getter && setter) { - const ArgumentData &setter_first_arg = setter->arguments.back()->get(); - if (getter->return_type.name != setter_first_arg.type.name) { - // Special case for Node::set_name - bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type && - setter_first_arg.type.name == p_context.names_cache.string_type; - - TEST_FAIL_COND(!whitelisted, - "Return type from getter doesn't match first argument of setter, for property: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - } - - const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type; - - const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref); - if (prop_class) { - TEST_COND(prop_class->is_singleton, - "Property type is a singleton: '" + p_class.name + "." + String(p_prop.name) + "'."); - } else { - TEST_FAIL_COND(!p_context.has_type(prop_type_ref), - "Property type '" + prop_type_ref.name + "' not found: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - - if (getter) { - if (p_prop.index != -1) { - const ArgumentData &idx_arg = getter->arguments.front()->get(); - if (idx_arg.type.name != p_context.names_cache.int_type) { - // If not an int, it can be an enum - TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0, - "Invalid type '" + idx_arg.type.name + "' for index argument of property getter: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - } - } - - if (setter) { - if (p_prop.index != -1) { - const ArgumentData &idx_arg = setter->arguments.front()->get(); - if (idx_arg.type.name != p_context.names_cache.int_type) { - // Assume the index parameter is an enum - // If not an int, it can be an enum - TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0, - "Invalid type '" + idx_arg.type.name + "' for index argument of property setter: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - } - } - - TEST_END(); -} - -TestResult validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) { - TEST_START(); - - const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type); - if (return_class) { - TEST_COND(return_class->is_singleton, - "Method return type is a singleton: '" + p_class.name + "." + p_method.name + "'."); - } - - for (const List<ArgumentData>::Element *F = p_method.arguments.front(); F; F = F->next()) { - const ArgumentData &arg = F->get(); - - const ExposedClass *arg_class = p_context.find_exposed_class(arg.type); - if (arg_class) { - TEST_COND(arg_class->is_singleton, - "Argument type is a singleton: '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'."); - } else { - TEST_FAIL_COND(!p_context.has_type(arg.type), - "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of method" + p_class.name + "." + p_method.name + "'."); - } - - if (arg.has_defval) { - TEST_COND(!arg_default_value_is_assignable_to_type(p_context, arg.defval, arg.type), - "Invalid default value for parameter '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'."); - } - } - - TEST_END(); -} - -TestResult validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) { - TEST_START(); - - for (const List<ArgumentData>::Element *F = p_signal.arguments.front(); F; F = F->next()) { - const ArgumentData &arg = F->get(); - - const ExposedClass *arg_class = p_context.find_exposed_class(arg.type); - if (arg_class) { - TEST_COND(arg_class->is_singleton, - "Argument class is a singleton: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'."); - } else { - TEST_FAIL_COND(!p_context.has_type(arg.type), - "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'."); - } - } - - TEST_END(); -} - -TestResult validate_class(const Context &p_context, const ExposedClass &p_exposed_class) { - TEST_START(); - - bool is_derived_type = p_exposed_class.base != StringName(); - - if (!is_derived_type) { - // Asserts about the base Object class - TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class, - "Class '" + p_exposed_class.name + "' has no base class."); - TEST_FAIL_COND(!p_exposed_class.is_instantiable, - "Object class is not instantiable."); - TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE, - "Object class is API is not API_CORE."); - TEST_FAIL_COND(p_exposed_class.is_singleton, - "Object class is registered as a singleton."); - } - - TEST_FAIL_COND(p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class, - "Singleton base class '" + String(p_exposed_class.base) + "' is not Object, for class '" + p_exposed_class.name + "'."); - - TEST_FAIL_COND(is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base), - "Base type '" + p_exposed_class.base.operator String() + "' does not exist, for class '" + p_exposed_class.name + "'."); - - for (const List<PropertyData>::Element *F = p_exposed_class.properties.front(); F; F = F->next()) { - TEST_CHECK(validate_property(p_context, p_exposed_class, F->get())); - } - - for (const List<MethodData>::Element *F = p_exposed_class.methods.front(); F; F = F->next()) { - TEST_CHECK(validate_method(p_context, p_exposed_class, F->get())); - } - - for (const List<SignalData>::Element *F = p_exposed_class.signals_.front(); F; F = F->next()) { - TEST_CHECK(validate_signal(p_context, p_exposed_class, F->get())); - } - - TEST_END(); -} - -TestResult add_exposed_classes(Context &r_context) { - TEST_START(); - - List<StringName> class_list; - ClassDB::get_class_list(&class_list); - class_list.sort_custom<StringName::AlphCompare>(); - - while (class_list.size()) { - StringName class_name = class_list.front()->get(); - - ClassDB::APIType api_type = ClassDB::get_api_type(class_name); - - if (api_type == ClassDB::API_NONE) { - class_list.pop_front(); - continue; - } - - if (!ClassDB::is_class_exposed(class_name)) { - OS::get_singleton()->print("Ignoring class '%s' because it's not exposed\n", String(class_name).utf8().get_data()); - class_list.pop_front(); - continue; - } - - if (!ClassDB::is_class_enabled(class_name)) { - OS::get_singleton()->print("Ignoring class '%s' because it's not enabled\n", String(class_name).utf8().get_data()); - class_list.pop_front(); - continue; - } - - ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name); - - ExposedClass exposed_class; - exposed_class.name = class_name; - exposed_class.api_type = api_type; - exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name); - exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton; - exposed_class.is_reference = ClassDB::is_parent_class(class_name, "Reference"); - exposed_class.base = ClassDB::get_parent_class(class_name); - - // Add properties - - List<PropertyInfo> property_list; - ClassDB::get_property_list(class_name, &property_list, true); - - Map<StringName, StringName> accessor_methods; - - for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) { - const PropertyInfo &property = E->get(); - - if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY) { - continue; - } - - PropertyData prop; - prop.name = property.name; - prop.setter = ClassDB::get_property_setter(class_name, prop.name); - prop.getter = ClassDB::get_property_getter(class_name, prop.name); - - if (prop.setter != StringName()) { - accessor_methods[prop.setter] = prop.name; - } - if (prop.getter != StringName()) { - accessor_methods[prop.getter] = prop.name; - } - - bool valid = false; - prop.index = ClassDB::get_property_index(class_name, prop.name, &valid); - TEST_FAIL_COND(!valid, "Invalid property: '" + exposed_class.name + "." + String(prop.name) + "'."); - - exposed_class.properties.push_back(prop); - } - - // Add methods - - List<MethodInfo> virtual_method_list; - ClassDB::get_virtual_methods(class_name, &virtual_method_list, true); - - List<MethodInfo> method_list; - ClassDB::get_method_list(class_name, &method_list, true); - method_list.sort(); - - for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) { - const MethodInfo &method_info = E->get(); - - int argc = method_info.arguments.size(); - - if (method_info.name.empty()) { - continue; - } - - MethodData method; - method.name = method_info.name; - - if (method_info.flags & METHOD_FLAG_VIRTUAL) { - method.is_virtual = true; - } - - PropertyInfo return_info = method_info.return_val; - - MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name); - - method.is_vararg = m && m->is_vararg(); - - if (!m && !method.is_virtual) { - TEST_FAIL_COND(!virtual_method_list.find(method_info), - "Missing MethodBind for non-virtual method: '" + exposed_class.name + "." + method.name + "'."); - - // A virtual method without the virtual flag. This is a special case. - - // The method Object.free is registered as a virtual method, but without the virtual flag. - // This is because this method is not supposed to be overridden, but called. - // We assume the return type is void. - method.return_type.name = r_context.names_cache.void_type; - - // Actually, more methods like this may be added in the future, which could return - // something different. Let's put this check to notify us if that ever happens. - if (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free") { - WARN_PRINT("Notification: New unexpected virtual non-overridable method found." - " We only expected Object.free, but found '" + - exposed_class.name + "." + method.name + "'."); - } - } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - method.return_type.name = return_info.class_name; - method.return_type.is_enum = true; - } else if (return_info.class_name != StringName()) { - method.return_type.name = return_info.class_name; - - bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE && - ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.reference_class); - TEST_COND(bad_reference_hint, String() + "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'." + - " Are you returning a reference type by pointer? Method: '" + - exposed_class.name + "." + method.name + "'."); - } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { - method.return_type.name = return_info.hint_string; - } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { - method.return_type.name = r_context.names_cache.variant_type; - } else if (return_info.type == Variant::NIL) { - method.return_type.name = r_context.names_cache.void_type; - } else { - // NOTE: We don't care about the size and sign of int and float in these tests - method.return_type.name = Variant::get_type_name(return_info.type); - } - - for (int i = 0; i < argc; i++) { - PropertyInfo arg_info = method_info.arguments[i]; - - String orig_arg_name = arg_info.name; - - ArgumentData arg; - arg.name = orig_arg_name; - - if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - arg.type.name = arg_info.class_name; - arg.type.is_enum = true; - } else if (arg_info.class_name != StringName()) { - arg.type.name = arg_info.class_name; - } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { - arg.type.name = arg_info.hint_string; - } else if (arg_info.type == Variant::NIL) { - arg.type.name = r_context.names_cache.variant_type; - } else { - // NOTE: We don't care about the size and sign of int and float in these tests - arg.type.name = Variant::get_type_name(arg_info.type); - } - - if (m && m->has_default_argument(i)) { - arg.has_defval = true; - arg.defval = m->get_default_argument(i); - } - - method.arguments.push_back(arg); - } - - if (method.is_vararg) { - ArgumentData vararg; - vararg.type.name = r_context.names_cache.vararg_stub_type; - vararg.name = "@varargs@"; - method.arguments.push_back(vararg); - } - - TEST_COND(exposed_class.find_property_by_name(method.name), - "Method name conflicts with property: '" + String(class_name) + "." + String(method.name) + "'."); - - // Classes starting with an underscore are ignored unless they're used as a property setter or getter - if (!method.is_virtual && String(method.name)[0] == '_') { - for (const List<PropertyData>::Element *F = exposed_class.properties.front(); F; F = F->next()) { - const PropertyData &prop = F->get(); - - if (prop.setter == method.name || prop.getter == method.name) { - exposed_class.methods.push_back(method); - break; - } - } - } else { - exposed_class.methods.push_back(method); - } - } - - // Add signals - - const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map; - const StringName *k = nullptr; - - while ((k = signal_map.next(k))) { - SignalData signal; - - const MethodInfo &method_info = signal_map.get(*k); - - signal.name = method_info.name; - - int argc = method_info.arguments.size(); - - for (int i = 0; i < argc; i++) { - PropertyInfo arg_info = method_info.arguments[i]; - - String orig_arg_name = arg_info.name; - - ArgumentData arg; - arg.name = orig_arg_name; - - if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - arg.type.name = arg_info.class_name; - arg.type.is_enum = true; - } else if (arg_info.class_name != StringName()) { - arg.type.name = arg_info.class_name; - } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { - arg.type.name = arg_info.hint_string; - } else if (arg_info.type == Variant::NIL) { - arg.type.name = r_context.names_cache.variant_type; - } else { - // NOTE: We don't care about the size and sign of int and float in these tests - arg.type.name = Variant::get_type_name(arg_info.type); - } - - signal.arguments.push_back(arg); - } - - bool method_conflict = exposed_class.find_property_by_name(signal.name); - - if (method_conflict || exposed_class.find_method_by_name(signal.name)) { - // TODO: - // ClassDB allows signal names that conflict with method or property names. - // However registering a signal with a conflicting name is still considered wrong. - // Unfortunately there are some existing cases that are yet to be fixed. - // Until those are fixed we will print a warning instead of failing the test. - WARN_PRINT("Signal name conflicts with " + String(method_conflict ? "method" : "property") + - ": '" + String(class_name) + "." + String(signal.name) + "'."); - } - - exposed_class.signals_.push_back(signal); - } - - // Add enums and constants - - List<String> constants; - ClassDB::get_integer_constant_list(class_name, &constants, true); - - const HashMap<StringName, List<StringName>> &enum_map = class_info->enum_map; - k = nullptr; - - while ((k = enum_map.next(k))) { - EnumData enum_; - enum_.name = *k; - - const List<StringName> &enum_constants = enum_map.get(*k); - for (const List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) { - const StringName &constant_name = E->get(); - int *value = class_info->constant_map.getptr(constant_name); - TEST_FAIL_COND(!value, "Missing enum constant value: '" + - String(class_name) + "." + String(enum_.name) + "." + String(constant_name) + "'."); - constants.erase(constant_name); - - ConstantData constant; - constant.name = constant_name; - constant.value = *value; - - enum_.constants.push_back(constant); - } - - exposed_class.enums.push_back(enum_); - - r_context.enum_types.push_back(String(class_name) + "." + String(*k)); - } - - for (const List<String>::Element *E = constants.front(); E; E = E->next()) { - const String &constant_name = E->get(); - int *value = class_info->constant_map.getptr(StringName(E->get())); - TEST_FAIL_COND(!value, "Missing enum constant value: '" + String(class_name) + "." + String(constant_name) + "'."); - - ConstantData constant; - constant.name = constant_name; - constant.value = *value; - - exposed_class.constants.push_back(constant); - } - - r_context.exposed_classes.insert(class_name, exposed_class); - class_list.pop_front(); - } - - TEST_END(); -} - -void add_builtin_types(Context &r_context) { - // NOTE: We don't care about the size and sign of int and float in these tests - for (int i = 0; i < Variant::VARIANT_MAX; i++) { - r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i))); - } - - r_context.builtin_types.push_back(_STR(Variant)); - r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type); - r_context.builtin_types.push_back("void"); -} - -void add_global_enums(Context &r_context) { - int global_constants_count = GlobalConstants::get_global_constant_count(); - - if (global_constants_count > 0) { - for (int i = 0; i < global_constants_count; i++) { - StringName enum_name = GlobalConstants::get_global_constant_enum(i); - - if (enum_name != StringName()) { - ConstantData constant; - constant.name = GlobalConstants::get_global_constant_name(i); - constant.value = GlobalConstants::get_global_constant_value(i); - - EnumData enum_; - enum_.name = enum_name; - List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_); - if (enum_match) { - enum_match->get().constants.push_back(constant); - } else { - enum_.constants.push_back(constant); - r_context.global_enums.push_back(enum_); - } - } - } - - for (List<EnumData>::Element *E = r_context.global_enums.front(); E; E = E->next()) { - r_context.enum_types.push_back(E->get().name); - } - } - - // HARDCODED - List<StringName> hardcoded_enums; - hardcoded_enums.push_back("Vector2.Axis"); - hardcoded_enums.push_back("Vector2i.Axis"); - hardcoded_enums.push_back("Vector3.Axis"); - hardcoded_enums.push_back("Vector3i.Axis"); - for (List<StringName>::Element *E = hardcoded_enums.front(); E; E = E->next()) { - // These enums are not generated and must be written manually (e.g.: Vector3.Axis) - // Here, we assume core types do not begin with underscore - r_context.enum_types.push_back(E->get()); - } -} - -TestResult run_class_db_tests() { - TEST_START(); - - Context context; - - TEST_FAIL_CHECK(add_exposed_classes(context)); - add_builtin_types(context); - add_global_enums(context); - - const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class); - TEST_FAIL_COND(!object_class, "Object class not found."); - TEST_FAIL_COND(object_class->base != StringName(), - "Object class derives from another class: '" + object_class->base + "'."); - - for (ExposedClasses::Element E = context.exposed_classes.front(); E; E = E.next()) { - TEST_CHECK(validate_class(context, E.value())); - } - - TEST_END(); -} - -MainLoop *test() { - TestResult pass = run_class_db_tests(); - - OS::get_singleton()->print("ClassDB tests: %s\n", pass == TestResult::PASS ? "PASS" : "FAILED"); - - if (pass == TestResult::FAILED) { - OS::get_singleton()->set_exit_code(pass == TestResult::PASS ? 0 : 1); - } - - return nullptr; -} - -} // namespace TestClassDB diff --git a/tests/test_class_db.h b/tests/test_class_db.h index 1a31cfb01b..d0d8136874 100644 --- a/tests/test_class_db.h +++ b/tests/test_class_db.h @@ -31,12 +31,806 @@ #ifndef GODOT_TEST_CLASS_DB_H #define GODOT_TEST_CLASS_DB_H -#include "core/os/main_loop.h" +#include "core/register_core_types.h" + +#include "core/global_constants.h" +#include "core/ordered_hash_map.h" +#include "core/os/os.h" +#include "core/string_name.h" +#include "core/ustring.h" +#include "core/variant.h" + +#include "tests/test_macros.h" + +#define TEST_COND DOCTEST_CHECK_FALSE_MESSAGE +#define TEST_FAIL DOCTEST_FAIL +#define TEST_FAIL_COND DOCTEST_REQUIRE_FALSE_MESSAGE +#define TEST_FAIL_COND_WARN DOCTEST_WARN_FALSE_MESSAGE namespace TestClassDB { -MainLoop *test(); +struct TypeReference { + StringName name; + bool is_enum = false; +}; + +struct ConstantData { + String name; + int value = 0; +}; + +struct EnumData { + StringName name; + List<ConstantData> constants; + + _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const { + return p_enum.name == name; + } +}; + +struct PropertyData { + StringName name; + int index = 0; + + StringName getter; + StringName setter; +}; + +struct ArgumentData { + TypeReference type; + String name; + bool has_defval = false; + Variant defval; +}; + +struct MethodData { + StringName name; + TypeReference return_type; + List<ArgumentData> arguments; + bool is_virtual = false; + bool is_vararg = false; +}; + +struct SignalData { + StringName name; + List<ArgumentData> arguments; +}; + +struct ExposedClass { + StringName name; + StringName base; + + bool is_singleton = false; + bool is_instantiable = false; + bool is_reference = false; + + ClassDB::APIType api_type; + + List<ConstantData> constants; + List<EnumData> enums; + List<PropertyData> properties; + List<MethodData> methods; + List<SignalData> signals_; + + const PropertyData *find_property_by_name(const StringName &p_name) const { + for (const List<PropertyData>::Element *E = properties.front(); E; E = E->next()) { + if (E->get().name == p_name) { + return &E->get(); + } + } + + return nullptr; + } + + const MethodData *find_method_by_name(const StringName &p_name) const { + for (const List<MethodData>::Element *E = methods.front(); E; E = E->next()) { + if (E->get().name == p_name) { + return &E->get(); + } + } + + return nullptr; + } +}; + +struct NamesCache { + StringName variant_type = StaticCString::create("Variant"); + StringName object_class = StaticCString::create("Object"); + StringName reference_class = StaticCString::create("Reference"); + StringName string_type = StaticCString::create("String"); + StringName string_name_type = StaticCString::create("StringName"); + StringName node_path_type = StaticCString::create("NodePath"); + StringName bool_type = StaticCString::create("bool"); + StringName int_type = StaticCString::create("int"); + StringName float_type = StaticCString::create("float"); + StringName void_type = StaticCString::create("void"); + StringName vararg_stub_type = StaticCString::create("@VarArg@"); + StringName vector2_type = StaticCString::create("Vector2"); + StringName rect2_type = StaticCString::create("Rect2"); + StringName vector3_type = StaticCString::create("Vector3"); + // Object not included as it must be checked for all derived classes + static constexpr int nullable_types_count = 17; + StringName nullable_types[nullable_types_count] = { + string_type, + string_name_type, + node_path_type, + + StaticCString::create(_STR(Array)), + StaticCString::create(_STR(Dictionary)), + StaticCString::create(_STR(Callable)), + StaticCString::create(_STR(Signal)), + + StaticCString::create(_STR(PackedByteArray)), + StaticCString::create(_STR(PackedInt32Array)), + StaticCString::create(_STR(PackedInt64rray)), + StaticCString::create(_STR(PackedFloat32Array)), + StaticCString::create(_STR(PackedFloat64Array)), + StaticCString::create(_STR(PackedStringArray)), + StaticCString::create(_STR(PackedVector2Array)), + StaticCString::create(_STR(PackedVector3Array)), + StaticCString::create(_STR(PackedColorArray)), + }; + + bool is_nullable_type(const StringName &p_type) const { + for (int i = 0; i < nullable_types_count; i++) { + if (p_type == nullable_types[i]) { + return true; + } + } + + return false; + } +}; + +typedef OrderedHashMap<StringName, ExposedClass> ExposedClasses; + +struct Context { + Vector<StringName> enum_types; + Vector<StringName> builtin_types; + ExposedClasses exposed_classes; + List<EnumData> global_enums; + NamesCache names_cache; + + const ExposedClass *find_exposed_class(const StringName &p_name) const { + ExposedClasses::ConstElement elem = exposed_classes.find(p_name); + return elem ? &elem.value() : nullptr; + } + + const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const { + ExposedClasses::ConstElement elem = exposed_classes.find(p_type_ref.name); + return elem ? &elem.value() : nullptr; + } + + bool has_type(const TypeReference &p_type_ref) const { + if (builtin_types.find(p_type_ref.name) >= 0) { + return true; + } + + if (p_type_ref.is_enum) { + if (enum_types.find(p_type_ref.name) >= 0) { + return true; + } + + // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead. + return builtin_types.find(names_cache.int_type); + } + + return false; + } +}; + +bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type, String *r_err_msg = nullptr) { + if (p_arg_type.name == p_context.names_cache.variant_type) { + // Variant can take anything + return true; + } + + switch (p_val.get_type()) { + case Variant::NIL: + return p_context.find_exposed_class(p_arg_type) || + p_context.names_cache.is_nullable_type(p_arg_type.name); + case Variant::BOOL: + return p_arg_type.name == p_context.names_cache.bool_type; + case Variant::INT: + return p_arg_type.name == p_context.names_cache.int_type || + p_arg_type.name == p_context.names_cache.float_type || + p_arg_type.is_enum; + case Variant::FLOAT: + return p_arg_type.name == p_context.names_cache.float_type; + case Variant::STRING: + case Variant::STRING_NAME: + return p_arg_type.name == p_context.names_cache.string_type || + p_arg_type.name == p_context.names_cache.string_name_type || + p_arg_type.name == p_context.names_cache.node_path_type; + case Variant::NODE_PATH: + return p_arg_type.name == p_context.names_cache.node_path_type; + case Variant::TRANSFORM: + case Variant::TRANSFORM2D: + case Variant::BASIS: + case Variant::QUAT: + case Variant::PLANE: + case Variant::AABB: + case Variant::COLOR: + case Variant::VECTOR2: + case Variant::RECT2: + case Variant::VECTOR3: + case Variant::_RID: + case Variant::ARRAY: + case Variant::DICTIONARY: + case Variant::PACKED_BYTE_ARRAY: + case Variant::PACKED_INT32_ARRAY: + case Variant::PACKED_INT64_ARRAY: + case Variant::PACKED_FLOAT32_ARRAY: + case Variant::PACKED_FLOAT64_ARRAY: + case Variant::PACKED_STRING_ARRAY: + case Variant::PACKED_VECTOR2_ARRAY: + case Variant::PACKED_VECTOR3_ARRAY: + case Variant::PACKED_COLOR_ARRAY: + case Variant::CALLABLE: + case Variant::SIGNAL: + return p_arg_type.name == Variant::get_type_name(p_val.get_type()); + case Variant::OBJECT: + return p_context.find_exposed_class(p_arg_type); + case Variant::VECTOR2I: + return p_arg_type.name == p_context.names_cache.vector2_type || + p_arg_type.name == Variant::get_type_name(p_val.get_type()); + case Variant::RECT2I: + return p_arg_type.name == p_context.names_cache.rect2_type || + p_arg_type.name == Variant::get_type_name(p_val.get_type()); + case Variant::VECTOR3I: + return p_arg_type.name == p_context.names_cache.vector3_type || + p_arg_type.name == Variant::get_type_name(p_val.get_type()); + default: + if (r_err_msg) { + *r_err_msg = "Unexpected Variant type: " + itos(p_val.get_type()); + } + break; + } + + return false; } +void validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) { + const MethodData *setter = p_class.find_method_by_name(p_prop.setter); + + // Search it in base classes too + const ExposedClass *top = &p_class; + while (!setter && top->base != StringName()) { + top = p_context.find_exposed_class(top->base); + TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'."); + setter = top->find_method_by_name(p_prop.setter); + } + + const MethodData *getter = p_class.find_method_by_name(p_prop.getter); + + // Search it in base classes too + top = &p_class; + while (!getter && top->base != StringName()) { + top = p_context.find_exposed_class(top->base); + TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'."); + getter = top->find_method_by_name(p_prop.getter); + } + + TEST_FAIL_COND((!setter && !getter), + "Couldn't find neither the setter nor the getter for property: '" + p_class.name + "." + String(p_prop.name) + "'."); + + if (setter) { + int setter_argc = p_prop.index != -1 ? 2 : 1; + TEST_FAIL_COND(setter->arguments.size() != setter_argc, + "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'."); + } + + if (getter) { + int getter_argc = p_prop.index != -1 ? 1 : 0; + TEST_FAIL_COND(getter->arguments.size() != getter_argc, + "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'."); + } + + if (getter && setter) { + const ArgumentData &setter_first_arg = setter->arguments.back()->get(); + if (getter->return_type.name != setter_first_arg.type.name) { + // Special case for Node::set_name + bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type && + setter_first_arg.type.name == p_context.names_cache.string_type; + + TEST_FAIL_COND(!whitelisted, + "Return type from getter doesn't match first argument of setter, for property: '" + p_class.name + "." + String(p_prop.name) + "'."); + } + } + + const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type; + + const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref); + if (prop_class) { + TEST_COND(prop_class->is_singleton, + "Property type is a singleton: '" + p_class.name + "." + String(p_prop.name) + "'."); + } else { + TEST_FAIL_COND(!p_context.has_type(prop_type_ref), + "Property type '" + prop_type_ref.name + "' not found: '" + p_class.name + "." + String(p_prop.name) + "'."); + } + + if (getter) { + if (p_prop.index != -1) { + const ArgumentData &idx_arg = getter->arguments.front()->get(); + if (idx_arg.type.name != p_context.names_cache.int_type) { + // If not an int, it can be an enum + TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0, + "Invalid type '" + idx_arg.type.name + "' for index argument of property getter: '" + p_class.name + "." + String(p_prop.name) + "'."); + } + } + } + + if (setter) { + if (p_prop.index != -1) { + const ArgumentData &idx_arg = setter->arguments.front()->get(); + if (idx_arg.type.name != p_context.names_cache.int_type) { + // Assume the index parameter is an enum + // If not an int, it can be an enum + TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0, + "Invalid type '" + idx_arg.type.name + "' for index argument of property setter: '" + p_class.name + "." + String(p_prop.name) + "'."); + } + } + } +} + +void validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) { + const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type); + if (return_class) { + TEST_COND(return_class->is_singleton, + "Method return type is a singleton: '" + p_class.name + "." + p_method.name + "'."); + } + + for (const List<ArgumentData>::Element *F = p_method.arguments.front(); F; F = F->next()) { + const ArgumentData &arg = F->get(); + + const ExposedClass *arg_class = p_context.find_exposed_class(arg.type); + if (arg_class) { + TEST_COND(arg_class->is_singleton, + "Argument type is a singleton: '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'."); + } else { + TEST_FAIL_COND(!p_context.has_type(arg.type), + "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of method" + p_class.name + "." + p_method.name + "'."); + } + + if (arg.has_defval) { + String type_error_msg; + bool arg_defval_assignable_to_type = arg_default_value_is_assignable_to_type(p_context, arg.defval, arg.type, &type_error_msg); + String err_msg = vformat("Invalid default value for parameter '%s' of method '%s.%s'.", arg.name, p_class.name, p_method.name); + if (!type_error_msg.empty()) { + err_msg += " " + type_error_msg; + } + TEST_COND(!arg_defval_assignable_to_type, err_msg.utf8().get_data()); + } + } +} + +void validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) { + for (const List<ArgumentData>::Element *F = p_signal.arguments.front(); F; F = F->next()) { + const ArgumentData &arg = F->get(); + + const ExposedClass *arg_class = p_context.find_exposed_class(arg.type); + if (arg_class) { + TEST_COND(arg_class->is_singleton, + "Argument class is a singleton: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'."); + } else { + TEST_FAIL_COND(!p_context.has_type(arg.type), + "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'."); + } + } +} + +void validate_class(const Context &p_context, const ExposedClass &p_exposed_class) { + bool is_derived_type = p_exposed_class.base != StringName(); + + if (!is_derived_type) { + // Asserts about the base Object class + TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class, + "Class '" + p_exposed_class.name + "' has no base class."); + TEST_FAIL_COND(!p_exposed_class.is_instantiable, + "Object class is not instantiable."); + TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE, + "Object class is API is not API_CORE."); + TEST_FAIL_COND(p_exposed_class.is_singleton, + "Object class is registered as a singleton."); + } + + TEST_FAIL_COND((p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class), + "Singleton base class '" + String(p_exposed_class.base) + "' is not Object, for class '" + p_exposed_class.name + "'."); + + TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)), + "Base type '" + p_exposed_class.base.operator String() + "' does not exist, for class '" + p_exposed_class.name + "'."); + + for (const List<PropertyData>::Element *F = p_exposed_class.properties.front(); F; F = F->next()) { + validate_property(p_context, p_exposed_class, F->get()); + } + + for (const List<MethodData>::Element *F = p_exposed_class.methods.front(); F; F = F->next()) { + validate_method(p_context, p_exposed_class, F->get()); + } + + for (const List<SignalData>::Element *F = p_exposed_class.signals_.front(); F; F = F->next()) { + validate_signal(p_context, p_exposed_class, F->get()); + } +} + +void add_exposed_classes(Context &r_context) { + List<StringName> class_list; + ClassDB::get_class_list(&class_list); + class_list.sort_custom<StringName::AlphCompare>(); + + while (class_list.size()) { + StringName class_name = class_list.front()->get(); + + ClassDB::APIType api_type = ClassDB::get_api_type(class_name); + + if (api_type == ClassDB::API_NONE) { + class_list.pop_front(); + continue; + } + + if (!ClassDB::is_class_exposed(class_name)) { + MESSAGE(vformat("Ignoring class '%s' because it's not exposed.", class_name).utf8().get_data()); + class_list.pop_front(); + continue; + } + + if (!ClassDB::is_class_enabled(class_name)) { + MESSAGE(vformat("Ignoring class '%s' because it's not enabled.", class_name).utf8().get_data()); + class_list.pop_front(); + continue; + } + + ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name); + + ExposedClass exposed_class; + exposed_class.name = class_name; + exposed_class.api_type = api_type; + exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name); + exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton; + exposed_class.is_reference = ClassDB::is_parent_class(class_name, "Reference"); + exposed_class.base = ClassDB::get_parent_class(class_name); + + // Add properties + + List<PropertyInfo> property_list; + ClassDB::get_property_list(class_name, &property_list, true); + + Map<StringName, StringName> accessor_methods; + + for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) { + const PropertyInfo &property = E->get(); + + if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY) { + continue; + } + + PropertyData prop; + prop.name = property.name; + prop.setter = ClassDB::get_property_setter(class_name, prop.name); + prop.getter = ClassDB::get_property_getter(class_name, prop.name); + + if (prop.setter != StringName()) { + accessor_methods[prop.setter] = prop.name; + } + if (prop.getter != StringName()) { + accessor_methods[prop.getter] = prop.name; + } + + bool valid = false; + prop.index = ClassDB::get_property_index(class_name, prop.name, &valid); + TEST_FAIL_COND(!valid, "Invalid property: '" + exposed_class.name + "." + String(prop.name) + "'."); + + exposed_class.properties.push_back(prop); + } + + // Add methods + + List<MethodInfo> virtual_method_list; + ClassDB::get_virtual_methods(class_name, &virtual_method_list, true); + + List<MethodInfo> method_list; + ClassDB::get_method_list(class_name, &method_list, true); + method_list.sort(); + + for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) { + const MethodInfo &method_info = E->get(); + + int argc = method_info.arguments.size(); + + if (method_info.name.empty()) { + continue; + } + + MethodData method; + method.name = method_info.name; + + if (method_info.flags & METHOD_FLAG_VIRTUAL) { + method.is_virtual = true; + } + + PropertyInfo return_info = method_info.return_val; + + MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name); + + method.is_vararg = m && m->is_vararg(); + + if (!m && !method.is_virtual) { + TEST_FAIL_COND(!virtual_method_list.find(method_info), + "Missing MethodBind for non-virtual method: '" + exposed_class.name + "." + method.name + "'."); + + // A virtual method without the virtual flag. This is a special case. + + // The method Object.free is registered as a virtual method, but without the virtual flag. + // This is because this method is not supposed to be overridden, but called. + // We assume the return type is void. + method.return_type.name = r_context.names_cache.void_type; + + // Actually, more methods like this may be added in the future, which could return + // something different. Let's put this check to notify us if that ever happens. + String warn_msg = vformat( + "Notification: New unexpected virtual non-overridable method found. " + "We only expected Object.free, but found '%s.%s'.", + exposed_class.name, method.name); + TEST_FAIL_COND_WARN( + (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"), + warn_msg.utf8().get_data()); + + } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + method.return_type.name = return_info.class_name; + method.return_type.is_enum = true; + } else if (return_info.class_name != StringName()) { + method.return_type.name = return_info.class_name; + + bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE && + ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.reference_class); + TEST_COND(bad_reference_hint, String() + "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'." + + " Are you returning a reference type by pointer? Method: '" + + exposed_class.name + "." + method.name + "'."); + } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { + method.return_type.name = return_info.hint_string; + } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { + method.return_type.name = r_context.names_cache.variant_type; + } else if (return_info.type == Variant::NIL) { + method.return_type.name = r_context.names_cache.void_type; + } else { + // NOTE: We don't care about the size and sign of int and float in these tests + method.return_type.name = Variant::get_type_name(return_info.type); + } + + for (int i = 0; i < argc; i++) { + PropertyInfo arg_info = method_info.arguments[i]; + + String orig_arg_name = arg_info.name; + + ArgumentData arg; + arg.name = orig_arg_name; + + if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + arg.type.name = arg_info.class_name; + arg.type.is_enum = true; + } else if (arg_info.class_name != StringName()) { + arg.type.name = arg_info.class_name; + } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { + arg.type.name = arg_info.hint_string; + } else if (arg_info.type == Variant::NIL) { + arg.type.name = r_context.names_cache.variant_type; + } else { + // NOTE: We don't care about the size and sign of int and float in these tests + arg.type.name = Variant::get_type_name(arg_info.type); + } + + if (m && m->has_default_argument(i)) { + arg.has_defval = true; + arg.defval = m->get_default_argument(i); + } + + method.arguments.push_back(arg); + } + + if (method.is_vararg) { + ArgumentData vararg; + vararg.type.name = r_context.names_cache.vararg_stub_type; + vararg.name = "@varargs@"; + method.arguments.push_back(vararg); + } + + TEST_COND(exposed_class.find_property_by_name(method.name), + "Method name conflicts with property: '" + String(class_name) + "." + String(method.name) + "'."); + + // Classes starting with an underscore are ignored unless they're used as a property setter or getter + if (!method.is_virtual && String(method.name)[0] == '_') { + for (const List<PropertyData>::Element *F = exposed_class.properties.front(); F; F = F->next()) { + const PropertyData &prop = F->get(); + + if (prop.setter == method.name || prop.getter == method.name) { + exposed_class.methods.push_back(method); + break; + } + } + } else { + exposed_class.methods.push_back(method); + } + } + + // Add signals + + const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map; + const StringName *k = nullptr; + + while ((k = signal_map.next(k))) { + SignalData signal; + + const MethodInfo &method_info = signal_map.get(*k); + + signal.name = method_info.name; + + int argc = method_info.arguments.size(); + + for (int i = 0; i < argc; i++) { + PropertyInfo arg_info = method_info.arguments[i]; + + String orig_arg_name = arg_info.name; + + ArgumentData arg; + arg.name = orig_arg_name; + + if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + arg.type.name = arg_info.class_name; + arg.type.is_enum = true; + } else if (arg_info.class_name != StringName()) { + arg.type.name = arg_info.class_name; + } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { + arg.type.name = arg_info.hint_string; + } else if (arg_info.type == Variant::NIL) { + arg.type.name = r_context.names_cache.variant_type; + } else { + // NOTE: We don't care about the size and sign of int and float in these tests + arg.type.name = Variant::get_type_name(arg_info.type); + } + + signal.arguments.push_back(arg); + } + + bool method_conflict = exposed_class.find_property_by_name(signal.name); + + // TODO: + // ClassDB allows signal names that conflict with method or property names. + // However registering a signal with a conflicting name is still considered wrong. + // Unfortunately there are some existing cases that are yet to be fixed. + // Until those are fixed we will print a warning instead of failing the test. + String warn_msg = vformat( + "Signal name conflicts with %s: '%s.%s.", + method_conflict ? "method" : "property", class_name, signal.name); + TEST_FAIL_COND_WARN((method_conflict || exposed_class.find_method_by_name(signal.name)), + warn_msg.utf8().get_data()); + + exposed_class.signals_.push_back(signal); + } + + // Add enums and constants + + List<String> constants; + ClassDB::get_integer_constant_list(class_name, &constants, true); + + const HashMap<StringName, List<StringName>> &enum_map = class_info->enum_map; + k = nullptr; + + while ((k = enum_map.next(k))) { + EnumData enum_; + enum_.name = *k; + + const List<StringName> &enum_constants = enum_map.get(*k); + for (const List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) { + const StringName &constant_name = E->get(); + int *value = class_info->constant_map.getptr(constant_name); + TEST_FAIL_COND(!value, "Missing enum constant value: '" + + String(class_name) + "." + String(enum_.name) + "." + String(constant_name) + "'."); + constants.erase(constant_name); + + ConstantData constant; + constant.name = constant_name; + constant.value = *value; + + enum_.constants.push_back(constant); + } + + exposed_class.enums.push_back(enum_); + + r_context.enum_types.push_back(String(class_name) + "." + String(*k)); + } + + for (const List<String>::Element *E = constants.front(); E; E = E->next()) { + const String &constant_name = E->get(); + int *value = class_info->constant_map.getptr(StringName(E->get())); + TEST_FAIL_COND(!value, "Missing enum constant value: '" + String(class_name) + "." + String(constant_name) + "'."); + + ConstantData constant; + constant.name = constant_name; + constant.value = *value; + + exposed_class.constants.push_back(constant); + } + + r_context.exposed_classes.insert(class_name, exposed_class); + class_list.pop_front(); + } +} + +void add_builtin_types(Context &r_context) { + // NOTE: We don't care about the size and sign of int and float in these tests + for (int i = 0; i < Variant::VARIANT_MAX; i++) { + r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i))); + } + + r_context.builtin_types.push_back(_STR(Variant)); + r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type); + r_context.builtin_types.push_back("void"); +} + +void add_global_enums(Context &r_context) { + int global_constants_count = GlobalConstants::get_global_constant_count(); + + if (global_constants_count > 0) { + for (int i = 0; i < global_constants_count; i++) { + StringName enum_name = GlobalConstants::get_global_constant_enum(i); + + if (enum_name != StringName()) { + ConstantData constant; + constant.name = GlobalConstants::get_global_constant_name(i); + constant.value = GlobalConstants::get_global_constant_value(i); + + EnumData enum_; + enum_.name = enum_name; + List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_); + if (enum_match) { + enum_match->get().constants.push_back(constant); + } else { + enum_.constants.push_back(constant); + r_context.global_enums.push_back(enum_); + } + } + } + + for (List<EnumData>::Element *E = r_context.global_enums.front(); E; E = E->next()) { + r_context.enum_types.push_back(E->get().name); + } + } + + // HARDCODED + List<StringName> hardcoded_enums; + hardcoded_enums.push_back("Vector2.Axis"); + hardcoded_enums.push_back("Vector2i.Axis"); + hardcoded_enums.push_back("Vector3.Axis"); + hardcoded_enums.push_back("Vector3i.Axis"); + for (List<StringName>::Element *E = hardcoded_enums.front(); E; E = E->next()) { + // These enums are not generated and must be written manually (e.g.: Vector3.Axis) + // Here, we assume core types do not begin with underscore + r_context.enum_types.push_back(E->get()); + } +} + +TEST_SUITE("[ClassDB]") { + TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") { + Context context; + + add_exposed_classes(context); + add_builtin_types(context); + add_global_enums(context); + + SUBCASE("[ClassDB] Find exposed class") { + const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class); + TEST_FAIL_COND(!object_class, "Object class not found."); + TEST_FAIL_COND(object_class->base != StringName(), + "Object class derives from another class: '" + object_class->base + "'."); + + for (ExposedClasses::Element E = context.exposed_classes.front(); E; E = E.next()) { + validate_class(context, E.value()); + } + } + } +} + +} // namespace TestClassDB + #endif //GODOT_TEST_CLASS_DB_H diff --git a/tests/test_expression.h b/tests/test_expression.h new file mode 100644 index 0000000000..85d37d1460 --- /dev/null +++ b/tests/test_expression.h @@ -0,0 +1,431 @@ +/*************************************************************************/ +/* test_expression.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef TEST_EXPRESSION_H +#define TEST_EXPRESSION_H + +#include "core/math/expression.h" + +#include "tests/test_macros.h" + +namespace TestExpression { + +TEST_CASE("[Expression] Integer arithmetic") { + Expression expression; + + CHECK_MESSAGE( + expression.parse("-123456") == OK, + "Integer identity should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == -123456, + "Integer identity should return the expected result."); + + CHECK_MESSAGE( + expression.parse("2 + 3") == OK, + "Integer addition should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 5, + "Integer addition should return the expected result."); + + CHECK_MESSAGE( + expression.parse("999999999999 + 999999999999") == OK, + "Large integer addition should parse successfully."); + CHECK_MESSAGE( + int64_t(expression.execute()) == 1'999'999'999'998, + "Large integer addition should return the expected result."); + + CHECK_MESSAGE( + expression.parse("25 / 10") == OK, + "Integer / integer division should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 2, + "Integer / integer divsion should return the expected result."); + + CHECK_MESSAGE( + expression.parse("2 * (6 + 14) / 2 - 5") == OK, + "Integer multiplication-addition-subtraction-division should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 15, + "Integer multiplication-addition-subtraction-division should return the expected result."); +} + +TEST_CASE("[Expression] Floating-point arithmetic") { + Expression expression; + + CHECK_MESSAGE( + expression.parse("-123.456") == OK, + "Float identity should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), -123.456), + "Float identity should return the expected result."); + + CHECK_MESSAGE( + expression.parse("2.0 + 3.0") == OK, + "Float addition should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 5), + "Float addition should return the expected result."); + + CHECK_MESSAGE( + expression.parse("3.0 / 10") == OK, + "Float / integer division should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 0.3), + "Float / integer divsion should return the expected result."); + + CHECK_MESSAGE( + expression.parse("3 / 10.0") == OK, + "Basic integer / float division should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 0.3), + "Basic integer / float divsion should return the expected result."); + + CHECK_MESSAGE( + expression.parse("3.0 / 10.0") == OK, + "Float / float division should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 0.3), + "Float / float divsion should return the expected result."); + + CHECK_MESSAGE( + expression.parse("2.5 * (6.0 + 14.25) / 2.0 - 5.12345") == OK, + "Float multiplication-addition-subtraction-division should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 20.18905), + "Float multiplication-addition-subtraction-division should return the expected result."); +} + +TEST_CASE("[Expression] Scientific notation") { + Expression expression; + + CHECK_MESSAGE( + expression.parse("2.e5") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 200'000), + "The expression should return the expected result."); + + // The middle "e" is ignored here. + CHECK_MESSAGE( + expression.parse("2e5") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 25), + "The expression should return the expected result."); + + CHECK_MESSAGE( + expression.parse("2e.5") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 2), + "The expression should return the expected result."); +} + +TEST_CASE("[Expression] Built-in functions") { + Expression expression; + + CHECK_MESSAGE( + expression.parse("sqrt(pow(3, 2) + pow(4, 2))") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 5, + "`sqrt(pow(3, 2) + pow(4, 2))` should return the expected result."); + + CHECK_MESSAGE( + expression.parse("stepify(sin(0.5), 0.01)") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(float(expression.execute()), 0.48), + "`stepify(sin(0.5), 0.01)` should return the expected result."); + + CHECK_MESSAGE( + expression.parse("pow(2.0, -2500)") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + Math::is_zero_approx(float(expression.execute())), + "`pow(2.0, -2500)` should return the expected result (asymptotically zero)."); +} + +TEST_CASE("[Expression] Boolean expressions") { + Expression expression; + + CHECK_MESSAGE( + expression.parse("24 >= 12") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + bool(expression.execute()), + "The boolean expression should evaluate to `true`."); + + CHECK_MESSAGE( + expression.parse("1.0 < 1.25 && 1.25 < 2.0") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + bool(expression.execute()), + "The boolean expression should evaluate to `true`."); + + CHECK_MESSAGE( + expression.parse("!2") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + !bool(expression.execute()), + "The boolean expression should evaluate to `false`."); + + CHECK_MESSAGE( + expression.parse("!!2") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + bool(expression.execute()), + "The boolean expression should evaluate to `true`."); + + CHECK_MESSAGE( + expression.parse("!0") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + bool(expression.execute()), + "The boolean expression should evaluate to `true`."); + + CHECK_MESSAGE( + expression.parse("!!0") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + !bool(expression.execute()), + "The boolean expression should evaluate to `false`."); + + CHECK_MESSAGE( + expression.parse("2 && 5") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + bool(expression.execute()), + "The boolean expression should evaluate to `true`."); + + CHECK_MESSAGE( + expression.parse("0 || 0") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + !bool(expression.execute()), + "The boolean expression should evaluate to `false`."); + + CHECK_MESSAGE( + expression.parse("(2 <= 4) && (2 > 5)") == OK, + "The boolean expression should parse successfully."); + CHECK_MESSAGE( + !bool(expression.execute()), + "The boolean expression should evaluate to `false`."); +} + +TEST_CASE("[Expression] Expressions with variables") { + Expression expression; + + PackedStringArray parameter_names; + parameter_names.push_back("foo"); + parameter_names.push_back("bar"); + CHECK_MESSAGE( + expression.parse("foo + bar + 50", parameter_names) == OK, + "The expression should parse successfully."); + Array values; + values.push_back(60); + values.push_back(20); + CHECK_MESSAGE( + int(expression.execute(values)) == 130, + "The expression should return the expected value."); + + PackedStringArray parameter_names_invalid; + parameter_names_invalid.push_back("foo"); + parameter_names_invalid.push_back("baz"); // Invalid parameter name. + CHECK_MESSAGE( + expression.parse("foo + bar + 50", parameter_names_invalid) == OK, + "The expression should parse successfully."); + Array values_invalid; + values_invalid.push_back(60); + values_invalid.push_back(20); + // Invalid parameters will parse successfully but print an error message when executing. + ERR_PRINT_OFF; + CHECK_MESSAGE( + int(expression.execute(values_invalid)) == 0, + "The expression should return the expected value."); + ERR_PRINT_ON; + + // Mismatched argument count (more values than parameters). + PackedStringArray parameter_names_mismatch; + parameter_names_mismatch.push_back("foo"); + parameter_names_mismatch.push_back("bar"); + CHECK_MESSAGE( + expression.parse("foo + bar + 50", parameter_names_mismatch) == OK, + "The expression should parse successfully."); + Array values_mismatch; + values_mismatch.push_back(60); + values_mismatch.push_back(20); + values_mismatch.push_back(110); + CHECK_MESSAGE( + int(expression.execute(values_mismatch)) == 130, + "The expression should return the expected value."); + + // Mismatched argument count (more parameters than values). + PackedStringArray parameter_names_mismatch2; + parameter_names_mismatch2.push_back("foo"); + parameter_names_mismatch2.push_back("bar"); + parameter_names_mismatch2.push_back("baz"); + CHECK_MESSAGE( + expression.parse("foo + bar + baz + 50", parameter_names_mismatch2) == OK, + "The expression should parse successfully."); + Array values_mismatch2; + values_mismatch2.push_back(60); + values_mismatch2.push_back(20); + // Having more parameters than values will parse successfully but print an + // error message when executing. + ERR_PRINT_OFF; + CHECK_MESSAGE( + int(expression.execute(values_mismatch2)) == 0, + "The expression should return the expected value."); + ERR_PRINT_ON; +} + +TEST_CASE("[Expression] Invalid expressions") { + Expression expression; + + CHECK_MESSAGE( + expression.parse("\\") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("0++") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("()") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("()()") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("() - ()") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("() * 12345") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("() * 12345") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("123'456") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); + + CHECK_MESSAGE( + expression.parse("123\"456") == ERR_INVALID_PARAMETER, + "The expression shouldn't parse successfully."); +} + +TEST_CASE("[Expression] Unusual expressions") { + Expression expression; + + // Redundant parentheses don't cause a parse error as long as they're matched. + CHECK_MESSAGE( + expression.parse("(((((((((((((((666)))))))))))))))") == OK, + "The expression should parse successfully."); + + // Using invalid identifiers doesn't cause a parse error. + ERR_PRINT_OFF; + CHECK_MESSAGE( + expression.parse("hello + hello") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 0, + "The expression should return the expected result."); + ERR_PRINT_ON; + + ERR_PRINT_OFF; + CHECK_MESSAGE( + expression.parse("$1.00 + €5") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 0, + "The expression should return the expected result."); + ERR_PRINT_ON; + + // Commas can't be used as a decimal parameter. + CHECK_MESSAGE( + expression.parse("123,456") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 123, + "The expression should return the expected result."); + + // Spaces can't be used as a separator for large numbers. + CHECK_MESSAGE( + expression.parse("123 456") == OK, + "The expression should parse successfully."); + CHECK_MESSAGE( + int(expression.execute()) == 123, + "The expression should return the expected result."); + + // Division by zero is accepted, even though it prints an error message normally. + CHECK_MESSAGE( + expression.parse("-25.4 / 0") == OK, + "The expression should parse successfully."); + ERR_PRINT_OFF; + CHECK_MESSAGE( + Math::is_zero_approx(float(expression.execute())), + "`-25.4 / 0` should return 0."); + ERR_PRINT_ON; + + CHECK_MESSAGE( + expression.parse("0 / 0") == OK, + "The expression should parse successfully."); + ERR_PRINT_OFF; + CHECK_MESSAGE( + int(expression.execute()) == 0, + "`0 / 0` should return 0."); + ERR_PRINT_ON; + + // The tests below currently crash the engine. + // + //CHECK_MESSAGE( + // expression.parse("(-9223372036854775807 - 1) % -1") == OK, + // "The expression should parse successfully."); + //CHECK_MESSAGE( + // int64_t(expression.execute()) == 0, + // "`(-9223372036854775807 - 1) % -1` should return the expected result."); + // + //CHECK_MESSAGE( + // expression.parse("(-9223372036854775807 - 1) / -1") == OK, + // "The expression should parse successfully."); + //CHECK_MESSAGE( + // int64_t(expression.execute()) == 0, + // "`(-9223372036854775807 - 1) / -1` should return the expected result."); +} + +} // namespace TestExpression + +#endif // TEST_EXPRESSION_H diff --git a/tests/test_gradient.h b/tests/test_gradient.h new file mode 100644 index 0000000000..88fe06b3ec --- /dev/null +++ b/tests/test_gradient.h @@ -0,0 +1,152 @@ +/*************************************************************************/ +/* test_gradient.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef TEST_GRADIENT_H +#define TEST_GRADIENT_H + +#include "core/class_db.h" +#include "core/color.h" +#include "scene/resources/gradient.h" + +#include "thirdparty/doctest/doctest.h" + +namespace TestGradient { + +TEST_CASE("[Gradient] Default gradient") { + // Black-white gradient. + Ref<Gradient> gradient = memnew(Gradient); + + CHECK_MESSAGE( + gradient->get_points_count() == 2, + "Default gradient should contain the expected number of points."); + + CHECK_MESSAGE( + gradient->get_color_at_offset(0.0).is_equal_approx(Color(0, 0, 0)), + "Default gradient should return the expected interpolated value at offset 0.0."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.4).is_equal_approx(Color(0.4, 0.4, 0.4)), + "Default gradient should return the expected interpolated value at offset 0.4."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.8).is_equal_approx(Color(0.8, 0.8, 0.8)), + "Default gradient should return the expected interpolated value at offset 0.8."); + CHECK_MESSAGE( + gradient->get_color_at_offset(1.0).is_equal_approx(Color(1, 1, 1)), + "Default gradient should return the expected interpolated value at offset 1.0."); + + // Out of bounds checks. + CHECK_MESSAGE( + gradient->get_color_at_offset(-1.0).is_equal_approx(Color(0, 0, 0)), + "Default gradient should return the expected interpolated value at offset -1.0."); + CHECK_MESSAGE( + gradient->get_color_at_offset(1234.0).is_equal_approx(Color(1, 1, 1)), + "Default gradient should return the expected interpolated value at offset 1234.0."); +} + +TEST_CASE("[Gradient] Custom gradient (points specified in order)") { + // Red-yellow-green gradient (with overbright green). + Ref<Gradient> gradient = memnew(Gradient); + Vector<Gradient::Point> points; + points.push_back({ 0.0, Color(1, 0, 0) }); + points.push_back({ 0.5, Color(1, 1, 0) }); + points.push_back({ 1.0, Color(0, 2, 0) }); + gradient->set_points(points); + + CHECK_MESSAGE( + gradient->get_points_count() == 3, + "Custom gradient should contain the expected number of points."); + + CHECK_MESSAGE( + gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 0, 0)), + "Custom gradient should return the expected interpolated value at offset 0.0."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.25).is_equal_approx(Color(1, 0.5, 0)), + "Custom gradient should return the expected interpolated value at offset 0.25."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.5).is_equal_approx(Color(1, 1, 0)), + "Custom gradient should return the expected interpolated value at offset 0.5."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.75).is_equal_approx(Color(0.5, 1.5, 0)), + "Custom gradient should return the expected interpolated value at offset 0.75."); + CHECK_MESSAGE( + gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 2, 0)), + "Custom gradient should return the expected interpolated value at offset 1.0."); + + gradient->remove_point(1); + CHECK_MESSAGE( + gradient->get_points_count() == 2, + "Custom gradient should contain the expected number of points after removing one point."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.5).is_equal_approx(Color(0.5, 1, 0)), + "Custom gradient should return the expected interpolated value at offset 0.5 after removing point at index 1."); +} + +TEST_CASE("[Gradient] Custom gradient (points specified out-of-order)") { + // HSL rainbow with points specified out of order. + // These should be sorted automatically when adding points. + Ref<Gradient> gradient = memnew(Gradient); + Vector<Gradient::Point> points; + points.push_back({ 0.2, Color(1, 0, 0) }); + points.push_back({ 0.0, Color(1, 1, 0) }); + points.push_back({ 0.8, Color(0, 1, 0) }); + points.push_back({ 0.4, Color(0, 1, 1) }); + points.push_back({ 1.0, Color(0, 0, 1) }); + points.push_back({ 0.6, Color(1, 0, 1) }); + gradient->set_points(points); + + CHECK_MESSAGE( + gradient->get_points_count() == 6, + "Custom out-of-order gradient should contain the expected number of points."); + + CHECK_MESSAGE( + gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 1, 0)), + "Custom out-of-order gradient should return the expected interpolated value at offset 0.0."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.3).is_equal_approx(Color(0.5, 0.5, 0.5)), + "Custom out-of-order gradient should return the expected interpolated value at offset 0.3."); + CHECK_MESSAGE( + gradient->get_color_at_offset(0.6).is_equal_approx(Color(1, 0, 1)), + "Custom out-of-order gradient should return the expected interpolated value at offset 0.6."); + CHECK_MESSAGE( + gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 0, 1)), + "Custom out-of-order gradient should return the expected interpolated value at offset 1.0."); + + gradient->remove_point(0); + CHECK_MESSAGE( + gradient->get_points_count() == 5, + "Custom out-of-order gradient should contain the expected number of points after removing one point."); + // The color will be clamped to the nearest point (which is at offset 0.2). + CHECK_MESSAGE( + gradient->get_color_at_offset(0.1).is_equal_approx(Color(1, 0, 0)), + "Custom out-of-order gradient should return the expected interpolated value at offset 0.1 after removing point at index 0."); +} + +} // namespace TestGradient + +#endif // TEST_GRADIENT_H diff --git a/tests/test_macros.h b/tests/test_macros.h index e4494ce11a..45ba8581dd 100644 --- a/tests/test_macros.h +++ b/tests/test_macros.h @@ -47,4 +47,61 @@ #define ERR_PRINT_OFF _print_error_enabled = false; #define ERR_PRINT_ON _print_error_enabled = true; +// Stringify all `Variant` compatible types for doctest output by default. +// https://github.com/onqtam/doctest/blob/master/doc/markdown/stringification.md + +#define DOCTEST_STRINGIFY_VARIANT(m_type) \ + template <> \ + struct doctest::StringMaker<m_type> { \ + static doctest::String convert(const m_type &p_val) { \ + const Variant val = p_val; \ + return val.get_construct_string().utf8().get_data(); \ + } \ + }; + +#define DOCTEST_STRINGIFY_VARIANT_POINTER(m_type) \ + template <> \ + struct doctest::StringMaker<m_type> { \ + static doctest::String convert(const m_type *p_val) { \ + const Variant val = p_val; \ + return val.get_construct_string().utf8().get_data(); \ + } \ + }; + +DOCTEST_STRINGIFY_VARIANT(Variant); +DOCTEST_STRINGIFY_VARIANT(::String); // Disambiguate from `doctest::String`. + +DOCTEST_STRINGIFY_VARIANT(Vector2); +DOCTEST_STRINGIFY_VARIANT(Vector2i); +DOCTEST_STRINGIFY_VARIANT(Rect2); +DOCTEST_STRINGIFY_VARIANT(Rect2i); +DOCTEST_STRINGIFY_VARIANT(Vector3); +DOCTEST_STRINGIFY_VARIANT(Vector3i); +DOCTEST_STRINGIFY_VARIANT(Transform2D); +DOCTEST_STRINGIFY_VARIANT(Plane); +DOCTEST_STRINGIFY_VARIANT(Quat); +DOCTEST_STRINGIFY_VARIANT(AABB); +DOCTEST_STRINGIFY_VARIANT(Basis); +DOCTEST_STRINGIFY_VARIANT(Transform); + +DOCTEST_STRINGIFY_VARIANT(::Color); // Disambiguate from `doctest::Color`. +DOCTEST_STRINGIFY_VARIANT(StringName); +DOCTEST_STRINGIFY_VARIANT(NodePath); +DOCTEST_STRINGIFY_VARIANT(RID); +DOCTEST_STRINGIFY_VARIANT_POINTER(Object); +DOCTEST_STRINGIFY_VARIANT(Callable); +DOCTEST_STRINGIFY_VARIANT(Signal); +DOCTEST_STRINGIFY_VARIANT(Dictionary); +DOCTEST_STRINGIFY_VARIANT(Array); + +DOCTEST_STRINGIFY_VARIANT(PackedByteArray); +DOCTEST_STRINGIFY_VARIANT(PackedInt32Array); +DOCTEST_STRINGIFY_VARIANT(PackedInt64Array); +DOCTEST_STRINGIFY_VARIANT(PackedFloat32Array); +DOCTEST_STRINGIFY_VARIANT(PackedFloat64Array); +DOCTEST_STRINGIFY_VARIANT(PackedStringArray); +DOCTEST_STRINGIFY_VARIANT(PackedVector2Array); +DOCTEST_STRINGIFY_VARIANT(PackedVector3Array); +DOCTEST_STRINGIFY_VARIANT(PackedColorArray); + #endif // TEST_MACROS_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 137882841c..43c1ef331f 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -36,7 +36,9 @@ #include "test_basis.h" #include "test_class_db.h" #include "test_color.h" +#include "test_expression.h" #include "test_gdscript.h" +#include "test_gradient.h" #include "test_gui.h" #include "test_math.h" #include "test_oa_hash_map.h" diff --git a/tests/test_ordered_hash_map.cpp b/tests/test_ordered_hash_map.cpp deleted file mode 100644 index d18a3784be..0000000000 --- a/tests/test_ordered_hash_map.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/*************************************************************************/ -/* test_ordered_hash_map.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "test_ordered_hash_map.h" - -#include "core/ordered_hash_map.h" -#include "core/os/os.h" -#include "core/pair.h" -#include "core/vector.h" - -namespace TestOrderedHashMap { - -bool test_insert() { - OrderedHashMap<int, int> map; - OrderedHashMap<int, int>::Element e = map.insert(42, 84); - - return e && e.key() == 42 && e.get() == 84 && e.value() == 84 && map[42] == 84 && map.has(42) && map.find(42); -} - -bool test_insert_overwrite() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(42, 1234); - - return map[42] == 1234; -} - -bool test_erase_via_element() { - OrderedHashMap<int, int> map; - OrderedHashMap<int, int>::Element e = map.insert(42, 84); - - map.erase(e); - return !e && !map.has(42) && !map.find(42); -} - -bool test_erase_via_key() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.erase(42); - return !map.has(42) && !map.find(42); -} - -bool test_size() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(123, 84); - map.insert(123, 84); - map.insert(0, 84); - map.insert(123485, 84); - - return map.size() == 4; -} - -bool test_iteration() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(123, 12385); - map.insert(0, 12934); - map.insert(123485, 1238888); - map.insert(123, 111111); - - Vector<Pair<int, int>> expected; - expected.push_back(Pair<int, int>(42, 84)); - expected.push_back(Pair<int, int>(123, 111111)); - expected.push_back(Pair<int, int>(0, 12934)); - expected.push_back(Pair<int, int>(123485, 1238888)); - - int idx = 0; - for (OrderedHashMap<int, int>::Element E = map.front(); E; E = E.next()) { - if (expected[idx] != Pair<int, int>(E.key(), E.value())) { - return false; - } - ++idx; - } - return true; -} - -bool test_const_iteration(const OrderedHashMap<int, int> &map) { - Vector<Pair<int, int>> expected; - expected.push_back(Pair<int, int>(42, 84)); - expected.push_back(Pair<int, int>(123, 111111)); - expected.push_back(Pair<int, int>(0, 12934)); - expected.push_back(Pair<int, int>(123485, 1238888)); - - int idx = 0; - for (OrderedHashMap<int, int>::ConstElement E = map.front(); E; E = E.next()) { - if (expected[idx] != Pair<int, int>(E.key(), E.value())) { - return false; - } - ++idx; - } - return true; -} - -bool test_const_iteration() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(123, 12385); - map.insert(0, 12934); - map.insert(123485, 1238888); - map.insert(123, 111111); - - return test_const_iteration(map); -} - -typedef bool (*TestFunc)(); - -TestFunc test_funcs[] = { - - test_insert, - test_insert_overwrite, - test_erase_via_element, - test_erase_via_key, - test_size, - test_iteration, - test_const_iteration, - nullptr - -}; - -MainLoop *test() { - int count = 0; - int passed = 0; - - while (true) { - if (!test_funcs[count]) { - break; - } - bool pass = test_funcs[count](); - if (pass) { - passed++; - } - OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED"); - - count++; - } - - OS::get_singleton()->print("\n\n\n"); - OS::get_singleton()->print("*************\n"); - OS::get_singleton()->print("***TOTALS!***\n"); - OS::get_singleton()->print("*************\n"); - - OS::get_singleton()->print("Passed %i of %i tests\n", passed, count); - - return nullptr; -} - -} // namespace TestOrderedHashMap diff --git a/tests/test_ordered_hash_map.h b/tests/test_ordered_hash_map.h index f251da0ba2..3182c391cb 100644 --- a/tests/test_ordered_hash_map.h +++ b/tests/test_ordered_hash_map.h @@ -31,11 +31,109 @@ #ifndef TEST_ORDERED_HASH_MAP_H #define TEST_ORDERED_HASH_MAP_H -#include "core/os/main_loop.h" +#include "core/ordered_hash_map.h" +#include "core/os/os.h" +#include "core/pair.h" +#include "core/vector.h" + +#include "tests/test_macros.h" namespace TestOrderedHashMap { -MainLoop *test(); +TEST_CASE("[OrderedHashMap] Insert element") { + OrderedHashMap<int, int> map; + OrderedHashMap<int, int>::Element e = map.insert(42, 84); + + CHECK(e); + CHECK(e.key() == 42); + CHECK(e.get() == 84); + CHECK(e.value() == 84); + CHECK(map[42] == 84); + CHECK(map.has(42)); + CHECK(map.find(42)); +} + +TEST_CASE("[OrderedHashMap] Overwrite element") { + OrderedHashMap<int, int> map; + map.insert(42, 84); + map.insert(42, 1234); + + CHECK(map[42] == 1234); +} + +TEST_CASE("[OrderedHashMap] Erase via element") { + OrderedHashMap<int, int> map; + OrderedHashMap<int, int>::Element e = map.insert(42, 84); + + map.erase(e); + CHECK(!e); + CHECK(!map.has(42)); + CHECK(!map.find(42)); +} + +TEST_CASE("[OrderedHashMap] Erase via key") { + OrderedHashMap<int, int> map; + map.insert(42, 84); + map.erase(42); + CHECK(!map.has(42)); + CHECK(!map.find(42)); } +TEST_CASE("[OrderedHashMap] Size") { + OrderedHashMap<int, int> map; + map.insert(42, 84); + map.insert(123, 84); + map.insert(123, 84); + map.insert(0, 84); + map.insert(123485, 84); + + CHECK(map.size() == 4); +} + +TEST_CASE("[OrderedHashMap] Iteration") { + OrderedHashMap<int, int> map; + map.insert(42, 84); + map.insert(123, 12385); + map.insert(0, 12934); + map.insert(123485, 1238888); + map.insert(123, 111111); + + Vector<Pair<int, int>> expected; + expected.push_back(Pair<int, int>(42, 84)); + expected.push_back(Pair<int, int>(123, 111111)); + expected.push_back(Pair<int, int>(0, 12934)); + expected.push_back(Pair<int, int>(123485, 1238888)); + + int idx = 0; + for (OrderedHashMap<int, int>::Element E = map.front(); E; E = E.next()) { + CHECK(expected[idx] == Pair<int, int>(E.key(), E.value())); + ++idx; + } +} + +TEST_CASE("[OrderedHashMap] Const iteration") { + OrderedHashMap<int, int> map; + map.insert(42, 84); + map.insert(123, 12385); + map.insert(0, 12934); + map.insert(123485, 1238888); + map.insert(123, 111111); + + const OrderedHashMap<int, int> const_map = map; + + Vector<Pair<int, int>> expected; + expected.push_back(Pair<int, int>(42, 84)); + expected.push_back(Pair<int, int>(123, 111111)); + expected.push_back(Pair<int, int>(0, 12934)); + expected.push_back(Pair<int, int>(123485, 1238888)); + + int idx = 0; + for (OrderedHashMap<int, int>::ConstElement E = const_map.front(); E; E = E.next()) { + CHECK(expected[idx] == Pair<int, int>(E.key(), E.value())); + ++idx; + } +} + +} // namespace TestOrderedHashMap + #endif // TEST_ORDERED_HASH_MAP_H diff --git a/tests/test_validate_testing.h b/tests/test_validate_testing.h index 24acdec96c..b4ea6eb576 100644 --- a/tests/test_validate_testing.h +++ b/tests/test_validate_testing.h @@ -53,6 +53,137 @@ TEST_SUITE("Validate tests") { ERR_PRINT_ON; CHECK_MESSAGE(_print_error_enabled, "Error printing should be re-enabled."); } + TEST_CASE("Stringify Variant types") { + Variant var; + INFO(var); + + String string("Godot is finally here!"); + INFO(string); + + Vector2 vec2(0.5, 1.0); + INFO(vec2); + + Vector2i vec2i(1, 2); + INFO(vec2i); + + Rect2 rect2(0.5, 0.5, 100.5, 100.5); + INFO(rect2); + + Rect2i rect2i(0, 0, 100, 100); + INFO(rect2i); + + Vector3 vec3(0.5, 1.0, 2.0); + INFO(vec3); + + Vector3i vec3i(1, 2, 3); + INFO(vec3i); + + Transform2D trans2d(0.5, Vector2(100, 100)); + INFO(trans2d); + + Plane plane(Vector3(1, 1, 1), 1.0); + INFO(plane); + + Quat quat(Vector3(0.5, 1.0, 2.0)); + INFO(quat); + + AABB aabb(Vector3(), Vector3(100, 100, 100)); + INFO(aabb); + + Basis basis(quat); + INFO(basis); + + Transform trans(basis); + INFO(trans); + + Color color(1, 0.5, 0.2, 0.3); + INFO(color); + + StringName string_name("has_method"); + INFO(string_name); + + NodePath node_path("godot/sprite"); + INFO(node_path); + + INFO(RID()); + + Object *obj = memnew(Object); + INFO(obj); + + Callable callable(obj, "has_method"); + INFO(callable); + + Signal signal(obj, "script_changed"); + INFO(signal); + + memdelete(obj); + + Dictionary dict; + dict["string"] = string; + dict["color"] = color; + INFO(dict); + + Array arr; + arr.push_back(string); + arr.push_back(color); + INFO(arr); + + PackedByteArray byte_arr; + byte_arr.push_back(0); + byte_arr.push_back(1); + byte_arr.push_back(2); + INFO(byte_arr); + + PackedInt32Array int32_arr; + int32_arr.push_back(0); + int32_arr.push_back(1); + int32_arr.push_back(2); + INFO(int32_arr); + + PackedInt64Array int64_arr; + int64_arr.push_back(0); + int64_arr.push_back(1); + int64_arr.push_back(2); + INFO(int64_arr); + + PackedFloat32Array float32_arr; + float32_arr.push_back(0.5); + float32_arr.push_back(1.5); + float32_arr.push_back(2.5); + INFO(float32_arr); + + PackedFloat64Array float64_arr; + float64_arr.push_back(0.5); + float64_arr.push_back(1.5); + float64_arr.push_back(2.5); + INFO(float64_arr); + + PackedStringArray str_arr = string.split(" "); + INFO(str_arr); + + PackedVector2Array vec2_arr; + vec2_arr.push_back(Vector2(0, 0)); + vec2_arr.push_back(Vector2(1, 1)); + vec2_arr.push_back(Vector2(2, 2)); + INFO(vec2_arr); + + PackedVector3Array vec3_arr; + vec3_arr.push_back(Vector3(0, 0, 0)); + vec3_arr.push_back(Vector3(1, 1, 1)); + vec3_arr.push_back(Vector3(2, 2, 2)); + INFO(vec3_arr); + + PackedColorArray color_arr; + color_arr.push_back(Color(0, 0, 0)); + color_arr.push_back(Color(1, 1, 1)); + color_arr.push_back(Color(2, 2, 2)); + INFO(color_arr); + + INFO("doctest insertion operator << " + << var << " " << vec2 << " " << rect2 << " " << color); + + CHECK(true); // So all above prints. + } } #endif // TEST_VALIDATE_TESTING_H |