OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #library('matcherTest'); | |
6 #import('../../../pkg/unittest/unittest.dart'); | |
7 #source('test_utils.dart'); | |
8 | |
9 doesNotThrow() {} | |
10 doesThrow() { throw 'X'; } | |
11 | |
12 class PrefixMatcher extends BaseMatcher { | |
13 final String _prefix; | |
14 const PrefixMatcher(this._prefix); | |
15 bool matches(item, MatchState matchState) { | |
16 return item is String && | |
17 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | |
18 } | |
19 | |
20 Description describe(Description description) => | |
21 description.add('a string starting with '). | |
22 addDescriptionOf(collapseWhitespace(_prefix)). | |
23 add(' ignoring whitespace'); | |
24 } | |
25 | |
26 void main() { | |
27 | |
28 initUtils(); | |
29 | |
30 // Core matchers | |
31 | |
32 group('Core matchers', () { | |
33 | |
34 test('isTrue', () { | |
35 shouldPass(true, isTrue); | |
36 shouldFail(false, isTrue, "Expected: true but: was <false>."); | |
37 }); | |
38 | |
39 test('isFalse', () { | |
40 shouldPass(false, isFalse); | |
41 shouldFail(true, isFalse, "Expected: false but: was <true>."); | |
42 }); | |
43 | |
44 test('isNull', () { | |
45 shouldPass(null, isNull); | |
46 shouldFail(false, isNull, "Expected: null but: was <false>."); | |
47 }); | |
48 | |
49 test('isNotNull', () { | |
50 shouldPass(false, isNotNull); | |
51 shouldFail(null, isNotNull, "Expected: not null but: was <null>."); | |
52 }); | |
53 | |
54 test('same', () { | |
55 var a = new Map(); | |
56 var b = new Map(); | |
57 shouldPass(a, same(a)); | |
58 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>."); | |
59 }); | |
60 | |
61 test('equals', () { | |
62 var a = new Map(); | |
63 var b = new Map(); | |
64 shouldPass(a, equals(a)); | |
65 shouldPass(a, equals(b)); | |
66 }); | |
67 | |
68 test('anything', () { | |
69 var a = new Map(); | |
70 shouldPass(0, anything); | |
71 shouldPass(null, anything); | |
72 shouldPass(a, anything); | |
73 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>."); | |
74 }); | |
75 | |
76 test('throws', () { | |
77 shouldFail(doesNotThrow, throws, | |
78 "Expected: throws an exception but: no exception."); | |
79 shouldPass(doesThrow, throws); | |
80 }); | |
81 | |
82 test('throwsA', () { | |
83 shouldPass(doesThrow, throwsA(equals('X'))); | |
84 shouldFail(doesThrow, throwsA(equals('Y')), | |
85 "Expected: throws an exception which matches 'Y' " | |
86 "but: exception 'X' does not match 'Y'."); | |
87 }); | |
88 | |
89 test('throwsFormatException', () { | |
90 shouldPass(() { throw new FormatException(''); }, | |
91 throwsFormatException); | |
92 shouldFail(() { throw new Exception(); }, | |
93 throwsFormatException, | |
94 "Expected: throws an exception which matches FormatException " | |
95 "but: exception <Exception> does not match FormatException."); | |
96 }); | |
97 | |
98 test('throwsIllegalArgumentException', () { | |
99 shouldPass(() { throw new IllegalArgumentException(''); }, | |
100 throwsIllegalArgumentException); | |
101 shouldFail(() { throw new Exception(); }, | |
102 throwsIllegalArgumentException, | |
103 "Expected: throws an exception which matches IllegalArgumentException " | |
104 "but: exception <Exception> does not match " | |
105 "IllegalArgumentException."); | |
106 }); | |
107 | |
108 test('throwsIllegalJSRegExpException', () { | |
109 shouldPass(() { throw new IllegalJSRegExpException('',''); }, | |
110 throwsIllegalJSRegExpException); | |
111 shouldFail(() { throw new Exception(); }, | |
112 throwsIllegalJSRegExpException, | |
113 "Expected: throws an exception which matches IllegalJSRegExpException " | |
114 "but: exception <Exception> does not match " | |
115 "IllegalJSRegExpException."); | |
116 }); | |
117 | |
118 test('throwsIndexOutOfRangeException', () { | |
119 shouldPass(() { throw new IndexOutOfRangeException(0); }, | |
120 throwsIndexOutOfRangeException); | |
121 shouldFail(() { throw new Exception(); }, | |
122 throwsIndexOutOfRangeException, | |
123 "Expected: throws an exception which matches IndexOutOfRangeException " | |
124 "but: exception <Exception> does not match " | |
125 "IndexOutOfRangeException."); | |
126 }); | |
127 | |
128 test('throwsNoSuchMethodException', () { | |
129 shouldPass(() { throw new NoSuchMethodException(null, '', null); }, | |
130 throwsNoSuchMethodException); | |
131 shouldFail(() { throw new Exception(); }, | |
132 throwsNoSuchMethodException, | |
133 "Expected: throws an exception which matches NoSuchMethodException " | |
134 "but: exception <Exception> does not match " | |
135 "NoSuchMethodException."); | |
136 }); | |
137 | |
138 test('throwsNotImplementedException', () { | |
139 shouldPass(() { throw new NotImplementedException(''); }, | |
140 throwsNotImplementedException); | |
141 shouldFail(() { throw new Exception(); }, | |
142 throwsNotImplementedException, | |
143 "Expected: throws an exception which matches NotImplementedException " | |
144 "but: exception <Exception> does not match " | |
145 "NotImplementedException."); | |
146 }); | |
147 | |
148 test('throwsNullPointerException', () { | |
149 shouldPass(() { throw new NullPointerException(''); }, | |
150 throwsNullPointerException); | |
151 shouldFail(() { throw new Exception(); }, | |
152 throwsNullPointerException, | |
153 "Expected: throws an exception which matches NullPointerException " | |
154 "but: exception <Exception> does not match " | |
155 "NullPointerException."); | |
156 }); | |
157 | |
158 test('throwsUnsupportedOperationException', () { | |
159 shouldPass(() { throw new UnsupportedOperationException(''); }, | |
160 throwsUnsupportedOperationException); | |
161 shouldFail(() { throw new Exception(); }, | |
162 throwsUnsupportedOperationException, | |
163 "Expected: throws an exception which matches " | |
164 "UnsupportedOperationException " | |
165 "but: exception <Exception> does not match " | |
166 "UnsupportedOperationException."); | |
167 }); | |
168 | |
169 test('returnsNormally', () { | |
170 shouldPass(doesNotThrow, returnsNormally); | |
171 shouldFail(doesThrow, returnsNormally, | |
172 "Expected: return normally but: threw 'X'."); | |
173 }); | |
174 | |
175 test('hasLength', () { | |
176 var a = new Map(); | |
177 var b = new List(); | |
178 shouldPass(a, hasLength(0)); | |
179 shouldPass(b, hasLength(0)); | |
180 shouldPass('a', hasLength(1)); | |
181 shouldFail(0, hasLength(0), new PrefixMatcher( | |
182 "Expected: an object with length of <0> " | |
183 "but: was <0> has no length property.")); | |
184 | |
185 b.add(0); | |
186 shouldPass(b, hasLength(1)); | |
187 shouldFail(b, hasLength(2), | |
188 "Expected: an object with length of <2> " | |
189 "but: was <[0]> with length of <1>."); | |
190 | |
191 b.add(0); | |
192 shouldFail(b, hasLength(1), | |
193 "Expected: an object with length of <1> " | |
194 "but: was <[0, 0]> with length of <2>."); | |
195 shouldPass(b, hasLength(2)); | |
196 }); | |
197 }); | |
198 | |
199 group('Numeric Matchers', () { | |
200 | |
201 test('greaterThan', () { | |
202 shouldPass(10, greaterThan(9)); | |
203 shouldFail(9, greaterThan(10), | |
204 "Expected: a value greater than <10> but: was <9>."); | |
205 }); | |
206 | |
207 test('greaterThanOrEqualTo', () { | |
208 shouldPass(10, greaterThanOrEqualTo(10)); | |
209 shouldFail(9, greaterThanOrEqualTo(10), | |
210 "Expected: a value greater than or equal to <10> but: was <9>."); | |
211 }); | |
212 | |
213 test('lessThan', () { | |
214 shouldFail(10, lessThan(9), "Expected: a value less than <9> " | |
215 "but: was <10>."); | |
216 shouldPass(9, lessThan(10)); | |
217 }); | |
218 | |
219 test('lessThanOrEqualTo', () { | |
220 shouldPass(10, lessThanOrEqualTo(10)); | |
221 shouldFail(11, lessThanOrEqualTo(10), | |
222 "Expected: a value less than or equal to <10> but: was <11>."); | |
223 }); | |
224 | |
225 test('isZero', () { | |
226 shouldPass(0, isZero); | |
227 shouldFail(1, isZero, "Expected: a value equal to <0> but: was <1>."); | |
228 }); | |
229 | |
230 test('isNonZero', () { | |
231 shouldFail(0, isNonZero, "Expected: a value not equal to <0> " | |
232 "but: was <0>."); | |
233 shouldPass(1, isNonZero); | |
234 }); | |
235 | |
236 test('isPositive', () { | |
237 shouldFail(-1, isPositive, "Expected: a positive value " | |
238 "but: was <-1>."); | |
239 shouldFail(0, isPositive, "Expected: a positive value " | |
240 "but: was <0>."); | |
241 shouldPass(1, isPositive); | |
242 }); | |
243 | |
244 test('isNegative', () { | |
245 shouldPass(-1, isNegative); | |
246 shouldFail(0, isNegative, | |
247 "Expected: a negative value but: was <0>."); | |
248 }); | |
249 | |
250 test('isNonPositive', () { | |
251 shouldPass(-1, isNonPositive); | |
252 shouldPass(0, isNonPositive); | |
253 shouldFail(1, isNonPositive, | |
254 "Expected: a non-positive value but: was <1>."); | |
255 }); | |
256 | |
257 test('isNonNegative', () { | |
258 shouldPass(1, isNonNegative); | |
259 shouldPass(0, isNonNegative); | |
260 shouldFail(-1, isNonNegative, | |
261 "Expected: a non-negative value but: was <-1>."); | |
262 }); | |
263 | |
264 test('closeTo', () { | |
265 shouldPass(0, closeTo(0, 1)); | |
266 shouldPass(-1, closeTo(0, 1)); | |
267 shouldPass(1, closeTo(0, 1)); | |
268 shouldFail(1.001, closeTo(0, 1), | |
269 "Expected: a numeric value within <1> of <0> " | |
270 "but: <1.001> differed by <1.001>."); | |
271 shouldFail(-1.001, closeTo(0, 1), | |
272 "Expected: a numeric value within <1> of <0> " | |
273 "but: <-1.001> differed by <1.001>."); | |
274 }); | |
275 | |
276 test('inInclusiveRange', () { | |
277 shouldFail(-1, inInclusiveRange(0,2), | |
278 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
279 "but: was <-1>."); | |
280 shouldPass(0, inInclusiveRange(0,2)); | |
281 shouldPass(1, inInclusiveRange(0,2)); | |
282 shouldPass(2, inInclusiveRange(0,2)); | |
283 shouldFail(3, inInclusiveRange(0,2), | |
284 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
285 "but: was <3>."); | |
286 }); | |
287 | |
288 test('inExclusiveRange', () { | |
289 shouldFail(0, inExclusiveRange(0,2), | |
290 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
291 "but: was <0>."); | |
292 shouldPass(1, inExclusiveRange(0,2)); | |
293 shouldFail(2, inExclusiveRange(0,2), | |
294 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
295 "but: was <2>."); | |
296 }); | |
297 | |
298 test('inOpenClosedRange', () { | |
299 shouldFail(0, inOpenClosedRange(0,2), | |
300 "Expected: be in range from 0 (exclusive) to 2 (inclusive) " | |
301 "but: was <0>."); | |
302 shouldPass(1, inOpenClosedRange(0,2)); | |
303 shouldPass(2, inOpenClosedRange(0,2)); | |
304 }); | |
305 | |
306 test('inClosedOpenRange', () { | |
307 shouldPass(0, inClosedOpenRange(0,2)); | |
308 shouldPass(1, inClosedOpenRange(0,2)); | |
309 shouldFail(2, inClosedOpenRange(0,2), | |
310 "Expected: be in range from 0 (inclusive) to 2 (exclusive) " | |
311 "but: was <2>."); | |
312 }); | |
313 }); | |
314 | |
315 group('String Matchers', () { | |
316 | |
317 test('isEmpty', () { | |
318 shouldPass('', isEmpty); | |
319 shouldFail(null, isEmpty, | |
320 "Expected: empty but: was <null>."); | |
321 shouldFail(0, isEmpty, | |
322 "Expected: empty but: was <0>."); | |
323 shouldFail('a', isEmpty, "Expected: empty but: was 'a'."); | |
324 }); | |
325 | |
326 test('equalsIgnoringCase', () { | |
327 shouldPass('hello', equalsIgnoringCase('HELLO')); | |
328 shouldFail('hi', equalsIgnoringCase('HELLO'), | |
329 "Expected: 'HELLO' ignoring case but: was 'hi'."); | |
330 }); | |
331 | |
332 test('equalsIgnoringWhitespace', () { | |
333 shouldPass(' hello world ', equalsIgnoringWhitespace('hello world')); | |
334 shouldFail(' helloworld ', equalsIgnoringWhitespace('hello world'), | |
335 "Expected: 'hello world' ignoring whitespace but: was 'helloworld'."); | |
336 }); | |
337 | |
338 test('startsWith', () { | |
339 shouldPass('hello', startsWith('')); | |
340 shouldPass('hello', startsWith('hell')); | |
341 shouldPass('hello', startsWith('hello')); | |
342 shouldFail('hello', startsWith('hello '), | |
343 "Expected: a string starting with 'hello ' but: was 'hello'."); | |
344 }); | |
345 | |
346 test('endsWith', () { | |
347 shouldPass('hello', endsWith('')); | |
348 shouldPass('hello', endsWith('lo')); | |
349 shouldPass('hello', endsWith('hello')); | |
350 shouldFail('hello', endsWith(' hello'), | |
351 "Expected: a string ending with ' hello' but: was 'hello'."); | |
352 }); | |
353 | |
354 test('contains', () { | |
355 shouldPass('hello', contains('')); | |
356 shouldPass('hello', contains('h')); | |
357 shouldPass('hello', contains('o')); | |
358 shouldPass('hello', contains('hell')); | |
359 shouldPass('hello', contains('hello')); | |
360 shouldFail('hello', contains(' '), | |
361 "Expected: contains ' ' but: was 'hello'."); | |
362 }); | |
363 | |
364 test('stringContainsInOrder', () { | |
365 shouldPass('goodbye cruel world', stringContainsInOrder([''])); | |
366 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye'])); | |
367 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel'])); | |
368 shouldPass('goodbye cruel world', stringContainsInOrder(['world'])); | |
369 shouldPass('goodbye cruel world', | |
370 stringContainsInOrder(['good', 'bye', 'world'])); | |
371 shouldPass('goodbye cruel world', | |
372 stringContainsInOrder(['goodbye', 'cruel'])); | |
373 shouldPass('goodbye cruel world', | |
374 stringContainsInOrder(['cruel', 'world'])); | |
375 shouldPass('goodbye cruel world', | |
376 stringContainsInOrder(['goodbye', 'cruel', 'world'])); | |
377 shouldFail('goodbye cruel world', | |
378 stringContainsInOrder(['goo', 'cruel', 'bye']), | |
379 "Expected: a string containing 'goo', 'cruel', 'bye' in order " | |
380 "but: was 'goodbye cruel world'."); | |
381 }); | |
382 | |
383 test('matches', () { | |
384 shouldPass('c0d', matches('[a-z][0-9][a-z]')); | |
385 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); | |
386 shouldFail('cOd', matches('[a-z][0-9][a-z]'), | |
387 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'."); | |
388 }); | |
389 }); | |
390 | |
391 group('Collection Matchers', () { | |
392 | |
393 test('isEmpty', () { | |
394 shouldPass([], isEmpty); | |
395 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>."); | |
396 }); | |
397 | |
398 test('contains', () { | |
399 var d = [1, 2]; | |
400 shouldPass(d, contains(1)); | |
401 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>."); | |
402 }); | |
403 | |
404 test('isIn', () { | |
405 var d = [1, 2]; | |
406 shouldPass(1, isIn(d)); | |
407 shouldFail(0, isIn(d), "Expected: is in <[1, 2]> but: was <0>."); | |
408 }); | |
409 | |
410 test('everyElement', () { | |
411 var d = [1, 2]; | |
412 var e = [1, 1, 1]; | |
413 shouldFail(d, everyElement(1), | |
414 "Expected: every element <1> but: was <2> at position 1."); | |
415 shouldPass(e, everyElement(1)); | |
416 }); | |
417 | |
418 test('someElement', () { | |
419 var d = [1, 2]; | |
420 var e = [1, 1, 1]; | |
421 shouldPass(d, someElement(2)); | |
422 shouldFail(e, someElement(2), | |
423 "Expected: some element <2> but: was <[1, 1, 1]>."); | |
424 }); | |
425 | |
426 test('orderedEquals', () { | |
427 shouldPass([null], orderedEquals([null])); | |
428 var d = [1, 2]; | |
429 shouldPass(d, orderedEquals([1, 2])); | |
430 shouldFail(d, orderedEquals([2, 1]), | |
431 "Expected: equals <[2, 1]> ordered " | |
432 "but: was <1> mismatch at position 0."); | |
433 }); | |
434 | |
435 test('unorderedEquals', () { | |
436 var d = [1, 2]; | |
437 shouldPass(d, unorderedEquals([2, 1])); | |
438 shouldFail(d, unorderedEquals([1]), | |
439 "Expected: equals <[1]> unordered " | |
440 "but: has too many elements (2 > 1)."); | |
441 shouldFail(d, unorderedEquals([3, 2, 1]), | |
442 "Expected: equals <[3, 2, 1]> unordered " | |
443 "but: has too few elements (2 < 3)."); | |
444 shouldFail(d, unorderedEquals([3, 1]), | |
445 "Expected: equals <[3, 1]> unordered " | |
446 "but: has no match for element <3> at position 0."); | |
447 }); | |
448 }); | |
449 | |
450 group('Map Matchers', () { | |
451 | |
452 test('isEmpty', () { | |
453 var a = new Map(); | |
454 shouldPass({}, isEmpty); | |
455 shouldPass(a, isEmpty); | |
456 a['foo'] = 'bar'; | |
457 shouldFail(a, isEmpty, "Expected: empty but: was <{foo: bar}>."); | |
458 }); | |
459 | |
460 test('contains', () { | |
461 var a = new Map(); | |
462 a['foo'] = 'bar'; | |
463 var b = new Map(); | |
464 shouldPass(a, contains('foo')); | |
465 shouldFail(b, contains('foo'), | |
466 "Expected: contains 'foo' but: was <{}>."); | |
467 shouldFail(10, contains('foo'), | |
468 "Expected: contains 'foo' but: was <10>."); | |
469 }); | |
470 | |
471 test('containsValue', () { | |
472 var a = new Map(); | |
473 a['foo'] = 'bar'; | |
474 shouldPass(a, containsValue('bar')); | |
475 shouldFail(a, containsValue('ba'), | |
476 "Expected: contains value 'ba' but: was <{foo: bar}>."); | |
477 }); | |
478 | |
479 test('containsPair', () { | |
480 var a = new Map(); | |
481 a['foo'] = 'bar'; | |
482 shouldPass(a, containsPair('foo', 'bar')); | |
483 shouldFail(a, containsPair('foo', 'ba'), | |
484 "Expected: contains pair 'foo' => 'ba' " | |
485 "but: contains key 'foo' but with value was 'bar'."); | |
486 shouldFail(a, containsPair('fo', 'bar'), | |
487 "Expected: contains pair 'fo' => 'bar' " | |
488 "but: <{foo: bar}> doesn't contain key 'fo'."); | |
489 }); | |
490 | |
491 test('hasLength', () { | |
492 var a = new Map(); | |
493 a['foo'] = 'bar'; | |
494 var b = new Map(); | |
495 shouldPass(a, hasLength(1)); | |
496 shouldFail(b, hasLength(1), | |
497 "Expected: an object with length of <1> " | |
498 "but: was <{}> with length of <0>."); | |
499 }); | |
500 }); | |
501 | |
502 group('Operator Matchers', () { | |
503 | |
504 test('anyOf', () { | |
505 shouldFail(0, anyOf([equals(1), equals(2)]), | |
506 "Expected: (<1> or <2>) but: was <0>."); | |
507 shouldPass(1, anyOf([equals(1), equals(2)])); | |
508 }); | |
509 | |
510 test('allOf', () { | |
511 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | |
512 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | |
513 "Expected: (a value less than <10> and a value greater than <0>) " | |
514 "but: a value greater than <0> was <-1>."); | |
515 }); | |
516 }); | |
517 | |
518 group('Predicate Matchers', () { | |
519 test('isInstanceOf', () { | |
520 shouldFail(0, predicate((x) => x is String, "an instance of String"), | |
521 "Expected: an instance of String but: was <0>."); | |
522 shouldPass('cow', predicate((x) => x is String, "an instance of String")); | |
523 }); | |
524 }); | |
525 } | |
526 | |
OLD | NEW |