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 matcher.test; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:collection'; | |
9 | |
10 import 'package:matcher/matcher.dart'; | |
11 import 'package:unittest/unittest.dart' show test, group; | |
12 | |
13 import 'test_utils.dart'; | |
14 | |
15 void main() { | |
16 | |
17 initUtils(); | |
18 | |
19 // Core matchers | |
20 | |
21 group('Core matchers', () { | |
22 | |
23 test('isTrue', () { | |
24 shouldPass(true, isTrue); | |
25 shouldFail(false, isTrue, "Expected: true Actual: <false>"); | |
26 }); | |
27 | |
28 test('isFalse', () { | |
29 shouldPass(false, isFalse); | |
30 shouldFail(10, isFalse, "Expected: false Actual: <10>"); | |
31 shouldFail(true, isFalse, "Expected: false Actual: <true>"); | |
32 }); | |
33 | |
34 test('isNull', () { | |
35 shouldPass(null, isNull); | |
36 shouldFail(false, isNull, "Expected: null Actual: <false>"); | |
37 }); | |
38 | |
39 test('isNotNull', () { | |
40 shouldPass(false, isNotNull); | |
41 shouldFail(null, isNotNull, "Expected: not null Actual: <null>"); | |
42 }); | |
43 | |
44 test('same', () { | |
45 var a = new Map(); | |
46 var b = new Map(); | |
47 shouldPass(a, same(a)); | |
48 shouldFail(b, same(a), "Expected: same instance as {} Actual: {}"); | |
49 }); | |
50 | |
51 test('equals', () { | |
52 var a = new Map(); | |
53 var b = new Map(); | |
54 shouldPass(a, equals(a)); | |
55 shouldPass(a, equals(b)); | |
56 }); | |
57 | |
58 test('anything', () { | |
59 var a = new Map(); | |
60 shouldPass(0, anything); | |
61 shouldPass(null, anything); | |
62 shouldPass(a, anything); | |
63 shouldFail(a, isNot(anything), "Expected: not anything Actual: {}"); | |
64 }); | |
65 | |
66 test('throws', () { | |
67 shouldFail(doesNotThrow, throws, | |
68 matches( | |
69 r"Expected: throws" | |
70 r" Actual: <Closure(: \(\) => dynamic " | |
71 r"from Function 'doesNotThrow': static\.)?>" | |
72 r" Which: did not throw")); | |
73 shouldPass(doesThrow, throws); | |
74 shouldFail(true, throws, | |
75 "Expected: throws" | |
76 " Actual: <true>" | |
77 " Which: is not a Function or Future"); | |
78 }); | |
79 | |
80 test('throwsA', () { | |
81 shouldPass(doesThrow, throwsA(equals('X'))); | |
82 shouldFail(doesThrow, throwsA(equals('Y')), | |
83 matches( | |
84 r"Expected: throws 'Y'" | |
85 r" Actual: <Closure(: \(\) => dynamic " | |
86 r"from Function 'doesThrow': static\.)?>" | |
87 r" Which: threw 'X'")); | |
88 }); | |
89 | |
90 test('returnsNormally', () { | |
91 shouldPass(doesNotThrow, returnsNormally); | |
92 shouldFail(doesThrow, returnsNormally, | |
93 matches( | |
94 r"Expected: return normally" | |
95 r" Actual: <Closure(: \(\) => dynamic " | |
96 r"from Function 'doesThrow': static\.)?>" | |
97 r" Which: threw 'X'")); | |
98 }); | |
99 | |
100 | |
101 test('hasLength', () { | |
102 var a = new Map(); | |
103 var b = new List(); | |
104 shouldPass(a, hasLength(0)); | |
105 shouldPass(b, hasLength(0)); | |
106 shouldPass('a', hasLength(1)); | |
107 shouldFail(0, hasLength(0), new PrefixMatcher( | |
108 "Expected: an object with length of <0> " | |
109 "Actual: <0> " | |
110 "Which: has no length property")); | |
111 | |
112 b.add(0); | |
113 shouldPass(b, hasLength(1)); | |
114 shouldFail(b, hasLength(2), | |
115 "Expected: an object with length of <2> " | |
116 "Actual: [0] " | |
117 "Which: has length of <1>"); | |
118 | |
119 b.add(0); | |
120 shouldFail(b, hasLength(1), | |
121 "Expected: an object with length of <1> " | |
122 "Actual: [0, 0] " | |
123 "Which: has length of <2>"); | |
124 shouldPass(b, hasLength(2)); | |
125 }); | |
126 | |
127 test('scalar type mismatch', () { | |
128 shouldFail('error', equals(5.1), | |
129 "Expected: <5.1> " | |
130 "Actual: 'error'"); | |
131 }); | |
132 | |
133 test('nested type mismatch', () { | |
134 shouldFail(['error'], equals([5.1]), | |
135 "Expected: [5.1] " | |
136 "Actual: ['error'] " | |
137 "Which: was 'error' instead of <5.1> at location [0]"); | |
138 }); | |
139 | |
140 test('doubly-nested type mismatch', () { | |
141 shouldFail([['error']], equals([[5.1]]), | |
142 "Expected: [[5.1]] " | |
143 "Actual: [['error']] " | |
144 "Which: was 'error' instead of <5.1> at location [0][0]"); | |
145 }); | |
146 | |
147 test('doubly nested inequality', () { | |
148 var actual1 = [['foo', 'bar'], ['foo'], 3, []]; | |
149 var expected1 = [['foo', 'bar'], ['foo'], 4, []]; | |
150 var reason1 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | |
151 "Actual: [['foo', 'bar'], ['foo'], 3, []] " | |
152 "Which: was <3> instead of <4> at location [2]"; | |
153 | |
154 var actual2 = [['foo', 'barry'], ['foo'], 4, []]; | |
155 var expected2 = [['foo', 'bar'], ['foo'], 4, []]; | |
156 var reason2 = "Expected: [['foo', 'bar'], ['foo'], 4, []] " | |
157 "Actual: [['foo', 'barry'], ['foo'], 4, []] " | |
158 "Which: was 'barry' instead of 'bar' at location [0][1]"; | |
159 | |
160 var actual3 = [['foo', 'bar'], ['foo'], 4, {'foo':'bar'}]; | |
161 var expected3 = [['foo', 'bar'], ['foo'], 4, {'foo':'barry'}]; | |
162 var reason3 = "Expected: [['foo', 'bar'], ['foo'], 4, {'foo': 'barry'}] " | |
163 "Actual: [['foo', 'bar'], ['foo'], 4, {'foo': 'bar'}] " | |
164 "Which: was 'bar' instead of 'barry' at location [3]['foo']"; | |
165 | |
166 shouldFail(actual1, equals(expected1), reason1); | |
167 shouldFail(actual2, equals(expected2), reason2); | |
168 shouldFail(actual3, equals(expected3), reason3); | |
169 }); | |
170 }); | |
171 | |
172 group('Numeric Matchers', () { | |
173 | |
174 test('greaterThan', () { | |
175 shouldPass(10, greaterThan(9)); | |
176 shouldFail(9, greaterThan(10), | |
177 "Expected: a value greater than <10> " | |
178 "Actual: <9> " | |
179 "Which: is not a value greater than <10>"); | |
180 }); | |
181 | |
182 test('greaterThanOrEqualTo', () { | |
183 shouldPass(10, greaterThanOrEqualTo(10)); | |
184 shouldFail(9, greaterThanOrEqualTo(10), | |
185 "Expected: a value greater than or equal to <10> " | |
186 "Actual: <9> " | |
187 "Which: is not a value greater than or equal to <10>"); | |
188 }); | |
189 | |
190 test('lessThan', () { | |
191 shouldFail(10, lessThan(9), | |
192 "Expected: a value less than <9> " | |
193 "Actual: <10> " | |
194 "Which: is not a value less than <9>"); | |
195 shouldPass(9, lessThan(10)); | |
196 }); | |
197 | |
198 test('lessThanOrEqualTo', () { | |
199 shouldPass(10, lessThanOrEqualTo(10)); | |
200 shouldFail(11, lessThanOrEqualTo(10), | |
201 "Expected: a value less than or equal to <10> " | |
202 "Actual: <11> " | |
203 "Which: is not a value less than or equal to <10>"); | |
204 }); | |
205 | |
206 test('isZero', () { | |
207 shouldPass(0, isZero); | |
208 shouldFail(1, isZero, | |
209 "Expected: a value equal to <0> " | |
210 "Actual: <1> " | |
211 "Which: is not a value equal to <0>"); | |
212 }); | |
213 | |
214 test('isNonZero', () { | |
215 shouldFail(0, isNonZero, | |
216 "Expected: a value not equal to <0> " | |
217 "Actual: <0> " | |
218 "Which: is not a value not equal to <0>"); | |
219 shouldPass(1, isNonZero); | |
220 }); | |
221 | |
222 test('isPositive', () { | |
223 shouldFail(-1, isPositive, | |
224 "Expected: a positive value " | |
225 "Actual: <-1> " | |
226 "Which: is not a positive value"); | |
227 shouldFail(0, isPositive, | |
228 "Expected: a positive value " | |
229 "Actual: <0> " | |
230 "Which: is not a positive value"); | |
231 shouldPass(1, isPositive); | |
232 }); | |
233 | |
234 test('isNegative', () { | |
235 shouldPass(-1, isNegative); | |
236 shouldFail(0, isNegative, | |
237 "Expected: a negative value " | |
238 "Actual: <0> " | |
239 "Which: is not a negative value"); | |
240 }); | |
241 | |
242 test('isNonPositive', () { | |
243 shouldPass(-1, isNonPositive); | |
244 shouldPass(0, isNonPositive); | |
245 shouldFail(1, isNonPositive, | |
246 "Expected: a non-positive value " | |
247 "Actual: <1> " | |
248 "Which: is not a non-positive value"); | |
249 }); | |
250 | |
251 test('isNonNegative', () { | |
252 shouldPass(1, isNonNegative); | |
253 shouldPass(0, isNonNegative); | |
254 shouldFail(-1, isNonNegative, | |
255 "Expected: a non-negative value " | |
256 "Actual: <-1> " | |
257 "Which: is not a non-negative value"); | |
258 }); | |
259 | |
260 test('closeTo', () { | |
261 shouldPass(0, closeTo(0, 1)); | |
262 shouldPass(-1, closeTo(0, 1)); | |
263 shouldPass(1, closeTo(0, 1)); | |
264 shouldFail(1.001, closeTo(0, 1), | |
265 "Expected: a numeric value within <1> of <0> " | |
266 "Actual: <1.001> " | |
267 "Which: differs by <1.001>"); | |
268 shouldFail(-1.001, closeTo(0, 1), | |
269 "Expected: a numeric value within <1> of <0> " | |
270 "Actual: <-1.001> " | |
271 "Which: differs by <1.001>"); | |
272 }); | |
273 | |
274 test('inInclusiveRange', () { | |
275 shouldFail(-1, inInclusiveRange(0,2), | |
276 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
277 "Actual: <-1>"); | |
278 shouldPass(0, inInclusiveRange(0,2)); | |
279 shouldPass(1, inInclusiveRange(0,2)); | |
280 shouldPass(2, inInclusiveRange(0,2)); | |
281 shouldFail(3, inInclusiveRange(0,2), | |
282 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
283 "Actual: <3>"); | |
284 }); | |
285 | |
286 test('inExclusiveRange', () { | |
287 shouldFail(0, inExclusiveRange(0,2), | |
288 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
289 "Actual: <0>"); | |
290 shouldPass(1, inExclusiveRange(0,2)); | |
291 shouldFail(2, inExclusiveRange(0,2), | |
292 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
293 "Actual: <2>"); | |
294 }); | |
295 | |
296 test('inOpenClosedRange', () { | |
297 shouldFail(0, inOpenClosedRange(0,2), | |
298 "Expected: be in range from 0 (exclusive) to 2 (inclusive) " | |
299 "Actual: <0>"); | |
300 shouldPass(1, inOpenClosedRange(0,2)); | |
301 shouldPass(2, inOpenClosedRange(0,2)); | |
302 }); | |
303 | |
304 test('inClosedOpenRange', () { | |
305 shouldPass(0, inClosedOpenRange(0,2)); | |
306 shouldPass(1, inClosedOpenRange(0,2)); | |
307 shouldFail(2, inClosedOpenRange(0,2), | |
308 "Expected: be in range from 0 (inclusive) to 2 (exclusive) " | |
309 "Actual: <2>"); | |
310 }); | |
311 }); | |
312 | |
313 group('String Matchers', () { | |
314 | |
315 test('isEmpty', () { | |
316 shouldPass('', isEmpty); | |
317 shouldFail(null, isEmpty, | |
318 "Expected: empty Actual: <null>"); | |
319 shouldFail(0, isEmpty, | |
320 "Expected: empty Actual: <0>"); | |
321 shouldFail('a', isEmpty, "Expected: empty Actual: 'a'"); | |
322 }); | |
323 | |
324 test('equalsIgnoringCase', () { | |
325 shouldPass('hello', equalsIgnoringCase('HELLO')); | |
326 shouldFail('hi', equalsIgnoringCase('HELLO'), | |
327 "Expected: 'HELLO' ignoring case Actual: 'hi'"); | |
328 }); | |
329 | |
330 test('equalsIgnoringWhitespace', () { | |
331 shouldPass(' hello world ', equalsIgnoringWhitespace('hello world')); | |
332 shouldFail(' helloworld ', equalsIgnoringWhitespace('hello world'), | |
333 "Expected: 'hello world' ignoring whitespace " | |
334 "Actual: ' helloworld ' " | |
335 "Which: is 'helloworld' with whitespace compressed"); | |
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 ' " | |
344 "Actual: 'hello'"); | |
345 }); | |
346 | |
347 test('endsWith', () { | |
348 shouldPass('hello', endsWith('')); | |
349 shouldPass('hello', endsWith('lo')); | |
350 shouldPass('hello', endsWith('hello')); | |
351 shouldFail('hello', endsWith(' hello'), | |
352 "Expected: a string ending with ' hello' " | |
353 "Actual: 'hello'"); | |
354 }); | |
355 | |
356 test('contains', () { | |
357 shouldPass('hello', contains('')); | |
358 shouldPass('hello', contains('h')); | |
359 shouldPass('hello', contains('o')); | |
360 shouldPass('hello', contains('hell')); | |
361 shouldPass('hello', contains('hello')); | |
362 shouldFail('hello', contains(' '), | |
363 "Expected: contains ' ' Actual: 'hello'"); | |
364 }); | |
365 | |
366 test('stringContainsInOrder', () { | |
367 shouldPass('goodbye cruel world', stringContainsInOrder([''])); | |
368 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye'])); | |
369 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel'])); | |
370 shouldPass('goodbye cruel world', stringContainsInOrder(['world'])); | |
371 shouldPass('goodbye cruel world', | |
372 stringContainsInOrder(['good', 'bye', 'world'])); | |
373 shouldPass('goodbye cruel world', | |
374 stringContainsInOrder(['goodbye', 'cruel'])); | |
375 shouldPass('goodbye cruel world', | |
376 stringContainsInOrder(['cruel', 'world'])); | |
377 shouldPass('goodbye cruel world', | |
378 stringContainsInOrder(['goodbye', 'cruel', 'world'])); | |
379 shouldFail('goodbye cruel world', | |
380 stringContainsInOrder(['goo', 'cruel', 'bye']), | |
381 "Expected: a string containing 'goo', 'cruel', 'bye' in order " | |
382 "Actual: 'goodbye cruel world'"); | |
383 }); | |
384 | |
385 test('matches', () { | |
386 shouldPass('c0d', matches('[a-z][0-9][a-z]')); | |
387 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); | |
388 shouldFail('cOd', matches('[a-z][0-9][a-z]'), | |
389 "Expected: match '[a-z][0-9][a-z]' Actual: 'cOd'"); | |
390 }); | |
391 }); | |
392 | |
393 group('Iterable Matchers', () { | |
394 | |
395 test('isEmpty', () { | |
396 shouldPass([], isEmpty); | |
397 shouldFail([1], isEmpty, "Expected: empty Actual: [1]"); | |
398 }); | |
399 | |
400 test('contains', () { | |
401 var d = [1, 2]; | |
402 shouldPass(d, contains(1)); | |
403 shouldFail(d, contains(0), "Expected: contains <0> " | |
404 "Actual: [1, 2]"); | |
405 }); | |
406 | |
407 test('equals with matcher element', () { | |
408 var d = ['foo', 'bar']; | |
409 shouldPass(d, equals(['foo', startsWith('ba')])); | |
410 shouldFail(d, equals(['foo', endsWith('ba')]), | |
411 "Expected: ['foo', <a string ending with 'ba'>] " | |
412 "Actual: ['foo', 'bar'] " | |
413 "Which: does not match a string ending with 'ba' at location [1]"); | |
414 }); | |
415 | |
416 test('isIn', () { | |
417 var d = [1, 2]; | |
418 shouldPass(1, isIn(d)); | |
419 shouldFail(0, isIn(d), "Expected: is in [1, 2] Actual: <0>"); | |
420 }); | |
421 | |
422 test('everyElement', () { | |
423 var d = [1, 2]; | |
424 var e = [1, 1, 1]; | |
425 shouldFail(d, everyElement(1), | |
426 "Expected: every element(<1>) " | |
427 "Actual: [1, 2] " | |
428 "Which: has value <2> which doesn't match <1> at index 1"); | |
429 shouldPass(e, everyElement(1)); | |
430 }); | |
431 | |
432 test('nested everyElement', () { | |
433 var d = [['foo', 'bar'], ['foo'], []]; | |
434 var e = [['foo', 'bar'], ['foo'], 3, []]; | |
435 shouldPass(d, everyElement(anyOf(isEmpty, contains('foo')))); | |
436 shouldFail(d, everyElement(everyElement(equals('foo'))), | |
437 "Expected: every element(every element('foo')) " | |
438 "Actual: [['foo', 'bar'], ['foo'], []] " | |
439 "Which: has value ['foo', 'bar'] which has value 'bar' " | |
440 "which is different. Expected: foo Actual: bar ^ " | |
441 "Differ at offset 0 at index 1 at index 0"); | |
442 shouldFail(d, everyElement(allOf(hasLength(greaterThan(0)), | |
443 contains('foo'))), | |
444 "Expected: every element((an object with length of a value " | |
445 "greater than <0> and contains 'foo')) " | |
446 "Actual: [['foo', 'bar'], ['foo'], []] " | |
447 "Which: has value [] which has length of <0> at index 2"); | |
448 shouldFail(d, everyElement(allOf(contains('foo'), | |
449 hasLength(greaterThan(0)))), | |
450 "Expected: every element((contains 'foo' and " | |
451 "an object with length of a value greater than <0>)) " | |
452 "Actual: [['foo', 'bar'], ['foo'], []] " | |
453 "Which: has value [] which doesn't match (contains 'foo' and " | |
454 "an object with length of a value greater than <0>) at index 2"); | |
455 shouldFail(e, everyElement(allOf(contains('foo'), | |
456 hasLength(greaterThan(0)))), | |
457 "Expected: every element((contains 'foo' and an object with " | |
458 "length of a value greater than <0>)) " | |
459 "Actual: [['foo', 'bar'], ['foo'], 3, []] " | |
460 "Which: has value <3> which is not a string, map or iterable " | |
461 "at index 2"); | |
462 }); | |
463 | |
464 test('anyElement', () { | |
465 var d = [1, 2]; | |
466 var e = [1, 1, 1]; | |
467 shouldPass(d, anyElement(2)); | |
468 shouldFail(e, anyElement(2), | |
469 "Expected: some element <2> Actual: [1, 1, 1]"); | |
470 }); | |
471 | |
472 test('orderedEquals', () { | |
473 shouldPass([null], orderedEquals([null])); | |
474 var d = [1, 2]; | |
475 shouldPass(d, orderedEquals([1, 2])); | |
476 shouldFail(d, orderedEquals([2, 1]), | |
477 "Expected: equals [2, 1] ordered " | |
478 "Actual: [1, 2] " | |
479 "Which: was <1> instead of <2> at location [0]"); | |
480 }); | |
481 | |
482 test('unorderedEquals', () { | |
483 var d = [1, 2]; | |
484 shouldPass(d, unorderedEquals([2, 1])); | |
485 shouldFail(d, unorderedEquals([1]), | |
486 "Expected: equals [1] unordered " | |
487 "Actual: [1, 2] " | |
488 "Which: has too many elements (2 > 1)"); | |
489 shouldFail(d, unorderedEquals([3, 2, 1]), | |
490 "Expected: equals [3, 2, 1] unordered " | |
491 "Actual: [1, 2] " | |
492 "Which: has too few elements (2 < 3)"); | |
493 shouldFail(d, unorderedEquals([3, 1]), | |
494 "Expected: equals [3, 1] unordered " | |
495 "Actual: [1, 2] " | |
496 "Which: has no match for <3> at index 0"); | |
497 }); | |
498 | |
499 test('unorderedMatchess', () { | |
500 var d = [1, 2]; | |
501 shouldPass(d, unorderedMatches([2, 1])); | |
502 shouldPass(d, unorderedMatches([greaterThan(1), greaterThan(0)])); | |
503 shouldFail(d, unorderedMatches([greaterThan(0)]), | |
504 "Expected: matches [a value greater than <0>] unordered " | |
505 "Actual: [1, 2] " | |
506 "Which: has too many elements (2 > 1)"); | |
507 shouldFail(d, unorderedMatches([3, 2, 1]), | |
508 "Expected: matches [<3>, <2>, <1>] unordered " | |
509 "Actual: [1, 2] " | |
510 "Which: has too few elements (2 < 3)"); | |
511 shouldFail(d, unorderedMatches([3, 1]), | |
512 "Expected: matches [<3>, <1>] unordered " | |
513 "Actual: [1, 2] " | |
514 "Which: has no match for <3> at index 0"); | |
515 shouldFail(d, unorderedMatches([greaterThan(3), greaterThan(0)]), | |
516 "Expected: matches [a value greater than <3>, a value greater than " | |
517 "<0>] unordered " | |
518 "Actual: [1, 2] " | |
519 "Which: has no match for a value greater than <3> at index 0"); | |
520 }); | |
521 | |
522 test('pairwise compare', () { | |
523 var c = [1, 2]; | |
524 var d = [1, 2, 3]; | |
525 var e = [1, 4, 9]; | |
526 shouldFail('x', pairwiseCompare(e, (e,a) => a <= e, | |
527 "less than or equal"), | |
528 "Expected: pairwise less than or equal [1, 4, 9] " | |
529 "Actual: 'x' " | |
530 "Which: is not an Iterable"); | |
531 shouldFail(c, pairwiseCompare(e, (e,a) => a <= e, "less than or equal"), | |
532 "Expected: pairwise less than or equal [1, 4, 9] " | |
533 "Actual: [1, 2] " | |
534 "Which: has length 2 instead of 3"); | |
535 shouldPass(d, pairwiseCompare(e, (e,a) => a <= e, "less than or equal")); | |
536 shouldFail(d, pairwiseCompare(e, (e,a) => a < e, "less than"), | |
537 "Expected: pairwise less than [1, 4, 9] " | |
538 "Actual: [1, 2, 3] " | |
539 "Which: has <1> which is not less than <1> at index 0"); | |
540 shouldPass(d, pairwiseCompare(e, (e,a) => a * a == e, "square root of")); | |
541 shouldFail(d, pairwiseCompare(e, (e,a) => a + a == e, "double"), | |
542 "Expected: pairwise double [1, 4, 9] " | |
543 "Actual: [1, 2, 3] " | |
544 "Which: has <1> which is not double <1> at index 0"); | |
545 }); | |
546 }); | |
547 | |
548 group('Map Matchers', () { | |
549 | |
550 test('isEmpty', () { | |
551 var a = new Map(); | |
552 shouldPass({}, isEmpty); | |
553 shouldPass(a, isEmpty); | |
554 a['foo'] = 'bar'; | |
555 shouldFail(a, isEmpty, "Expected: empty " | |
556 "Actual: {'foo': 'bar'}"); | |
557 }); | |
558 | |
559 test('equals', () { | |
560 var a = new Map(); | |
561 a['foo'] = 'bar'; | |
562 var b = new Map(); | |
563 b['foo'] = 'bar'; | |
564 var c = new Map(); | |
565 c['bar'] = 'foo'; | |
566 shouldPass(a, equals(b)); | |
567 shouldFail(b, equals(c), | |
568 "Expected: {'bar': 'foo'} " | |
569 "Actual: {'foo': 'bar'} " | |
570 "Which: is missing map key 'bar'"); | |
571 }); | |
572 | |
573 test('equals with different lengths', () { | |
574 var a = new LinkedHashMap(); | |
575 a['foo'] = 'bar'; | |
576 var b = new LinkedHashMap(); | |
577 b['foo'] = 'bar'; | |
578 b['bar'] = 'foo'; | |
579 var c = new LinkedHashMap(); | |
580 c['bar'] = 'foo'; | |
581 c['barrista'] = 'caffeine'; | |
582 shouldFail(a, equals(b), | |
583 "Expected: {'foo': 'bar', 'bar': 'foo'} " | |
584 "Actual: {'foo': 'bar'} " | |
585 "Which: has different length and is missing map key 'bar'"); | |
586 shouldFail(b, equals(a), | |
587 "Expected: {'foo': 'bar'} " | |
588 "Actual: {'foo': 'bar', 'bar': 'foo'} " | |
589 "Which: has different length and has extra map key 'bar'"); | |
590 shouldFail(b, equals(c), | |
591 "Expected: {'bar': 'foo', 'barrista': 'caffeine'} " | |
592 "Actual: {'foo': 'bar', 'bar': 'foo'} " | |
593 "Which: is missing map key 'barrista'"); | |
594 shouldFail(c, equals(b), | |
595 "Expected: {'foo': 'bar', 'bar': 'foo'} " | |
596 "Actual: {'bar': 'foo', 'barrista': 'caffeine'} " | |
597 "Which: is missing map key 'foo'"); | |
598 shouldFail(a, equals(c), | |
599 "Expected: {'bar': 'foo', 'barrista': 'caffeine'} " | |
600 "Actual: {'foo': 'bar'} " | |
601 "Which: has different length and is missing map key 'bar'"); | |
602 shouldFail(c, equals(a), | |
603 "Expected: {'foo': 'bar'} " | |
604 "Actual: {'bar': 'foo', 'barrista': 'caffeine'} " | |
605 "Which: has different length and is missing map key 'foo'"); | |
606 }); | |
607 | |
608 test('equals with matcher value', () { | |
609 var a = new Map(); | |
610 a['foo'] = 'bar'; | |
611 shouldPass(a, equals({'foo': startsWith('ba')})); | |
612 shouldFail(a, equals({'foo': endsWith('ba')}), | |
613 "Expected: {'foo': <a string ending with 'ba'>} " | |
614 "Actual: {'foo': 'bar'} " | |
615 "Which: does not match a string ending with 'ba' " | |
616 "at location ['foo']"); | |
617 }); | |
618 | |
619 test('contains', () { | |
620 var a = new Map(); | |
621 a['foo'] = 'bar'; | |
622 var b = new Map(); | |
623 shouldPass(a, contains('foo')); | |
624 shouldFail(b, contains('foo'), | |
625 "Expected: contains 'foo' Actual: {}"); | |
626 shouldFail(10, contains('foo'), | |
627 "Expected: contains 'foo' Actual: <10> " | |
628 "Which: is not a string, map or iterable"); | |
629 }); | |
630 | |
631 test('containsValue', () { | |
632 var a = new Map(); | |
633 a['foo'] = 'bar'; | |
634 shouldPass(a, containsValue('bar')); | |
635 shouldFail(a, containsValue('ba'), | |
636 "Expected: contains value 'ba' " | |
637 "Actual: {'foo': 'bar'}"); | |
638 }); | |
639 | |
640 test('containsPair', () { | |
641 var a = new Map(); | |
642 a['foo'] = 'bar'; | |
643 shouldPass(a, containsPair('foo', 'bar')); | |
644 shouldFail(a, containsPair('foo', 'ba'), | |
645 "Expected: contains pair 'foo' => 'ba' " | |
646 "Actual: {'foo': 'bar'} " | |
647 "Which: is different. Both strings start the same, but " | |
648 "the given value also has the following trailing characters: r"); | |
649 shouldFail(a, containsPair('fo', 'bar'), | |
650 "Expected: contains pair 'fo' => 'bar' " | |
651 "Actual: {'foo': 'bar'} " | |
652 "Which: doesn't contain key 'fo'"); | |
653 }); | |
654 | |
655 test('hasLength', () { | |
656 var a = new Map(); | |
657 a['foo'] = 'bar'; | |
658 var b = new Map(); | |
659 shouldPass(a, hasLength(1)); | |
660 shouldFail(b, hasLength(1), | |
661 "Expected: an object with length of <1> " | |
662 "Actual: {} " | |
663 "Which: has length of <0>"); | |
664 }); | |
665 }); | |
666 | |
667 group('Operator Matchers', () { | |
668 | |
669 test('anyOf', () { | |
670 shouldFail(0, anyOf([equals(1), equals(2)]), | |
671 "Expected: (<1> or <2>) Actual: <0>"); | |
672 shouldPass(1, anyOf([equals(1), equals(2)])); | |
673 }); | |
674 | |
675 test('allOf', () { | |
676 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | |
677 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | |
678 "Expected: (a value less than <10> and a value greater than <0>) " | |
679 "Actual: <-1> " | |
680 "Which: is not a value greater than <0>"); | |
681 }); | |
682 }); | |
683 | |
684 group('Future Matchers', () { | |
685 | |
686 test('completes - unexpected error', () { | |
687 var completer = new Completer(); | |
688 completer.completeError('X'); | |
689 shouldFail(completer.future, completes, | |
690 contains('Expected future to complete successfully, ' | |
691 'but it failed with X'), | |
692 isAsync: true); | |
693 }); | |
694 | |
695 test('completes - successfully', () { | |
696 var completer = new Completer(); | |
697 completer.complete('1'); | |
698 shouldPass(completer.future, completes, isAsync: true); | |
699 }); | |
700 | |
701 test('throws - unexpected to see normal completion', () { | |
702 var completer = new Completer(); | |
703 completer.complete('1'); | |
704 shouldFail(completer.future, throws, | |
705 contains("Expected future to fail, but succeeded with '1'"), | |
706 isAsync: true); | |
707 }); | |
708 | |
709 test('throws - expected to see exception', () { | |
710 var completer = new Completer(); | |
711 completer.completeError('X'); | |
712 shouldPass(completer.future, throws, isAsync: true); | |
713 }); | |
714 | |
715 test('throws - expected to see exception thrown later on', () { | |
716 var completer = new Completer(); | |
717 var chained = completer.future.then((_) { throw 'X'; }); | |
718 shouldPass(chained, throws, isAsync: true); | |
719 completer.complete('1'); | |
720 }); | |
721 | |
722 test('throwsA - unexpected normal completion', () { | |
723 var completer = new Completer(); | |
724 completer.complete('1'); | |
725 shouldFail(completer.future, throwsA(equals('X')), | |
726 contains("Expected future to fail, but succeeded with '1'"), | |
727 isAsync: true); | |
728 }); | |
729 | |
730 test('throwsA - correct error', () { | |
731 var completer = new Completer(); | |
732 completer.completeError('X'); | |
733 shouldPass(completer.future, throwsA(equals('X')), isAsync: true); | |
734 }); | |
735 | |
736 test('throwsA - wrong error', () { | |
737 var completer = new Completer(); | |
738 completer.completeError('X'); | |
739 shouldFail(completer.future, throwsA(equals('Y')), | |
740 "Expected: 'Y' Actual: 'X' " | |
741 "Which: is different. " | |
742 "Expected: Y Actual: X ^ Differ at offset 0", | |
743 isAsync: true); | |
744 }); | |
745 }); | |
746 | |
747 group('Predicate Matchers', () { | |
748 test('isInstanceOf', () { | |
749 shouldFail(0, predicate((x) => x is String, "an instance of String"), | |
750 "Expected: an instance of String Actual: <0>"); | |
751 shouldPass('cow', predicate((x) => x is String, "an instance of String")); | |
752 }); | |
753 }); | |
754 | |
755 group('exception/error matchers', () { | |
756 // TODO(gram): extend this to more types; for now this is just | |
757 // the types being added in this CL. | |
758 | |
759 test('throwsCyclicInitializationError', () { | |
760 expect(() => _Bicycle.foo, throwsCyclicInitializationError); | |
761 }); | |
762 | |
763 test('throwsAbstractClassInstantiationError', () { | |
764 expect(() => new _AbstractClass(), throwsAbstractClassInstantiationError); | |
765 }); | |
766 | |
767 test('throwsConcurrentModificationError', () { | |
768 expect(() { | |
769 var a = { 'foo': 'bar' }; | |
770 for (var k in a.keys) { | |
771 a.remove(k); | |
772 } | |
773 }, throwsConcurrentModificationError); | |
774 }); | |
775 | |
776 test('throwsNullThrownError', () { | |
777 expect(() => throw null, throwsNullThrownError); | |
778 }); | |
779 | |
780 test('throwsFallThroughError', () { | |
781 expect(() { | |
782 var a = 0; | |
783 switch (a) { | |
784 case 0: | |
785 a += 1; | |
786 case 1: | |
787 return; | |
788 } | |
789 }, throwsFallThroughError); | |
790 }); | |
791 }); | |
792 } | |
793 | |
794 class _Bicycle { | |
795 static final foo = bar(); | |
796 | |
797 static bar() { | |
798 return foo + 1; | |
799 } | |
800 } | |
801 | |
802 abstract class _AbstractClass { | |
803 } | |
OLD | NEW |