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

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

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

Powered by Google App Engine
This is Rietveld 408576698