-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.um
108 lines (87 loc) · 2.78 KB
/
test.um
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
104
105
106
107
108
// testing the test framework //
// so meta //
import ( "std.um"; "toast.um" )
fn alwaysPass(T: ^toast::Context) {
if !T.assert.isTrue(1 + 2 == 3, "math doesn't work") { return }
}
fn alwaysFail(T: ^toast::Context) {
if !T.assert.isTrue(false, "logic works") { return }
}
fn failOnNonExistentFile(T: ^toast::Context) {
f, e := std::fopen("AJHSKJHDSKhdksjhdsjhsd.txt", "rb")
msg := "couldn't open non-existent file (as it's supposed to do)"
if !T.assert.isOk(e, msg) { return }
}
fn passOnExistentFile(T: ^toast::Context) {
f, e := std::fopen("toast.um", "rb")
if !T.assert.isOk(e, "this should NOT HAPPEN") { return }
std::fclose(f)
}
fn assertEqual(T: ^toast::Context, a, b: int): bool {
T.startCustom()
return T.endCustom(T.assert.isTrue(
a == b, "expected a and b to be equal"
))
}
fn customAssert(T: ^toast::Context) {
if !assertEqual(T, 1, 1) { return }
}
fn assertSquare(T: ^toast::Context, n, s: int): bool {
T.startCustom()
if n * n != s {
return T.endCustom(T.fail(sprintf("%v squared does not equal %v", n, s)))
}
return T.endCustom(true)
}
fn customLogic(T: ^toast::Context) {
if !assertSquare(T, 3, 9 ) { return }
if !assertSquare(T, 3, 10) { return }
}
fn assertTypes(T: ^toast::Context) {
if !T.assert.sameType(42, 90) { return }
if !T.assert.sameType("hello", "world") { return }
}
fn testToast(T: ^toast::Context) {
expected := map[str]bool{
"always pass": true,
"always fail": false,
"fake file": false,
"real file": true,
"custom assertion": true,
"custom logic": false,
"assert types": true
}
U := toast::newContext()
U.printWithColor = true
U.registerTests({
{ name: "always pass", func: alwaysPass },
{ name: "always fail", func: alwaysFail },
{ name: "fake file", func: failOnNonExistentFile },
{ name: "real file", func: passOnExistentFile },
{ name: "custom assertion", func: customAssert },
{ name: "custom logic", func: customLogic },
{ name: "assert types", func: assertTypes }
})
U.run(false)
fprintf(std::stderr(), "\n")
for _, r in U.tests {
n := r.name
state := (r.result.code == 0)
if state != expected[n] {
s := sprintf(
"expected '%s' to %s, but it %s",
n, expected[n] ? "pass" : "fail",
state ? "passed": "failed"
)
T.fail(s)
return
}
}
}
fn main() {
T := toast::newContext()
T.compactOutput = false
T.printWithColor = true
T.registerTest("toast itself", testToast)
T.run()
}