-
Notifications
You must be signed in to change notification settings - Fork 1
/
Assert.wren
295 lines (261 loc) · 6.42 KB
/
Assert.wren
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
/**
* Minimalist assertion library for unit testing in Wren.
*
* @author Rob Loach (@RobLoach)
* @license MIT
* @website https://github.com/RobLoach/wren-assert
*/
class Assert {
/**
* Assert that the two given values are equal.
*/
static equal(actual, expected) {
equal(actual, expected, "Expected inputs to be equal: %(actual) != %(expected)")
}
/**
* Assert that the two given values are equal, with a given message.
*/
static equal(actual, expected, message) {
if (actual != expected) {
fail(message)
}
}
/**
* Asserts that the two given values are equal.
*
* Alias for Assert.equal(actual, expected).
*/
static [actual, expected] {
equal(actual, expected)
}
/**
* Asserts that the two given values are equal, with a message.
*
* Alias for Assert.equal(actual, expected, message).
*/
static [actual, expected, message] {
equal(actual, expected, message)
}
/**
* Assert that the two given values are not equal.
*/
static notEqual(actual, expected) {
notEqual(actual, expected, "Expected inputs to be equal: %(actual) == %(expected)")
}
/**
* Assert that the two given values are not equal, with a given message.
*/
static notEqual(actual, expected, message) {
if (actual == expected) {
fail(message)
}
}
/**
* Assert that the given value is truthy.
*/
static ok(value) {
ok(value, "Expected input to be ok: %(value)")
}
/**
* Assert that the given value is truthy, with a given message.
*/
static ok(value, message) {
if (value) {
// Nothing.
} else {
fail(message)
}
}
/**
* Assert that the given value is falsey.
*/
static notOk(value) {
notOk(value, "Expected input to be ok: %(value)")
}
/**
* Assert that the given value is falsey, with a given message.
*/
static notOk(value, message) {
if (value) {
fail(message)
}
}
/**
* Asserts that the given value is ok.
*
* Alias for Assert.ok(value).
*/
static [value] {
ok(value)
}
/**
* Assert that the given function aborts.
*/
static aborts(fn) {
aborts(fn, "Expected function to abort")
}
/**
* Assert that the given function aborts, with a given message.
*/
static aborts(fn, message) {
var fiber = Fiber.new(fn)
fiber.try()
if (!fiber.error) {
fail(message)
}
}
/**
* Assert that the given function does not abort.
*/
static doesNotAbort(fn) {
var fiber = Fiber.new(fn)
fiber.try()
if (fiber.error) {
fail("Expected the function to not abort: " + fiber.error)
}
}
/**
* Assert that the given function does not abort, with a given message.
*/
static doesNotAbort(fn, message) {
var fiber = Fiber.new(fn)
fiber.try()
if (fiber.error) {
fail(message)
}
}
/**
* Assert that the given object matches the given type.
*/
static typeOf(object, type) {
typeOf(object, type, "Expected %(object) to be of type %(type)")
}
/**
* Assert that the given object matches the given type, with a message.
*/
static typeOf(object, type, message) {
if (!(object is type)) {
fail(message)
}
}
/**
* Assert that the given object doesn't match the given type.
*/
static notTypeOf(object, type) {
notTypeOf(object, type, "Expected %(object) to not be of type %(type)")
}
/**
* Assert that the given object doesn't match the given type, with a message.
*/
static notTypeOf(object, type, message) {
if (object is type) {
fail(message)
}
}
/**
* Assert that a list matches a given count.
*/
static countOf(list, count) {
countOf(list, count, "Expected the list to have a count of %(count)")
}
/**
* Assert that a list matches a given count, with a message.
*/
static countOf(list, count, message) {
equal(list.count, count, message)
}
/**
* Asserts that the two given objects, and their children, are equal, with a message.
*/
static deepEqual(actual, expected, message) {
if (actual is String && expected is String) {
equal(actual, expected, message)
} else if (actual is Sequence && expected is Sequence) {
if (actual.count != expected.count) {
fail(message)
}
for (i in 0..actual.count - 1) {
deepEqual(actual[i], expected[i], message)
}
} else {
equal(actual, expected, message)
}
}
/**
* Asserts that the two given objects, and their children, are equal.
*/
static deepEqual(actual, expected) {
deepEqual(actual, expected, "Expected the given sequences are deepEqual")
}
/**
* Asserts that the given value is not null.
*/
static exists(value, message) {
notEqual(value, null, message)
}
/**
* Asserts that the given value is not null.
*/
static exists(value) {
notEqual(value, null, "Expected the given value to not be null")
}
/**
* Asserts that the given value is null.
*/
static notExists(value, message) {
equal(value, null, message)
}
/**
* Asserts that the given value is null.
*/
static notExists(value) {
notExists(value, "Expected the given value to be null")
}
/**
* Asserts that the given sequence haystack contains the given needle value.
*/
static contains(haystack, needle, message) {
if (haystack is Sequence) {
ok(haystack.contains(needle), message)
} else {
equal(haystack, needle, message)
}
}
static contains(haystack, needle) {
contains(haystack, needle, "The given haystack sequence does not contain the value %(needle)")
}
/**
* Throws an abort on the current fiber with the given message.
*/
static fail(message) {
if (!__disabled) {
Fiber.abort(message)
}
}
/**
* Throws an abort on the current fiber.
*/
static fail() {
fail("There was a failed assertion")
}
/**
* Throws an abort on the current fiber, using a built message.
*/
static fail(actual, expected, operator) {
fail("Expected that %(actual) %(operator) %(expected)")
}
/**
* Throws an assert on the given fiber, assuming an equal operator.
*/
static fail(actual, expected) {
fail(actual, expected, "==")
}
/**
* Gets whether or not assertions should be skipped.
*/
static disabled { __disabled }
/**
* Set `Assert.disabled = true` to have assertions skip avoid throwing Fiber.aborts().
*/
static disabled=(value) { __disabled = value}
}