-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.rs
206 lines (165 loc) · 4.15 KB
/
tester.rs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// rust testing micro-framework
// licensed: public domain or cc0
#[macro_escape];
pub use std::uint;
pub use std::io;
pub fn report_failure(expected: ~str, actual: ~str) {
io::print(fmt!("\n\x1b[31;1mFail\x1b[39;0m - %s vs %s", actual, expected));
}
trait TestInput {
fn compare(&self, b: &Self) -> bool;
fn compare_near(&self, b: &Self, tolerance: Self) -> bool;
fn fail(&self, b: Self);
}
macro_rules! impl_test_input(
($t:ty) => {
impl TestInput for $t {
pub fn compare(&self, b: &$t) -> bool {
*self == *b
}
pub fn compare_near(&self, b: &$t, _tolerance: $t) -> bool {
*self == *b
}
pub fn fail(&self, b: $t) {
report_failure(self.to_str(), b.to_str());
}
}
}
)
macro_rules! impl_test_float(
($t:ty) => {
impl TestInput for $t {
pub fn compare(&self, b: &$t) -> bool {
*self == *b
}
pub fn compare_near(&self, b: &$t, tolerance: $t) -> bool {
(*self > b - tolerance) && (*self < b + tolerance)
}
pub fn fail(&self, b: $t) {
report_failure(self.to_str(), b.to_str());
}
}
}
)
macro_rules! impl_test_char(
($t:ty) => {
impl TestInput for $t {
pub fn compare(&self, b: &$t) -> bool {
*self == *b
}
pub fn compare_near(&self, b: &$t, _tolerance: $t) -> bool {
*self == *b
}
pub fn fail(&self, b: $t) {
report_failure(fmt!("%c", *self), fmt!("%c", b))
}
}
}
)
impl_test_input!(~str)
impl_test_input!(u8)
impl_test_input!(u16)
impl_test_input!(u32)
impl_test_input!(u64)
impl_test_input!(uint)
impl_test_input!(i8)
impl_test_input!(i16)
impl_test_input!(i32)
impl_test_input!(i64)
impl_test_input!(int)
impl_test_input!(bool)
impl_test_char!(char)
impl_test_float!(float)
impl_test_float!(f32)
impl_test_float!(f64)
pub fn perform_test<T:TestInput>(a:T, b:T, negate: bool) -> bool {
if (a.compare(&b) ^ negate) {
true
}
else {
a.fail(b);
false
}
}
pub fn perform_near<T:TestInput>(a:T, b:T, tolerance:T, negate:bool) -> bool {
if (a.compare_near(&b, tolerance) ^ negate) {
true
}
else {
a.fail(b);
false
}
}
macro_rules! describe(
($prompt:expr, $func:expr) => (
fn main() {
let module_name = $prompt;
let mut _current_test = "<invalid>";
let mut _indent = 0;
let mut _tests:uint = 0;
let mut _fails:uint = 0;
let mut _successes:uint = 0;
io::println(module_name);
$func;
let assertions = _successes + _fails;
io::println(fmt!("\n%u tests %u assertions %u failures", _tests, assertions, _fails));
}
)
)
macro_rules! test(
($prompt:expr, $func:expr) => ({
_current_test = $prompt;
_indent += 1;
do uint::range_step(0, _indent, 1) |_| {
io::print(" ");
true
};
io::println(_current_test);
$func;
_indent -= 1;
})
)
macro_rules! must(
($a:expr eq $b:expr) => (
if (perform_test($a, $b, false)) { _successes += 1; } else { _failure = true; _fails += 1; }
);
($a:expr near $b:expr) => (
if (perform_near($a, $b, 0.00001, false)) { _successes += 1; } else { _failure = true; _fails += 1; }
);
($a:expr near $b:expr within $t:expr) => (
if (perform_near($a, $b, $t, false)) { _successes += 1; } else { _failure = true; _fails += 1; }
);
)
macro_rules! wont(
($a:expr eq $b:expr) => (
if (perform_test($a, $b, true)) { _successes += 1; } else { _failure = true; _fails += 1; }
);
($a:expr near $b:expr) => (
if (perform_near($a, $b, 0.00001, true)) { _successes += 1; } else { _failure = true; _fails += 1; }
);
($a:expr near $b:expr within $t:expr) => (
if (perform_near($a, $b, $t, true)) { _successes += 1; } else { _failure = true; _fails += 1; }
);
)
macro_rules! should(
($prompt:expr, $func:expr) => ({
let mut _failure = false;
_tests += 1;
_indent += 1;
do uint::range_step(0, _indent, 1) |_| {
io::print(" ");
true
};
io::print("should ");
io::print($prompt);
$func;
if (!_failure) {
io::print(" - ");
io::println("\x1b[32;1mPass\x1b[39;0m");
}
else {
io::println("");
}
_indent -= 1;
})
)