-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.opa
121 lines (109 loc) · 3.77 KB
/
test.opa
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
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* A slightly better unittest system
*
* Example usage:
* function run_tests() {
* t = Test.begin()
* t = Test.pass(t, "test 1")
* t = Test.fail(t, "test 2", "this always fails")
* t = Test.expect_equals(t, "test 3", 2*8, 16)
* t = Test.expect_equals(t, "test 4", 2*8, 20)
* t = Test.expect_none(t, "test 5", {none}, "something is weird!")
* t = Test.expect_none(t, "test 6", {some: 1}, "something is weird!")
* t = Test.expect_true(t, "test 7", true, "hmmm...")
* t = Test.expect_true(t, "test 8", false, "hmmm...")
* t = Test.expect_false(t, "test 9", true, "hmmm...")
* t = Test.expect_false(t, "test 10", false, "hmmm...")
* Test.end(t)
* }
* run_tests()
*
*
*
* This file is part of RiskyBird
*
* RiskyBird is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RiskyBird is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with RiskyBird. If not, see <http://www.gnu.org/licenses/>.
*
* @author Julien Verlaguet
* @author Alok Menghrajani
*/
import stdlib.system
type Test.result = {
list(string) passed,
list(string) failed,
}
module Test {
function Test.result begin() {
prerrln("Starting unittests")
{passed: [], failed: []}
}
function void end(Test.result res) {
prerrln("--------------------------------------------------------------------------------")
prerrln("Passed: {List.length(res.passed)}")
prerrln("Failed: {List.length(res.failed)}")
prerrln("")
if (List.is_empty(res.failed)) {
System.exit(0)
} else {
prerrln("List of failed tests:")
_ = List.map(function(s){prerrln("* {_fill(s, 78)}")}, res.failed)
System.exit(List.length(res.failed));
}
}
function pass(Test.result res, string test_name) {
prerrln("{_fill(test_name, 40)}: PASS")
{res with passed:List.cons(test_name, res.passed)}
}
function fail(Test.result res, string test_name, string extra_message) {
right = ": FAIL {extra_message}"
right = _fill(right, 40)
prerrln("{_fill(test_name, 40)}{right}")
{res with failed:List.cons(test_name, res.failed)}
}
function expect_equals(Test.result res, string test_name, 'a test_value, 'a expected_value) {
if (test_value == expected_value) {
pass(res, test_name)
} else {
extra_message = "(expecting {expected_value}, got {test_value})"
fail(res, test_name, extra_message)
}
}
function expect_none(Test.result res, string test_name, option('a) test_value, string extra_message) {
expect_true(res, test_name, Option.is_none(test_value), extra_message)
}
function expect_some(Test.result res, string test_name, option('a) test_value, string extra_message) {
expect_true(res, test_name, Option.is_some(test_value), extra_message)
}
function expect_true(Test.result res, string test_name, bool test_value, string extra_message) {
if (test_value) {
pass(res, test_name)
} else {
fail(res, test_name, extra_message)
}
}
function expect_false(Test.result res, string test_name, bool test_value, string extra_message) {
if (test_value) {
fail(res, test_name, extra_message)
} else {
pass(res, test_name)
}
}
function string _fill(string s, int l) {
if (String.length(s) > l) {
"{String.substring(0, l-3, s)}..."
} else {
String.pad_right(" ", l, s)
}
}
}