-
Notifications
You must be signed in to change notification settings - Fork 4
/
Asserter.winxed
334 lines (291 loc) · 11.6 KB
/
Asserter.winxed
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
namespace Rosella { namespace Test
{
/* Test Asserter to provide an OO-like interface to the functions defined
in the Assert namespace
*/
// TODO: Add the ability for the user to provide a custom subclass for
// asserter instead.
class Asserter
{
// Routine to get a default message, if none is provided
function default_message(string msg, int has_msg, string def)
{
if (has_msg)
return msg;
return def;
}
// Unconditional fail. Throws a Rosella.Test.Failure
function fail(string why,
int is_internal [optional,named], int has_is_i [opt_flag],
var exception [optional,named], int has_ex [opt_flag]
)
{
using Rosella.build;
if (!has_is_i)
is_internal = 0;
if (!has_ex)
exception = null;
var ex = build(class Rosella.Test.Failure, why, exception, is_internal);
ex.throw();
}
/* Assertion Functions
Each of these functions asserts some condition. If the condition
holds, nothing happens. If the condition fails, we call fail().
*/
function block(string message, var block)
{
if (!block())
self.fail(message);
}
function block_false(string message, var block)
{
if (block())
self.fail(message);
}
function can(var obj, string method,
string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "item failed 'can'");
int i_can = 0;
${ can i_can, obj, method };
if (!i_can)
self.fail(message);
}
function can_not(var obj, string method,
string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "item failed 'can_not'");
int i_can = 0;
${ can i_can, obj, method };
if (i_can)
self.fail(message);
}
function defined(var obj,
string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "item not defined");
int is_defined = 0;
${ defined is_defined, obj };
if (!is_defined)
self.fail(message);
}
function not_defined(var obj,
string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "item is defined");
int is_defined = 0;
${ defined is_defined, obj };
if (is_defined)
self.fail(message);
}
function does(var obj, var role,
string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "item failed 'does'");
int obj_does = 0;
${ does obj_does, obj, role };
if (!obj_does)
self.fail(message);
}
function does_not(obj, role, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "item failed 'does'");
int obj_does = 0;
${ does obj_does, obj, role };
if (obj_does)
self.fail(message);
}
function equal(var o1, var o2,
string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "objects not equal");
int equal = 0;
${ iseq equal, o1, o2 };
if (!equal)
self.fail(message);
}
function not_equal(var o1, var o2,
string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "objects equal");
int equal = 0;
${ iseq equal, o1, o2 };
if (equal)
self.fail(message);
}
function instance_of(var obj, var type, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "object is not instance_of");
using Rosella.get_type_class;
var class_obj = get_type_class(type);
int it_isa = 0;
${ isa it_isa, obj, class_obj };
if (!it_isa)
self.fail(message);
}
function not_instance_of(var obj, var type, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "object is instance_of");
using Rosella.get_type_class;
var class_obj = get_type_class(type);
int it_isa = 0;
${ isa it_isa, obj, class_obj };
if (it_isa)
self.fail(message);
}
//function isa($obj, $class, string message [optional], int has_msg [opt_flag] = "object not isa") {
// self.fail($message) unless pir::isa__iPP($obj, P6metaclass.get_parrotclass($class));
//}
//function not_isa($obj, $class, string message [optional], int has_msg [opt_flag] = "object isa") {
// self.fail($message) if pir::isa__iPP($obj, P6metaclass.get_parrotclass($class));
//}
// TODO: Port matcher library
//function match($obj, $matcher, string message [optional], int has_msg [opt_flag] = "match failed") {
// unless $matcher.matches($obj) {
// my $explain = $matcher.describe_self("\nExpected ")
// ~ $matcher.describe_failure("\nbut ", $obj);
// self.fail($message ~ $explain);
// }
//}
//function not_match($obj, $matcher, string message [optional], int has_msg [opt_flag]) {
// if $matcher.matches($obj) {
// self.fail($message);
// }
//}
function is_null(var obj, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "object is not null");
if (obj != null)
self.fail(message);
}
function not_null(var obj, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "object is null");
if (obj == null)
self.fail(message);
}
function same(var o1, var o2, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "objects not same");
if (o1 !== o2)
self.fail(message);
}
function not_same(var o1, var o2, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "objects are same");
if (o1 === o2)
self.fail(message);
}
function throws(var block, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "does not throw");
try {
block();
self.fail(message);
} catch() {}
}
function throws_nothing(var block, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "block throws");
try {
block();
} catch(e) {
self.fail(message, e:[named("exception")]);
}
}
function is_true(int bool, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "is not true");
if (!bool)
self.fail(message);
}
function is_false(int bool, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "is not false");
if (bool)
self.fail(message);
}
//function within_delta($o1, $o2, $delta, string message [optional], int has_msg [opt_flag] "difference not within delta") {
// my $difference = $o1 - $o2;
// $difference = - $difference if $difference < 0;
// self.fail($message) unless $difference < $delta;
//}
//#~ like(obj, regex, message)
//#~ not_like
//#function want_fail($message, &block) {
//# throws(Exception::RosellaFailure, $message, &block);
//#}
//#function want_pass($message, &block) {
//# throws_nothing($message, &block);
//#}
// meta-test. Runs the block as a test, and expects a failure
function expect_fail(var block, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "test did not fail");
var exception = null;
try {
block();
} catch (e) {
exception = e;
}
if (exception == null)
self.fail(message);
var payload = exception.payload;
if (payload == null)
self.fail(message, exception:[named("exception")]);
using Rosella.isa_type;
int isa_failure = isa_type(class Rosella.Test.Failure, payload);
if (!isa_failure)
self.fail(message, exception:[named("exception")]);
}
// meta-test. Runs the block as a test, and expects a pass
function expect_pass(var block, string message [optional], int has_msg [opt_flag])
{
message = self.default_message(message, has_msg, "test did not pass");
var exception = null;
try {
block();
} catch (e) {
exception = e;
}
if (exception != null)
self.fail(message, exception:[named("exception")]);
}
// Run the test, show that the output (and maybe the error output) matches
// the values expected
function output_is(var block, string output,
string message [optional], int has_msg [opt_flag],
string erroutput [named,optional], int has_erroutput [opt_flag])
{
using Rosella.IO.swap_handles;
message = self.default_message(message, has_msg, "output did not match");
var stdout = new "StringHandle";
stdout.open("Rosella.Test.Assert.stdout", "rw");
var stderr = new "StringHandle";
stderr.open("Rosella.Test.Assert.stderr", "rw");
var save_handles = swap_handles({"stdout": stdout, "stderr": stderr});
var exception = null;
try {
block();
} catch (e) {
exception = e;
}
swap_handles(save_handles);
if (exception != null) {
say(exception);
self.fail("Block threw exception before output received",
exception:[named("exception")]
);
}
// TODO: We should trim leading/trailing whitespace
string out_str = stdout.readall();
if (out_str != output)
self.fail(sprintf("%s\nExpected: '%s'\nReceived:'%s'", [message, output, out_str]));
if (!has_erroutput)
return;
string errout_str = stderr.readall();
if (errout_str != erroutput)
self.fail(message);
}
}
}}