Files
claudeandClaude Opus 5.5 dee417df3d F4: Headless test harness
Add an in-repo test runner that discovers test/**/test_*.gd and runs under --headless.
It uses Godot's Logger API, so engine and script errors during a test fail that test.
make test runs the suite, and make test-selfcheck runs deliberately failing fixtures
to prove each kind of failure is detected.

Closes #4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 17:12:59 -05:00

108 lines
3.4 KiB
GDScript

## Base class for tests. Files named test_*.gd extend this and define test_* methods.
## Test methods may await. Any engine or script error raised during a test fails it,
## unless the test declared that error with expect_error().
class_name TestCase
extends RefCounted
var _failures: Array[String] = []
var _expected_errors: Array[String] = []
## Runs before each test. Override as needed; may await.
func before_each() -> void:
pass
## Runs after each test, even if the test failed. Override as needed; may await.
func after_each() -> void:
pass
## Records a failure. The test keeps running, so one run reports every failure.
func fail(message: String) -> void:
_failures.append(message)
## Declares that an engine error containing `fragment` is expected in this test.
## The test fails if the error does not happen.
func expect_error(fragment: String) -> void:
_expected_errors.append(fragment)
func assert_true(condition: bool, message: String = "") -> void:
if not condition:
fail(_describe("expected true", message))
func assert_false(condition: bool, message: String = "") -> void:
if condition:
fail(_describe("expected false", message))
func assert_eq(actual: Variant, expected: Variant, message: String = "") -> void:
if not _same(actual, expected):
fail(_describe("expected %s, got %s" % [_show(expected), _show(actual)], message))
func assert_ne(actual: Variant, unexpected: Variant, message: String = "") -> void:
if _same(actual, unexpected):
fail(_describe("expected a value other than %s" % _show(unexpected), message))
func assert_near(actual: float, expected: float, tolerance: float, message: String = "") -> void:
if absf(actual - expected) > tolerance:
var detail: String = "expected %f within %f, got %f" % [expected, tolerance, actual]
fail(_describe(detail, message))
func assert_null(value: Variant, message: String = "") -> void:
if value != null:
fail(_describe("expected null, got %s" % _show(value), message))
func assert_not_null(value: Variant, message: String = "") -> void:
if value == null:
fail(_describe("expected a value, got null", message))
## Clears per-test state. Called by the runner.
func reset_for_test() -> void:
_failures = []
_expected_errors = []
## Returns this test's failures, including unexpected and missing engine errors.
func finish_test(engine_errors: Array[String]) -> Array[String]:
var results: Array[String] = _failures.duplicate()
var expected: Array[String] = _expected_errors.duplicate()
for error: String in engine_errors:
var matched: int = -1
for index: int in expected.size():
if error.contains(expected[index]):
matched = index
break
if matched >= 0:
expected.remove_at(matched)
else:
results.append("engine error: %s" % error)
for missing: String in expected:
results.append("expected an engine error containing '%s', but none occurred" % missing)
return results
func _describe(detail: String, message: String) -> String:
return detail if message.is_empty() else "%s: %s" % [message, detail]
## Exact comparison that never treats values of different types as equal,
## except int and float, which compare by value.
func _same(a: Variant, b: Variant) -> bool:
var numeric: Array[int] = [TYPE_INT, TYPE_FLOAT]
if typeof(a) != typeof(b) and not (typeof(a) in numeric and typeof(b) in numeric):
return false
return a == b
func _show(value: Variant) -> String:
return "'%s'" % value if value is String else str(value)