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('unittestTest'); | |
6 | |
7 #import('../../lib/unittest/unittest.dart'); | |
8 | |
9 | |
10 doesNotThrow() {} | |
11 doesThrow() { throw 'X'; } | |
12 | |
13 int errorCount; | |
14 String errorString; | |
15 var testHandler; | |
16 | |
17 class MyFailureHandler implements FailureHandler { | |
18 ErrorFormatter format; | |
19 | |
20 MyFailureHandler() { | |
21 format = configureExpectFormatter(); | |
22 } | |
23 void fail(String reason) { | |
24 ++errorCount; | |
25 errorString = reason; | |
26 } | |
27 void failMatch(actual, Matcher matcher, String reason) { | |
28 fail(format(actual, matcher, reason)); | |
29 } | |
30 } | |
31 | |
32 void shouldFail(var value, Matcher matcher, String expected) { | |
33 errorCount = 0; | |
34 errorString = ''; | |
35 configureExpectHandler(testHandler); | |
36 expect(value, matcher); | |
37 configureExpectHandler(null); | |
38 expect(errorCount, equals(1)); | |
39 if (expected is String) | |
40 expect(errorString, equalsIgnoringWhitespace(expected)); | |
41 else | |
42 expect(errorString, expected); | |
43 } | |
44 | |
45 void shouldPass(var value, Matcher matcher) { | |
46 errorCount = 0; | |
47 errorString = ''; | |
48 configureExpectHandler(testHandler); | |
49 expect(value, matcher); | |
50 configureExpectHandler(null); | |
51 expect(errorCount, equals(0)); | |
52 } | |
53 | |
54 class PrefixMatcher extends BaseMatcher { | |
55 final String _prefix; | |
56 const PrefixMatcher(this._prefix); | |
57 bool matches(item) { | |
58 return item is String && | |
Bob Nystrom
2012/06/04 20:04:09
Extra space after "return".
gram
2012/06/05 16:25:46
Done.
| |
59 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | |
60 } | |
61 | |
62 Description describe(Description description) => | |
63 description.add('a string starting with '). | |
64 addDescriptionOf(collapseWhitespace(_prefix)). | |
65 add(' ignoring whitespace'); | |
66 } | |
67 | |
68 void main() { | |
69 | |
70 testHandler = new MyFailureHandler(); | |
71 | |
72 var a = new Map(); | |
73 var b = new Map(); | |
74 var c = new List(); | |
75 | |
76 // Core matchers | |
77 | |
78 group('Core matchers', () { | |
79 test('isTrue', () { | |
80 shouldPass(true, isTrue); | |
81 shouldFail(false, isTrue, "Expected: true but: was <false>"); | |
82 }); | |
83 | |
84 test('isFalse', () { | |
85 shouldPass(false, isFalse); | |
86 shouldFail(true, isFalse, "Expected: false but: was <true>"); | |
87 }); | |
88 | |
89 test('isNull', () { | |
90 shouldPass(null, isNull); | |
91 shouldFail(false, isNull, "Expected: null but: was <false>"); | |
92 }); | |
93 | |
94 test('isNotNull', () { | |
95 shouldPass(false, isNotNull); | |
96 shouldFail(null, isNotNull, "Expected: not null but: was <null>"); | |
97 }); | |
98 | |
99 test('same', () { | |
100 shouldPass(a, same(a)); | |
101 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>"); | |
102 }); | |
103 | |
104 test('equals', () { | |
105 shouldPass(a, equals(a)); | |
106 shouldFail(a, equals(b), "Expected: <{}> but: was <{}>"); | |
107 }); | |
108 | |
109 test('anything', () { | |
110 shouldPass(a, anything); | |
111 shouldFail(a, isNot(anything), "Expected: not anything but: was <{}>"); | |
112 }); | |
113 | |
114 test('throws', () { | |
115 shouldFail(doesNotThrow, throws, | |
116 "Expected: throws an exception but: no exception"); | |
117 shouldPass(doesThrow, throws); | |
118 }); | |
119 | |
120 test('throwsA', () { | |
121 shouldPass(doesThrow, throwsA(equals('X'))); | |
122 shouldFail(doesThrow, throwsA(equals('Y')), | |
123 "Expected: throws an exception which matches 'Y' " | |
124 "but: exception does not match 'Y'"); | |
125 }); | |
126 | |
127 test('returnsNormally', () { | |
128 shouldPass(doesNotThrow, returnsNormally); | |
129 shouldFail(doesThrow, returnsNormally, | |
130 "Expected: return normally but: threw exception"); | |
131 }); | |
132 | |
133 test('isInstanceOf', () { | |
134 shouldFail(0, new isInstanceOf<String>('String'), | |
135 "Expected: an instance of String but: was <0>"); | |
136 shouldPass('cow', new isInstanceOf<String>('String')); | |
137 }); | |
138 | |
139 test('hasLength', () { | |
140 shouldPass(c, hasLength(0)); | |
141 shouldPass(a, hasLength(0)); | |
142 shouldPass('a', hasLength(1)); | |
143 shouldFail(0, hasLength(0), new PrefixMatcher( | |
144 "Expected: an object with length of <0> " | |
145 "but: was <0> has no length property")); | |
146 | |
147 c.add(0); | |
148 shouldPass(c, hasLength(1)); | |
149 shouldFail(c, hasLength(2), | |
150 "Expected: an object with length of <2> " | |
151 "but: was <[0]> with length of <1>"); | |
152 | |
153 c.add(0); | |
154 shouldFail(c, hasLength(1), | |
155 "Expected: an object with length of <1> " | |
156 "but: was <[0, 0]> with length of <2>"); | |
157 shouldPass(c, hasLength(2)); | |
158 }); | |
159 }); | |
160 | |
161 group('Numeric Matchers', () { | |
162 | |
163 test('greaterThan', () { | |
164 shouldPass(10, greaterThan(9)); | |
165 shouldFail(9, greaterThan(10), | |
166 "Expected: a value greater than <10> but: was <9>"); | |
167 }); | |
168 | |
169 test('greaterThanOrEqualTo', () { | |
170 shouldPass(10, greaterThanOrEqualTo(10)); | |
171 shouldFail(9, greaterThanOrEqualTo(10), | |
172 "Expected: a value greater than or equal to <10> but: was <9>"); | |
173 }); | |
174 | |
175 test('lessThan', () { | |
176 shouldFail(10, lessThan(9), "Expected: a value less than <9> " | |
177 "but: was <10>"); | |
178 shouldPass(9, lessThan(10)); | |
179 }); | |
180 | |
181 test('lessThanOrEqualTo', () { | |
182 shouldPass(10, lessThanOrEqualTo(10)); | |
183 shouldFail(11, lessThanOrEqualTo(10), | |
184 "Expected: a value less than or equal to <10> but: was <11>"); | |
185 }); | |
186 | |
187 test('isZero', () { | |
188 shouldPass(0, isZero); | |
189 shouldFail(1, isZero, "Expected: a value equal to <0> but: was <1>"); | |
190 }); | |
191 | |
192 test('isNonZero', () { | |
193 shouldFail(0, isNonZero, "Expected: a value not equal to <0> " | |
194 "but: was <0>"); | |
195 shouldPass(1, isNonZero); | |
196 }); | |
197 | |
198 test('isPositive', () { | |
199 shouldFail(-1, isPositive, "Expected: a value positive <0> " | |
200 "but: was <-1>"); | |
201 shouldPass(0, isPositive); | |
202 shouldPass(1, isPositive); | |
203 }); | |
204 | |
205 test('isNegative', () { | |
206 shouldPass(-1, isNegative); | |
207 shouldFail(0, isNegative, "Expected: a value negative <0> but: was <0>"); | |
208 }); | |
209 | |
210 test('closeTo', () { | |
211 shouldPass(0, closeTo(0, 1)); | |
212 shouldPass(-1, closeTo(0, 1)); | |
213 shouldPass(1, closeTo(0, 1)); | |
214 shouldFail(1.001, closeTo(0, 1), | |
215 "Expected: a numeric value within <1> of <0> " | |
216 "but: <1.001> differed by <1.001>"); | |
217 shouldFail(-1.001, closeTo(0, 1), | |
218 "Expected: a numeric value within <1> of <0> " | |
219 "but: <-1.001> differed by <1.001>"); | |
220 }); | |
221 | |
222 test('inInclusiveRange', () { | |
223 shouldFail(-1, inInclusiveRange(0,2), | |
224 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
225 "but: was <-1>"); | |
226 shouldPass(0, inInclusiveRange(0,2)); | |
227 shouldPass(1, inInclusiveRange(0,2)); | |
228 shouldPass(2, inInclusiveRange(0,2)); | |
229 shouldFail(3, inInclusiveRange(0,2), | |
230 "Expected: be in range from 0 (inclusive) to 2 (inclusive) " | |
231 "but: was <3>"); | |
232 }); | |
233 | |
234 test('inExclusiveRange', () { | |
235 shouldFail(0, inExclusiveRange(0,2), | |
236 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
237 "but: was <0>"); | |
238 shouldPass(1, inExclusiveRange(0,2)); | |
239 shouldFail(2, inExclusiveRange(0,2), | |
240 "Expected: be in range from 0 (exclusive) to 2 (exclusive) " | |
241 "but: was <2>"); | |
242 }); | |
243 | |
244 test('inOpenClosedRange', () { | |
245 shouldFail(0, inOpenClosedRange(0,2), | |
246 "Expected: be in range from 0 (exclusive) to 2 (inclusive) " | |
247 "but: was <0>"); | |
248 shouldPass(1, inOpenClosedRange(0,2)); | |
249 shouldPass(2, inOpenClosedRange(0,2)); | |
250 }); | |
251 | |
252 test('inClosedOpenRange', () { | |
253 shouldPass(0, inClosedOpenRange(0,2)); | |
254 shouldPass(1, inClosedOpenRange(0,2)); | |
255 shouldFail(2, inClosedOpenRange(0,2), | |
256 "Expected: be in range from 0 (inclusive) to 2 (exclusive) " | |
257 "but: was <2>"); | |
258 }); | |
259 }); | |
260 | |
261 | |
262 group('String Matchers', () { | |
263 | |
264 test('isEmpty', () { | |
265 shouldPass('', isEmpty); | |
266 shouldFail(null, isEmpty, | |
267 "Expected: empty but: was <null>"); | |
268 shouldFail(0, isEmpty, | |
269 "Expected: empty but: was <0>"); | |
270 shouldFail('a', isEmpty, "Expected: empty but: was 'a'"); | |
271 }); | |
272 | |
273 test('equalsIgnoringCase', () { | |
274 shouldPass('hello', equalsIgnoringCase('HELLO')); | |
275 shouldFail('hi', equalsIgnoringCase('HELLO'), | |
276 "Expected: 'HELLO' ignoring case but: was 'hi'"); | |
277 }); | |
278 | |
279 test('equalsIgnoringWhitespace', () { | |
280 shouldPass(' hello world ', equalsIgnoringWhitespace('hello world')); | |
281 shouldFail(' helloworld ', equalsIgnoringWhitespace('hello world'), | |
282 "Expected: 'hello world' ignoring whitespace but: was 'helloworld'"); | |
283 }); | |
284 | |
285 test('startsWith', () { | |
286 shouldPass('hello', startsWith('')); | |
287 shouldPass('hello', startsWith('hell')); | |
288 shouldPass('hello', startsWith('hello')); | |
289 shouldFail('hello', startsWith('hello '), | |
290 "Expected: a string starting with 'hello ' but: was 'hello'"); | |
291 }); | |
292 | |
293 test('endsWith', () { | |
294 shouldPass('hello', endsWith('')); | |
295 shouldPass('hello', endsWith('lo')); | |
296 shouldPass('hello', endsWith('hello')); | |
297 shouldFail('hello', endsWith(' hello'), | |
298 "Expected: a string ending with ' hello' but: was 'hello'"); | |
299 }); | |
300 | |
301 test('contains', () { | |
302 shouldPass('hello', contains('')); | |
303 shouldPass('hello', contains('h')); | |
304 shouldPass('hello', contains('o')); | |
305 shouldPass('hello', contains('hell')); | |
306 shouldPass('hello', contains('hello')); | |
307 shouldFail('hello', contains(' '), | |
308 "Expected: contains ' ' but: was 'hello'"); | |
309 }); | |
310 | |
311 test('stringContainsInOrder', () { | |
312 shouldPass('goodbye cruel world', stringContainsInOrder([''])); | |
313 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye'])); | |
314 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel'])); | |
315 shouldPass('goodbye cruel world', stringContainsInOrder(['world'])); | |
316 shouldPass('goodbye cruel world', | |
317 stringContainsInOrder(['good', 'bye', 'world'])); | |
318 shouldPass('goodbye cruel world', | |
319 stringContainsInOrder(['goodbye', 'cruel'])); | |
320 shouldPass('goodbye cruel world', | |
321 stringContainsInOrder(['cruel', 'world'])); | |
322 shouldPass('goodbye cruel world', | |
323 stringContainsInOrder(['goodbye', 'cruel', 'world'])); | |
324 shouldFail('goodbye cruel world', | |
325 stringContainsInOrder(['goo', 'cruel', 'bye']), | |
326 "Expected: a string containing 'goo', 'cruel', 'bye' in order " | |
327 "but: was 'goodbye cruel world'"); | |
328 }); | |
329 | |
330 test('matches', () { | |
331 shouldPass('c0d', matches('[a-z][0-9][a-z]')); | |
332 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); | |
333 shouldFail('cOd', matches('[a-z][0-9][a-z]'), | |
334 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'"); | |
335 }); | |
336 }); | |
337 | |
338 group('Collection Matchers', () { | |
339 | |
340 test('isEmpty', () { | |
341 shouldPass([], isEmpty); | |
342 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>"); | |
343 }); | |
344 | |
345 var d = [1, 2]; | |
346 | |
347 test('contains', () { | |
348 shouldPass(d, contains(1)); | |
349 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>"); | |
350 }); | |
351 | |
352 var e = [1, 1, 1]; | |
353 | |
354 test('everyElement', () { | |
355 shouldFail(d, everyElement(1), | |
356 "Expected: every element <1> but: was <[1, 2]>"); | |
357 shouldPass(e, everyElement(1)); | |
358 }); | |
359 | |
360 test('someElement', () { | |
361 shouldPass(d, someElement(2)); | |
362 shouldFail(e, someElement(2), | |
363 "Expected: some element <2> but: was <[1, 1, 1]>"); | |
364 }); | |
365 | |
366 test('orderedEquals', () { | |
367 shouldPass(d, orderedEquals([1, 2])); | |
368 shouldFail(d, orderedEquals([2, 1]), | |
369 "Expected: equals <[2, 1]> ordered " | |
370 "but: mismatch at position 0"); | |
371 }); | |
372 | |
373 test('unorderedEquals', () { | |
374 shouldPass(d, unorderedEquals([2, 1])); | |
375 shouldFail(d, unorderedEquals([1]), | |
376 "Expected: equals <[1]> unordered " | |
377 "but: has too many elements (2 > 1)"); | |
378 shouldFail(d, unorderedEquals([3, 2, 1]), | |
379 "Expected: equals <[3, 2, 1]> unordered " | |
380 "but: has too few elements (2 < 3)"); | |
381 shouldFail(d, unorderedEquals([3, 1]), | |
382 "Expected: equals <[3, 1]> unordered " | |
383 "but: has no match for element 3 at position 0"); | |
384 }); | |
385 }); | |
386 | |
387 group('Map Matchers', () { | |
388 | |
389 test('isEmpty', () { | |
390 shouldPass({}, isEmpty); | |
391 shouldPass(a, isEmpty); | |
392 a['foo'] = 'bar'; | |
393 shouldFail(a, isEmpty, "Expected: empty but: was <{foo: bar}>"); | |
394 }); | |
395 | |
396 test('contains', () { | |
397 shouldPass(a, contains('foo')); | |
398 shouldFail(b, contains('foo'), | |
399 "Expected: contains 'foo' but: was <{}>"); | |
400 shouldFail(10, contains('foo'), | |
401 "Expected: contains 'foo' but: was <10>"); | |
402 }); | |
403 | |
404 test('containsValue', () { | |
405 shouldPass(a, containsValue('bar')); | |
406 shouldFail(a, containsValue('ba'), | |
407 "Expected: contains value 'ba' but: was <{foo: bar}>"); | |
408 }); | |
409 | |
410 test('containsPair', () { | |
411 shouldPass(a, containsPair('foo', 'bar')); | |
412 shouldFail(a, containsPair('foo', 'ba'), | |
413 "Expected: contains pair 'foo' => 'ba' " | |
414 "but: contains key 'foo' but with value "); | |
415 shouldFail(a, containsPair('fo', 'bar'), | |
416 "Expected: contains pair 'fo' => 'bar' " | |
417 "but: <{foo: bar}>' doesn't contain key ''fo'"); | |
418 }); | |
419 | |
420 test('hasLength', () { | |
421 shouldPass(a, hasLength(1)); | |
422 shouldFail(b, hasLength(1), | |
423 "Expected: an object with length of <1> " | |
424 "but: was <{}> with length of <0>"); | |
425 }); | |
426 }); | |
427 | |
428 group('Operator Matchers', () { | |
429 | |
430 test('anyOf', () { | |
431 shouldFail(0, anyOf([equals(1), equals(2)]), | |
432 "Expected: (<1> or <2>) but: was <0>"); | |
433 shouldPass(1, anyOf([equals(1), equals(2)])); | |
434 }); | |
435 | |
436 test('allOf', () { | |
437 shouldPass(1, allOf([lessThan(10), greaterThan(0)])); | |
438 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), | |
439 "Expected: (a value less than <10> and a value greater than <0>) " | |
440 "but: a value greater than <0> was <-1>"); | |
441 }); | |
442 }); | |
443 } | |
444 | |
445 | |
OLD | NEW |