gdscript_test_runner_suite.h 4.8 KB

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