Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Side by Side Diff: tests/utils/unittest_test.dart

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

Powered by Google App Engine
This is Rietveld 408576698