63 lines
1.8 KiB
GDScript
63 lines
1.8 KiB
GDScript
class_name TestBase
|
|
extends SceneTree
|
|
## Shared boilerplate for the headless suites: the --sample guard, loading the
|
|
## app, frame waiting, the check helpers and the PASS/FAIL summary the runner
|
|
## (tools/run_tests.sh) looks for. A suite extends this and calls
|
|
## `if not await start_main(): return` first when it needs the app.
|
|
|
|
var _failures := 0
|
|
var main: Main
|
|
|
|
|
|
## Returns false (after quitting with 2) unless the suite was 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
|
|
print("Run with -- --sample so the test can't touch saved data.")
|
|
quit(2)
|
|
return false
|
|
|
|
|
|
## Hook: suites that need a customised Main override this.
|
|
func _create_main() -> Main:
|
|
return load("res://ui/main.tscn").instantiate()
|
|
|
|
|
|
## Guard, then loads the app under root and waits for it. Returns false (after
|
|
## quitting) if the guard fails or the app would save.
|
|
func start_main() -> bool:
|
|
if not require_sample():
|
|
return false
|
|
main = _create_main()
|
|
root.add_child(main)
|
|
await _frames(5)
|
|
if main._saving_enabled:
|
|
print("Saving is enabled; refusing to run.")
|
|
quit(2)
|
|
return false
|
|
return true
|
|
|
|
|
|
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)])
|
|
|
|
|
|
## Prints the summary line the runner greps for, then quits.
|
|
func finish() -> void:
|
|
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
|
quit(1 if _failures else 0)
|