aboutsummaryrefslogtreecommitdiff
path: root/core/testing/runner.odin
blob: e3286988c924f6c0201597303ca8e357a41c1569 (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
//+private
package testing

import "core:io"
import "core:os"
import "core:slice"

reset_t :: proc(t: ^T) {
	clear(&t.cleanups);
	t.error_count = 0;
}
end_t :: proc(t: ^T) {
	for i := len(t.cleanups)-1; i >= 0; i -= 1 {
		c := t.cleanups[i];
		c.procedure(c.user_data);
	}
}

runner :: proc(internal_tests: []Internal_Test) -> bool {
	stream := os.stream_from_handle(os.stdout);
	w, _ := io.to_writer(stream);

	t := &T{};
	t.w = w;
	reserve(&t.cleanups, 1024);
	defer delete(t.cleanups);

	total_success_count := 0;
	total_test_count := len(internal_tests);

	slice.sort_by(internal_tests, proc(a, b: Internal_Test) -> bool {
		if a.pkg < b.pkg {
			return true;
		}
		return a.name < b.name;
	});

	prev_pkg := "";

	for it in internal_tests {
		if it.p == nil {
			total_test_count -= 1;
			continue;
		}

		free_all(context.temp_allocator);
		reset_t(t);
		defer end_t(t);

		if prev_pkg != it.pkg {
			prev_pkg = it.pkg;
			logf(t, "[Package: %s]", it.pkg);
		}

		logf(t, "[Test: %s]", it.name);

		run_internal_test(t, it);

		if failed(t) {
			logf(t, "[%s : FAILURE]", it.name);
		} else {
			logf(t, "[%s : SUCCESS]", it.name);
			total_success_count += 1;
		}
	}
	logf(t, "----------------------------------------");
	if total_test_count == 0 {
		log(t, "NO TESTS RAN");
	} else {
		logf(t, "%d/%d SUCCESSFUL", total_success_count, total_test_count);
	}
	return total_success_count == total_test_count;
}