aboutsummaryrefslogtreecommitdiff
path: root/core/testing/testing.odin
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-03-14 18:01:31 +0000
committergingerBill <bill@gingerbill.org>2021-03-14 18:01:31 +0000
commit2aa588209e784274136b516224372fdd677d3e8f (patch)
treed7fb6dd1051e34cd039cd49cc6895e4e58391afc /core/testing/testing.odin
parent10f91a0d3f64902687683ac53dd286b25d3f7d5e (diff)
`odin test` to work with the new `core:testing` package
Diffstat (limited to 'core/testing/testing.odin')
-rw-r--r--core/testing/testing.odin68
1 files changed, 68 insertions, 0 deletions
diff --git a/core/testing/testing.odin b/core/testing/testing.odin
new file mode 100644
index 000000000..d9d4a53a3
--- /dev/null
+++ b/core/testing/testing.odin
@@ -0,0 +1,68 @@
+package testing
+
+import "core:fmt"
+import "core:io"
+
+Test_Signature :: proc(^T);
+
+Internal_Test :: struct {
+ 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,
+}
+
+
+error :: proc(t: ^T, args: ..any, loc := #caller_location) {
+ log(t=t, args=args, loc=loc);
+ t.error_count += 1;
+}
+
+errorf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
+ logf(t=t, format=format, args=args, loc=loc);
+ t.error_count += 1;
+}
+
+fail :: proc(t: ^T) {
+ error(t, "FAIL");
+ t.error_count += 1;
+}
+
+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=t, args={msg}, loc=loc);
+ }
+ return ok;
+}