aboutsummaryrefslogtreecommitdiff
path: root/core/testing/testing.odin
blob: 0ceb58e33d82ad739a548101223d1a94b54da18d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package testing

import "core:fmt"
import "core:io"
import "core:time"
import "core:intrinsics"

// IMPORTANT NOTE: Compiler requires this layout
Test_Signature :: proc(^T)

// IMPORTANT NOTE: Compiler requires this layout
Internal_Test :: struct {
	pkg:  string,
	name: string,
	p:    Test_Signature,
}


Internal_Cleanup :: struct {
	procedure: proc(rawptr),
	user_data: rawptr,
}

T :: struct {
	error_count: int,

	w: io.Writer,

	cleanups: [dynamic]Internal_Cleanup,

	_fail_now: proc() -> !,
}


error :: proc(t: ^T, args: ..any, loc := #caller_location) {
	fmt.wprintf(t.w, "%v: ", loc)
	fmt.wprintln(t.w, ..args)
	t.error_count += 1
}

errorf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
	fmt.wprintf(t.w, "%v: ", loc)
	fmt.wprintf(t.w, format, ..args)
	fmt.wprintln(t.w)
	t.error_count += 1
}

fail :: proc(t: ^T, loc := #caller_location) {
	error(t, "FAIL", loc=loc)
	t.error_count += 1
}

fail_now :: proc(t: ^T, msg := "", loc := #caller_location) {
	if msg != "" {
		error(t, "FAIL:", msg, loc=loc)
	} else {
		error(t, "FAIL", loc=loc)
	}
	t.error_count += 1
	if t._fail_now != nil {
		t._fail_now()
	}
}

failed :: proc(t: ^T) -> bool {
	return t.error_count != 0
}

log :: proc(t: ^T, args: ..any, loc := #caller_location) {
	fmt.wprintln(t.w, ..args)
}

logf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
	fmt.wprintf(t.w, format, ..args)
	fmt.wprintln(t.w)
}


// cleanup registers a procedure and user_data, which will be called when the test, and all its subtests, complete
// cleanup proceduers will be called in LIFO (last added, first called) order.
cleanup :: proc(t: ^T, procedure: proc(rawptr), user_data: rawptr) {
	append(&t.cleanups, Internal_Cleanup{procedure, user_data})
}

expect :: proc(t: ^T, ok: bool, msg: string = "", loc := #caller_location) -> bool {
	if !ok {
		error(t, msg, loc=loc)
	}
	return ok
}
expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
	ok := value == expected
	if !ok {
		errorf(t, "expected %v, got %v", expected, value, loc=loc)
	}
	return ok
}



set_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
	_fail_timeout(t, duration, loc)
}