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

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

Powered by Google App Engine
This is Rietveld 408576698