2026-11-10 19:53:07 -06:00
|
|
|
class_name TestBase
|
2026-11-25 15:36:55 -06:00
|
|
|
extends Node
|
|
|
|
|
## Shared boilerplate for the headless suites: the --sample guard, frame
|
|
|
|
|
## waiting and the check helpers. tests/run_suites.gd runs several suites in one
|
|
|
|
|
## Godot process: it adds the suite to the tree, calls run() and waits for
|
|
|
|
|
## finish(). Suites that need the app extend AppTestBase.
|
|
|
|
|
## Never mention Main or other UI classes here: every suite would then compile
|
|
|
|
|
## the whole app (about 1 s each).
|
2026-11-10 19:53:07 -06:00
|
|
|
|
2026-11-25 15:36:55 -06:00
|
|
|
var failures := 0
|
|
|
|
|
var done := false
|
2026-11-10 19:53:07 -06:00
|
|
|
|
2026-11-25 15:36:55 -06:00
|
|
|
# The tree's own names, so suites read like a SceneTree script.
|
|
|
|
|
var root: Window:
|
|
|
|
|
get: return get_tree().root
|
|
|
|
|
var process_frame: Signal:
|
|
|
|
|
get: return get_tree().process_frame
|
2026-11-10 19:53:07 -06:00
|
|
|
|
2026-11-25 15:36:55 -06:00
|
|
|
|
|
|
|
|
## The suite's checks; overridden by each suite, ends with finish().
|
|
|
|
|
func run() -> void:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## False (and the suite fails) unless started with `-- --sample`, so a test
|
|
|
|
|
## can't touch saved data.
|
2026-11-10 19:53:07 -06:00
|
|
|
func require_sample() -> bool:
|
|
|
|
|
if OS.get_cmdline_user_args().has("--sample"):
|
|
|
|
|
return true
|
2026-11-25 15:36:55 -06:00
|
|
|
abort("Run with -- --sample so the test can't touch saved data.")
|
2026-11-10 19:53:07 -06:00
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
2026-11-25 15:36:55 -06:00
|
|
|
func create_timer(secs: float) -> SceneTreeTimer:
|
|
|
|
|
return get_tree().create_timer(secs)
|
2026-11-10 19:53:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _frames(n: int = 2) -> void:
|
|
|
|
|
for i in n:
|
|
|
|
|
await process_frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _check(ok: bool, what: String) -> void:
|
|
|
|
|
if not ok:
|
2026-11-25 15:36:55 -06:00
|
|
|
failures += 1
|
2026-11-10 19:53:07 -06:00
|
|
|
print("FAIL ", what)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _eq(got: Variant, want: Variant, what: String) -> void:
|
|
|
|
|
var same: bool = typeof(got) == typeof(want) and got == want
|
|
|
|
|
if not same and got is Array and want is Array:
|
|
|
|
|
same = str(got) == str(want)
|
|
|
|
|
_check(same, "%s\n got: %s\n want: %s" % [what, str(got), str(want)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func finish() -> void:
|
2026-11-25 15:36:55 -06:00
|
|
|
done = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Fails and ends the suite before its checks.
|
|
|
|
|
func abort(why: String) -> void:
|
|
|
|
|
_check(false, why)
|
|
|
|
|
finish()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Puts back what the suite changed for the next one in this process.
|
|
|
|
|
func clean_up() -> void:
|
|
|
|
|
pass
|
2026-11-14 19:19:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Deletes a folder and everything in it (for temporary test folders).
|
|
|
|
|
func _wipe(path: String) -> void:
|
|
|
|
|
var abs := ProjectSettings.globalize_path(path)
|
|
|
|
|
if not DirAccess.dir_exists_absolute(abs):
|
|
|
|
|
return
|
|
|
|
|
for sub in DirAccess.get_directories_at(abs):
|
|
|
|
|
_wipe(path.path_join(sub))
|
|
|
|
|
for f in DirAccess.get_files_at(abs):
|
|
|
|
|
DirAccess.remove_absolute(abs.path_join(f))
|
|
|
|
|
DirAccess.remove_absolute(abs)
|