Path: blob/master/modules/gdscript/tests/gdscript_test_runner_suite.h
20997 views
/**************************************************************************/1/* gdscript_test_runner_suite.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "gdscript_test_runner.h"3334#include "modules/gdscript/gdscript_cache.h"35#include "tests/test_macros.h"36#include "tests/test_utils.h"3738namespace GDScriptTests {3940class TestGDScriptCacheAccessor {41public:42static bool has_shallow(String p_path) {43return GDScriptCache::singleton->shallow_gdscript_cache.has(p_path);44}4546static bool has_full(String p_path) {47return GDScriptCache::singleton->full_gdscript_cache.has(p_path);48}49};5051// TODO: Handle some cases failing on release builds. See: https://github.com/godotengine/godot/pull/8845252#ifdef TOOLS_ENABLED53TEST_SUITE("[Modules][GDScript]") {54TEST_CASE("Script compilation and runtime") {55bool print_filenames = OS::get_singleton()->get_cmdline_args().find("--print-filenames") != nullptr;56bool use_binary_tokens = OS::get_singleton()->get_cmdline_args().find("--use-binary-tokens") != nullptr;57GDScriptTestRunner runner("modules/gdscript/tests/scripts", true, print_filenames, use_binary_tokens);58int fail_count = runner.run_tests();59INFO("Make sure `*.out` files have expected results.");60REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");61}62}63#endif // TOOLS_ENABLED6465TEST_CASE("[Modules][GDScript] Load source code dynamically and run it") {66GDScriptLanguage::get_singleton()->init();67Ref<GDScript> gdscript = memnew(GDScript);68gdscript->set_source_code(R"(69extends RefCounted7071func _init():72set_meta("result", 42)73)");74// A spurious `Condition "err" is true` message is printed (despite parsing being successful and returning `OK`).75// Silence it.76ERR_PRINT_OFF;77const Error error = gdscript->reload();78ERR_PRINT_ON;79CHECK_MESSAGE(error == OK, "The script should parse successfully.");8081// Run the script by assigning it to a reference-counted object.82Ref<RefCounted> ref_counted = memnew(RefCounted);83ref_counted->set_script(gdscript);84CHECK_MESSAGE(int(ref_counted->get_meta("result")) == 42, "The script should assign object metadata successfully.");85}8687TEST_CASE("[Modules][GDScript] Loading keeps ResourceCache and GDScriptCache in sync") {88const String path = TestUtils::get_temp_path("gdscript_load_test.gd");8990{91Ref<FileAccess> fa = FileAccess::open(path, FileAccess::ModeFlags::WRITE);92fa->store_string("extends Node\n");93fa->close();94}9596CHECK(!ResourceCache::has(path));97CHECK(!TestGDScriptCacheAccessor::has_shallow(path));98CHECK(!TestGDScriptCacheAccessor::has_full(path));99100Ref<GDScript> loaded = ResourceLoader::load(path);101102CHECK(ResourceCache::has(path));103CHECK(!TestGDScriptCacheAccessor::has_shallow(path));104CHECK(TestGDScriptCacheAccessor::has_full(path));105}106107TEST_CASE("[Modules][GDScript] Validate built-in API") {108GDScriptLanguage *lang = GDScriptLanguage::get_singleton();109110// Validate methods.111List<MethodInfo> builtin_methods;112lang->get_public_functions(&builtin_methods);113114SUBCASE("[Modules][GDScript] Validate built-in methods") {115for (const MethodInfo &mi : builtin_methods) {116for (int64_t i = 0; i < mi.arguments.size(); ++i) {117TEST_COND((mi.arguments[i].name.is_empty() || mi.arguments[i].name.begins_with("_unnamed_arg")),118vformat("Unnamed argument in position %d of built-in method '%s'.", i, mi.name));119}120}121}122123// Validate annotations.124List<MethodInfo> builtin_annotations;125lang->get_public_annotations(&builtin_annotations);126127SUBCASE("[Modules][GDScript] Validate built-in annotations") {128for (const MethodInfo &ai : builtin_annotations) {129for (int64_t i = 0; i < ai.arguments.size(); ++i) {130TEST_COND((ai.arguments[i].name.is_empty() || ai.arguments[i].name.begins_with("_unnamed_arg")),131vformat("Unnamed argument in position %d of built-in annotation '%s'.", i, ai.name));132}133}134}135}136137} // namespace GDScriptTests138139140