gdscript_test_runner_suite.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**************************************************************************/
  2. /* gdscript_test_runner_suite.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef GDSCRIPT_TEST_RUNNER_SUITE_H
  31. #define GDSCRIPT_TEST_RUNNER_SUITE_H
  32. #include "gdscript_test_runner.h"
  33. #include "tests/test_macros.h"
  34. namespace GDScriptTests {
  35. // TODO: Handle some cases failing on release builds. See: https://github.com/godotengine/godot/pull/88452
  36. #ifdef TOOLS_ENABLED
  37. TEST_SUITE("[Modules][GDScript]") {
  38. // GDScript 2.0 is still under heavy construction.
  39. // Allow the tests to fail, but do not ignore errors during development.
  40. // Update the scripts and expected output as needed.
  41. TEST_CASE("Script compilation and runtime") {
  42. bool print_filenames = OS::get_singleton()->get_cmdline_args().find("--print-filenames") != nullptr;
  43. GDScriptTestRunner runner("modules/gdscript/tests/scripts", true, print_filenames);
  44. int fail_count = runner.run_tests();
  45. INFO("Make sure `*.out` files have expected results.");
  46. REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");
  47. }
  48. }
  49. TEST_CASE("[Modules][GDScript] Load source code dynamically and run it") {
  50. Ref<GDScript> gdscript = memnew(GDScript);
  51. gdscript->set_source_code(R"(
  52. extends RefCounted
  53. func _init():
  54. set_meta("result", 42)
  55. )");
  56. // A spurious `Condition "err" is true` message is printed (despite parsing being successful and returning `OK`).
  57. // Silence it.
  58. ERR_PRINT_OFF;
  59. const Error error = gdscript->reload();
  60. ERR_PRINT_ON;
  61. CHECK_MESSAGE(error == OK, "The script should parse successfully.");
  62. // Run the script by assigning it to a reference-counted object.
  63. Ref<RefCounted> ref_counted = memnew(RefCounted);
  64. ref_counted->set_script(gdscript);
  65. CHECK_MESSAGE(int(ref_counted->get_meta("result")) == 42, "The script should assign object metadata successfully.");
  66. }
  67. #endif // TOOLS_ENABLED
  68. TEST_CASE("[Modules][GDScript] Validate built-in API") {
  69. GDScriptLanguage *lang = GDScriptLanguage::get_singleton();
  70. // Validate methods.
  71. List<MethodInfo> builtin_methods;
  72. lang->get_public_functions(&builtin_methods);
  73. SUBCASE("[Modules][GDScript] Validate built-in methods") {
  74. for (const MethodInfo &mi : builtin_methods) {
  75. for (int j = 0; j < mi.arguments.size(); j++) {
  76. PropertyInfo arg = mi.arguments[j];
  77. TEST_COND((arg.name.is_empty() || arg.name.begins_with("_unnamed_arg")),
  78. vformat("Unnamed argument in position %d of built-in method '%s'.", j, mi.name));
  79. }
  80. }
  81. }
  82. // Validate annotations.
  83. List<MethodInfo> builtin_annotations;
  84. lang->get_public_annotations(&builtin_annotations);
  85. SUBCASE("[Modules][GDScript] Validate built-in annotations") {
  86. for (const MethodInfo &ai : builtin_annotations) {
  87. for (int j = 0; j < ai.arguments.size(); j++) {
  88. PropertyInfo arg = ai.arguments[j];
  89. TEST_COND((arg.name.is_empty() || arg.name.begins_with("_unnamed_arg")),
  90. vformat("Unnamed argument in position %d of built-in annotation '%s'.", j, ai.name));
  91. }
  92. }
  93. }
  94. }
  95. } // namespace GDScriptTests
  96. #endif // GDSCRIPT_TEST_RUNNER_SUITE_H