TableTower/tests/testbase.gd

81 lines
2.2 KiB
GDScript3
Raw Normal View History

class_name TestBase
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).
var failures := 0
var done := false
# 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
## 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.
func require_sample() -> bool:
if OS.get_cmdline_user_args().has("--sample"):
return true
abort("Run with -- --sample so the test can't touch saved data.")
return false
func create_timer(secs: float) -> SceneTreeTimer:
return get_tree().create_timer(secs)
func _frames(n: int = 2) -> void:
for i in n:
await process_frame
func _check(ok: bool, what: String) -> void:
if not ok:
failures += 1
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:
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
## 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)