56 lines
1.9 KiB
GDScript
56 lines
1.9 KiB
GDScript
class_name TestCase
|
||
extends RefCounted
|
||
|
||
## 测试断言基类。由 tests/run_tests.gd 注入 _runner 后调用。
|
||
|
||
var _runner: Object = null
|
||
|
||
|
||
func ok(condition: bool, label: String, detail: String = "") -> void:
|
||
_runner.report_assert(condition, label, detail)
|
||
|
||
|
||
func is_true(condition: bool, label: String, detail: String = "") -> void:
|
||
ok(condition, label, detail if detail != "" else "期望 true,实际 false")
|
||
|
||
|
||
func is_false(condition: bool, label: String, detail: String = "") -> void:
|
||
ok(not condition, label, detail if detail != "" else "期望 false,实际 true")
|
||
|
||
|
||
func eq(actual: Variant, expected: Variant, label: String) -> void:
|
||
ok(actual == expected, label, "期望 %s,实际 %s" % [str(expected), str(actual)])
|
||
|
||
|
||
func ne(actual: Variant, unexpected: Variant, label: String) -> void:
|
||
ok(actual != unexpected, label, "不应等于 %s" % str(unexpected))
|
||
|
||
|
||
func almost(actual: float, expected: float, tolerance: float, label: String) -> void:
|
||
ok(
|
||
absf(actual - expected) <= tolerance,
|
||
label,
|
||
"期望 %.6f ± %.6f,实际 %.6f(差 %.6f)" % [expected, tolerance, actual, absf(actual - expected)]
|
||
)
|
||
|
||
|
||
func between(actual: float, low: float, high: float, label: String) -> void:
|
||
ok(actual >= low and actual <= high, label, "期望落在 [%.4f, %.4f],实际 %.4f" % [low, high, actual])
|
||
|
||
|
||
func is_null(value: Variant, label: String) -> void:
|
||
ok(value == null, label, "期望 null,实际 %s" % str(value))
|
||
|
||
|
||
func not_null(value: Variant, label: String) -> void:
|
||
ok(value != null, label, "期望非 null")
|
||
|
||
|
||
## 跳过当前测试(依赖外部服务不可达等)。不计入通过,也不计入失败。
|
||
func skip(label: String, reason: String) -> void:
|
||
_runner.report_skip(label, reason)
|
||
|
||
|
||
func contains(haystack: String, needle: String, label: String) -> void:
|
||
ok(haystack.contains(needle), label, "「%s」中未包含「%s」" % [haystack, needle])
|