gdscript_test_runner_suite.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. TEST_CASE("Script compilation and runtime") {
  39. bool print_filenames = OS::get_singleton()->get_cmdline_args().find("--print-filenames") != nullptr;
  40. bool use_binary_tokens = OS::get_singleton()->get_cmdline_args().find("--use-binary-tokens") != nullptr;
  41. GDScriptTestRunner runner("modules/gdscript/tests/scripts", true, print_filenames, use_binary_tokens);
  42. int fail_count = runner.run_tests();
  43. INFO("Make sure `*.out` files have expected results.");
  44. REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");
  45. }
  46. }
  47. TEST_CASE("[Modules][GDScript] Load source code dynamically and run it") {
  48. Ref<GDScript> gdscript = memnew(GDScript);
  49. gdscript->set_source_code(R"(
  50. extends RefCounted
  51. func _init():
  52. set_meta("result", 42)
  53. )");
  54. // A spurious `Condition "err" is true` message is printed (despite parsing being successful and returning `OK`).
  55. // Silence it.
  56. ERR_PRINT_OFF;
  57. const Error error = gdscript->reload();
  58. ERR_PRINT_ON;
  59. CHECK_MESSAGE(error == OK, "The script should parse successfully.");
  60. // Run the script by assigning it to a reference-counted object.
  61. Ref<RefCounted> ref_counted = memnew(RefCounted);
  62. ref_counted->set_script(gdscript);
  63. CHECK_MESSAGE(int(ref_counted->get_meta("result")) == 42, "The script should assign object metadata successfully.");
  64. }
  65. #endif // TOOLS_ENABLED
  66. TEST_CASE("[Modules][GDScript] Validate built-in API") {
  67. GDScriptLanguage *lang = GDScriptLanguage::get_singleton();
  68. // Validate methods.
  69. List<MethodInfo> builtin_methods;
  70. lang->get_public_functions(&builtin_methods);
  71. SUBCASE("[Modules][GDScript] Validate built-in methods") {
  72. for (const MethodInfo &mi : builtin_methods) {
  73. int i = 0;
  74. for (List<PropertyInfo>::ConstIterator itr = mi.arguments.begin(); itr != mi.arguments.end(); ++itr, ++i) {
  75. TEST_COND((itr->name.is_empty() || itr->name.begins_with("_unnamed_arg")),
  76. vformat("Unnamed argument in position %d of built-in method '%s'.", i, mi.name));
  77. }
  78. }
  79. }
  80. // Validate annotations.
  81. List<MethodInfo> builtin_annotations;
  82. lang->get_public_annotations(&builtin_annotations);
  83. SUBCASE("[Modules][GDScript] Validate built-in annotations") {
  84. for (const MethodInfo &ai : builtin_annotations) {
  85. int i = 0;
  86. for (List<PropertyInfo>::ConstIterator itr = ai.arguments.begin(); itr != ai.arguments.end(); ++itr, ++i) {
  87. TEST_COND((itr->name.is_empty() || itr->name.begins_with("_unnamed_arg")),
  88. vformat("Unnamed argument in position %d of built-in annotation '%s'.", i, ai.name));
  89. }
  90. }
  91. }
  92. }
  93. } // namespace GDScriptTests
  94. #endif // GDSCRIPT_TEST_RUNNER_SUITE_H