| 1 |
@abstract class_name BaseTest |
| 2 |
|
| 3 |
var _test_started := 0 |
| 4 |
var _test_completed := 0 |
| 5 |
var _test_assert_passes := 0 |
| 6 |
var _test_assert_failures := 0 |
| 7 |
|
| 8 |
@abstract func run_tests() |
| 9 |
|
| 10 |
func __exec_test(test_func: Callable): |
| 11 |
_test_started += 1 |
| 12 |
var ret = test_func.call() |
| 13 |
if ret == true: |
| 14 |
_test_completed += 1 |
| 15 |
|
| 16 |
func __reset_tests(): |
| 17 |
_test_started = 0 |
| 18 |
_test_completed = 0 |
| 19 |
_test_assert_passes = 0 |
| 20 |
_test_assert_failures = 0 |
| 21 |
|
| 22 |
func __get_stack_frame(): |
| 23 |
for s in get_stack(): |
| 24 |
if not s.function.begins_with('__') and s.function != "assert_equal": |
| 25 |
return s |
| 26 |
return null |
| 27 |
|
| 28 |
func __assert_pass(): |
| 29 |
_test_assert_passes += 1 |
| 30 |
pass |
| 31 |
|
| 32 |
func __assert_fail(): |
| 33 |
_test_assert_failures += 1 |
| 34 |
var s = __get_stack_frame() |
| 35 |
if s != null: |
| 36 |
print_rich ("[color=red] == FAILURE: In function %s() from '%s' on line %s[/color]" % [s.function, s.source, s.line]) |
| 37 |
else: |
| 38 |
print_rich ("[color=red] == FAILURE (run with --debug to get more information!) ==[/color]") |
| 39 |
|
| 40 |
func assert_equal(actual, expected): |
| 41 |
if actual == expected: |
| 42 |
__assert_pass() |
| 43 |
else: |
| 44 |
__assert_fail() |
| 45 |
print (" |-> Expected '%s' but got '%s'" % [expected, actual]) |
| 46 |
|
| 47 |
func assert_true(value): |
| 48 |
if value: |
| 49 |
__assert_pass() |
| 50 |
else: |
| 51 |
__assert_fail() |
| 52 |
print (" |-> Expected '%s' to be truthy" % value) |