/
Asserter.winxed
353 lines (316 loc) · 12.2 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* Test Asserter class
This class provides test assertions. Assertions check various things
during the test run and signal failure if the checks are not met.
By default an instance of Asserter is located in the
"assert"/"$!assert" attribute of the TestCase object during the test
run.
You can provide a custom subclass, a custom instance, or a replacement
object for this functionality by passing an object to
Rosella.Test.test with the name "asserter".
*/
class Rosella.Test.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]
)
{
if (!has_is_i)
is_internal = 0;
if (!has_ex)
exception = null;
Rosella.Test.internal_fail(why, is_internal, exception);
}
/* Assertion Functions
Each of these functions asserts some condition. If the condition
holds, nothing happens. If the condition fails, we call fail().
*/
// Execute a block of code that is expected to return a boolean. Expect true
function block(string message, var block)
{
if (!block())
self.fail(message);
}
// Execute a block of code that is expected to return a boolean. Expect false
function block_false(string message, var block)
{
if (block())
self.fail(message);
}
// Determine if the object "can" do the given method. Expect that it can
function can(var obj, string method,
string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, "item failed 'can'");
if (!can(obj, method))
self.fail(message);
}
// Determine if the object "can" do the given method. Expect that it can not
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'");
if (can(obj, method))
self.fail(message);
}
// Determine if the object is defined. Expect that it is
function defined(var obj,
string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, "item not defined");
if (!defined(obj))
self.fail(message);
}
// Determine if the object is defined. Expect that it is not
function not_defined(var obj,
string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, "item is defined");
if (defined(obj))
self.fail(message);
}
// Determine if the object implements the given role. Expect that it does
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;
if (role instanceof 'String')
obj_does = does(obj, string(role));
else
obj_does = does(obj, role);
if (!obj_does)
self.fail(message);
}
// Determine if the object implements the given role. Expect that it doesn't
function does_not(var obj, var role, string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, "item failed 'does'");
if (does(obj, role))
self.fail(message);
}
// Determine if the two objects are equal. Expect that they are
function equal(var o1, var o2,
string message [optional], int has_msg [opt_flag])
{
if (!has_msg) {
try {
string s1 = string(o1);
string s2 = string(o2);
message = sprintf("objects not equal '%s' != '%s'", [s1, s2]);
has_msg = 1;
} catch() { }
}
message = self.__default_message(message, has_msg, "objects not equal");
int equal = 0;
${ iseq equal, o1, o2 };
if (!equal)
self.fail(message);
}
// Determine if the two objects are equal. Expect that they are not
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);
}
// Determine if the object is an instance of the type. Expect so.
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");
var class_obj = Rosella.get_type_class(type);
if (!isa(obj, class_obj))
self.fail(message);
}
// Determine if the object is an instance of the type. Expect not.
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");
var class_obj = Rosella.get_type_class(type);
if (isa(obj, class_obj))
self.fail(message);
}
// Determine if the object is null. Expect so.
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);
}
// Determine if the object is null. Expect not.
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);
}
// Determine if the two objects are the same instance. Expect so.
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);
}
// Determine if the two objects are the same instance. Expect not.
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);
}
// Expect that the given block, when executed, throws an exception.
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() {}
}
// Expect that the given block, when executed, does not throw any exceptions
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")]);
}
}
// Check that the value is true
function is_true(var bool, string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, "is not true");
int false_flag;
${ isfalse false_flag, bool };
if (false_flag)
self.fail(message);
}
// Check that the value is false
function is_false(var bool, string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, "is not false");
int true_flag;
${ istrue true_flag, bool };
if (true_flag)
self.fail(message);
}
// 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. Notice that the strings are matched exactly,
// including leading and trailing whitespace
function output_is(var block, var output,
string message [optional], int has_msg [opt_flag],
string erroutput [named,optional], int has_erroutput [opt_flag])
{
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 = Rosella.IO.swap_handles({"stdout": stdout, "stderr": stderr});
var exception = null;
try {
block();
} catch (e) {
exception = e;
}
Rosella.IO.swap_handles(save_handles);
if (exception != null) {
say(exception);
self.fail("Block threw exception before output received",
exception:[named("exception")]
);
}
string output_s;
if (does(output, "array"))
output_s = join("\n", output);
else
output_s = string(output);
string out_str = stdout.readall();
if (out_str != output_s)
self.fail(sprintf("%s\nExpected: '%s'\nReceived: '%s'", [message, output_s, out_str]));
if (!has_erroutput)
return;
string errout_str = stderr.readall();
if (errout_str != erroutput)
self.fail(message);
}
// Check that the hash contains a value at the given key
function exists_keyed_str(var h, string k,
string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, sprintf("key '%s' does not exist", [k]));
if (!(exists h[k]))
self.fail(message);
}
// Check that the hash does not contain a value at the given key
function not_exists_keyed_str(var h, string k,
string message [optional], int has_msg [opt_flag])
{
message = self.__default_message(message, has_msg, sprintf("key '%s' does exist", [k]));
if (exists h[k])
self.fail(message);
}
function is_match(var a, var b, string message [optional], int has_msg [opt_flag])
{
var matcher = Rosella.Test.matcher_factory().create(a, b);
var result = matcher.expect_match(a, b);
if (!result.success()) {
message = self.__default_message(message, has_msg, result.message());
self.fail(message);
}
}
function is_not_match(var a, var b, string message [optional], int has_msg [opt_flag])
{
var matcher = Rosella.Test.matcher_factory().create(a, b);
var result = matcher.expect_no_match(a, b);
if (!result.success()) {
message = self.__default_message(message, has_msg, result.message());
self.fail(message);
}
}
}