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

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

Issue 10441104: New expectation functions plus convert old tests to use these. (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/utils/markdown_test.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, String 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 test('isInstanceOf', () {
126 shouldFail(0, new isInstanceOf<String>('String'),
127 "Expected: an instance of String but: was <0>");
128 shouldPass('cow', new isInstanceOf<String>('String'));
129 });
130
131 test('hasLength', () {
132 shouldPass(c, hasLength(0));
133 shouldPass(a, hasLength(0));
134 shouldPass('a', hasLength(1));
135 shouldFail(0, hasLength(0), new PrefixMatcher(
136 "Expected: an object with length of <0> "
137 "but: was <0> has no length property"));
138
139 c.add(0);
140 shouldPass(c, hasLength(1));
141 shouldFail(c, hasLength(2),
142 "Expected: an object with length of <2> "
143 "but: was <[0]> with length of <1>");
144
145 c.add(0);
146 shouldFail(c, hasLength(1),
147 "Expected: an object with length of <1> "
148 "but: was <[0, 0]> with length of <2>");
149 shouldPass(c, hasLength(2));
150 });
151 });
152
153 group('Numeric Matchers', () {
154
155 test('greaterThan', () {
156 shouldPass(10, greaterThan(9));
157 shouldFail(9, greaterThan(10),
158 "Expected: a value greater than <10> but: was <9>");
159 });
160
161 test('greaterThanOrEqualTo', () {
162 shouldPass(10, greaterThanOrEqualTo(10));
163 shouldFail(9, greaterThanOrEqualTo(10),
164 "Expected: a value greater than or equal to <10> but: was <9>");
165 });
166
167 test('lessThan', () {
168 shouldFail(10, lessThan(9), "Expected: a value less than <9> "
169 "but: was <10>");
170 shouldPass(9, lessThan(10));
171 });
172
173 test('lessThanOrEqualTo', () {
174 shouldPass(10, lessThanOrEqualTo(10));
175 shouldFail(11, lessThanOrEqualTo(10),
176 "Expected: a value less than or equal to <10> but: was <11>");
177 });
178
179 test('isZero', () {
180 shouldPass(0, isZero);
181 shouldFail(1, isZero, "Expected: a value equal to <0> but: was <1>");
182 });
183
184 test('isNonZero', () {
185 shouldFail(0, isNonZero, "Expected: a value not equal to <0> "
186 "but: was <0>");
187 shouldPass(1, isNonZero);
188 });
189
190 test('isPositive', () {
191 shouldFail(-1, isPositive, "Expected: a positive value "
192 "but: was <-1>");
193 shouldFail(0, isPositive, "Expected: a positive value "
194 "but: was <0>");
195 shouldPass(1, isPositive);
196 });
197
198 test('isNegative', () {
199 shouldPass(-1, isNegative);
200 shouldFail(0, isNegative,
201 "Expected: a negative value but: was <0>");
202 });
203
204 test('isNonPositive', () {
205 shouldPass(-1, isNonPositive);
206 shouldPass(0, isNonPositive);
207 shouldFail(1, isNonPositive,
208 "Expected: a non-positive value but: was <1>");
209 });
210
211 test('isNonNegative', () {
212 shouldPass(1, isNonNegative);
213 shouldPass(0, isNonNegative);
214 shouldFail(-1, isNonNegative,
215 "Expected: a non-negative value but: was <-1>");
216 });
217
218 test('closeTo', () {
219 shouldPass(0, closeTo(0, 1));
220 shouldPass(-1, closeTo(0, 1));
221 shouldPass(1, closeTo(0, 1));
222 shouldFail(1.001, closeTo(0, 1),
223 "Expected: a numeric value within <1> of <0> "
224 "but: <1.001> differed by <1.001>");
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 });
229
230 test('inInclusiveRange', () {
231 shouldFail(-1, inInclusiveRange(0,2),
232 "Expected: be in range from 0 (inclusive) to 2 (inclusive) "
233 "but: was <-1>");
234 shouldPass(0, inInclusiveRange(0,2));
235 shouldPass(1, inInclusiveRange(0,2));
236 shouldPass(2, inInclusiveRange(0,2));
237 shouldFail(3, inInclusiveRange(0,2),
238 "Expected: be in range from 0 (inclusive) to 2 (inclusive) "
239 "but: was <3>");
240 });
241
242 test('inExclusiveRange', () {
243 shouldFail(0, inExclusiveRange(0,2),
244 "Expected: be in range from 0 (exclusive) to 2 (exclusive) "
245 "but: was <0>");
246 shouldPass(1, inExclusiveRange(0,2));
247 shouldFail(2, inExclusiveRange(0,2),
248 "Expected: be in range from 0 (exclusive) to 2 (exclusive) "
249 "but: was <2>");
250 });
251
252 test('inOpenClosedRange', () {
253 shouldFail(0, inOpenClosedRange(0,2),
254 "Expected: be in range from 0 (exclusive) to 2 (inclusive) "
255 "but: was <0>");
256 shouldPass(1, inOpenClosedRange(0,2));
257 shouldPass(2, inOpenClosedRange(0,2));
258 });
259
260 test('inClosedOpenRange', () {
261 shouldPass(0, inClosedOpenRange(0,2));
262 shouldPass(1, inClosedOpenRange(0,2));
263 shouldFail(2, inClosedOpenRange(0,2),
264 "Expected: be in range from 0 (inclusive) to 2 (exclusive) "
265 "but: was <2>");
266 });
267 });
268
269
270 group('String Matchers', () {
271
272 test('isEmpty', () {
273 shouldPass('', isEmpty);
274 shouldFail(null, isEmpty,
275 "Expected: empty but: was <null>");
276 shouldFail(0, isEmpty,
277 "Expected: empty but: was <0>");
278 shouldFail('a', isEmpty, "Expected: empty but: was 'a'");
279 });
280
281 test('equalsIgnoringCase', () {
282 shouldPass('hello', equalsIgnoringCase('HELLO'));
283 shouldFail('hi', equalsIgnoringCase('HELLO'),
284 "Expected: 'HELLO' ignoring case but: was 'hi'");
285 });
286
287 test('equalsIgnoringWhitespace', () {
288 shouldPass(' hello world ', equalsIgnoringWhitespace('hello world'));
289 shouldFail(' helloworld ', equalsIgnoringWhitespace('hello world'),
290 "Expected: 'hello world' ignoring whitespace but: was 'helloworld'");
291 });
292
293 test('startsWith', () {
294 shouldPass('hello', startsWith(''));
295 shouldPass('hello', startsWith('hell'));
296 shouldPass('hello', startsWith('hello'));
297 shouldFail('hello', startsWith('hello '),
298 "Expected: a string starting with 'hello ' but: was 'hello'");
299 });
300
301 test('endsWith', () {
302 shouldPass('hello', endsWith(''));
303 shouldPass('hello', endsWith('lo'));
304 shouldPass('hello', endsWith('hello'));
305 shouldFail('hello', endsWith(' hello'),
306 "Expected: a string ending with ' hello' but: was 'hello'");
307 });
308
309 test('contains', () {
310 shouldPass('hello', contains(''));
311 shouldPass('hello', contains('h'));
312 shouldPass('hello', contains('o'));
313 shouldPass('hello', contains('hell'));
314 shouldPass('hello', contains('hello'));
315 shouldFail('hello', contains(' '),
316 "Expected: contains ' ' but: was 'hello'");
317 });
318
319 test('stringContainsInOrder', () {
320 shouldPass('goodbye cruel world', stringContainsInOrder(['']));
321 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye']));
322 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel']));
323 shouldPass('goodbye cruel world', stringContainsInOrder(['world']));
324 shouldPass('goodbye cruel world',
325 stringContainsInOrder(['good', 'bye', 'world']));
326 shouldPass('goodbye cruel world',
327 stringContainsInOrder(['goodbye', 'cruel']));
328 shouldPass('goodbye cruel world',
329 stringContainsInOrder(['cruel', 'world']));
330 shouldPass('goodbye cruel world',
331 stringContainsInOrder(['goodbye', 'cruel', 'world']));
332 shouldFail('goodbye cruel world',
333 stringContainsInOrder(['goo', 'cruel', 'bye']),
334 "Expected: a string containing 'goo', 'cruel', 'bye' in order "
335 "but: was 'goodbye cruel world'");
336 });
337
338 test('matches', () {
339 shouldPass('c0d', matches('[a-z][0-9][a-z]'));
340 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]')));
341 shouldFail('cOd', matches('[a-z][0-9][a-z]'),
342 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'");
343 });
344 });
345
346 group('Collection Matchers', () {
347
348 test('isEmpty', () {
349 shouldPass([], isEmpty);
350 shouldFail([1], isEmpty, "Expected: empty but: was <[1]>");
351 });
352
353 var d = [1, 2];
354
355 test('contains', () {
356 shouldPass(d, contains(1));
357 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>");
358 });
359
360 var e = [1, 1, 1];
361
362 test('everyElement', () {
363 shouldFail(d, everyElement(1),
364 "Expected: every element <1> but: was <[1, 2]>");
365 shouldPass(e, everyElement(1));
366 });
367
368 test('someElement', () {
369 shouldPass(d, someElement(2));
370 shouldFail(e, someElement(2),
371 "Expected: some element <2> but: was <[1, 1, 1]>");
372 });
373
374 test('orderedEquals', () {
375 shouldPass(d, orderedEquals([1, 2]));
376 shouldFail(d, orderedEquals([2, 1]),
377 "Expected: equals <[2, 1]> ordered "
378 "but: mismatch at position 0");
379 });
380
381 test('unorderedEquals', () {
382 shouldPass(d, unorderedEquals([2, 1]));
383 shouldFail(d, unorderedEquals([1]),
384 "Expected: equals <[1]> unordered "
385 "but: has too many elements (2 > 1)");
386 shouldFail(d, unorderedEquals([3, 2, 1]),
387 "Expected: equals <[3, 2, 1]> unordered "
388 "but: has too few elements (2 < 3)");
389 shouldFail(d, unorderedEquals([3, 1]),
390 "Expected: equals <[3, 1]> unordered "
391 "but: has no match for element 3 at position 0");
392 });
393 });
394
395 group('Map Matchers', () {
396
397 test('isEmpty', () {
398 shouldPass({}, isEmpty);
399 shouldPass(a, isEmpty);
400 a['foo'] = 'bar';
401 shouldFail(a, isEmpty, "Expected: empty but: was <{foo: bar}>");
402 });
403
404 test('contains', () {
405 shouldPass(a, contains('foo'));
406 shouldFail(b, contains('foo'),
407 "Expected: contains 'foo' but: was <{}>");
408 shouldFail(10, contains('foo'),
409 "Expected: contains 'foo' but: was <10>");
410 });
411
412 test('containsValue', () {
413 shouldPass(a, containsValue('bar'));
414 shouldFail(a, containsValue('ba'),
415 "Expected: contains value 'ba' but: was <{foo: bar}>");
416 });
417
418 test('containsPair', () {
419 shouldPass(a, containsPair('foo', 'bar'));
420 shouldFail(a, containsPair('foo', 'ba'),
421 "Expected: contains pair 'foo' => 'ba' "
422 "but: contains key 'foo' but with value ");
423 shouldFail(a, containsPair('fo', 'bar'),
424 "Expected: contains pair 'fo' => 'bar' "
425 "but: <{foo: bar}>' doesn't contain key ''fo'");
426 });
427
428 test('hasLength', () {
429 shouldPass(a, hasLength(1));
430 shouldFail(b, hasLength(1),
431 "Expected: an object with length of <1> "
432 "but: was <{}> with length of <0>");
433 });
434 });
435
436 group('Operator Matchers', () {
437
438 test('anyOf', () {
439 shouldFail(0, anyOf([equals(1), equals(2)]),
440 "Expected: (<1> or <2>) but: was <0>");
441 shouldPass(1, anyOf([equals(1), equals(2)]));
442 });
443
444 test('allOf', () {
445 shouldPass(1, allOf([lessThan(10), greaterThan(0)]));
446 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]),
447 "Expected: (a value less than <10> and a value greater than <0>) "
448 "but: a value greater than <0> was <-1>");
449 });
450 });
451 }
452
453
OLDNEW
« no previous file with comments | « tests/utils/markdown_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698