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

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
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
Bob Nystrom 2012/05/30 23:23:51 Yay unit tests for unittest! This is awesome.
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library('unittestTest');
Bob Nystrom 2012/05/30 23:23:51 unittest_test
6
7 #import('../../lib/unittest/unittest.dart');
8
9
10 foo() {}
Bob Nystrom 2012/05/30 23:23:51 "doesNotThrow"
11 bar() { throw 'X'; }
Bob Nystrom 2012/05/30 23:23:51 "doesThrow"
12
13 int errorCount;
14 String errorString;
15 var myhandler;
Bob Nystrom 2012/05/30 23:23:51 myHandler Or, ideally, a more meaningful name.
16
17 class MyFailureHandler implements IFailureHandler {
18 DefaultErrorFormatter f = null;
19
20 void fail(String reason) {
21 ++errorCount;
22 errorString = reason;
23 }
24 void failMatch(actual, IMatcher matcher, String reason) {
25 if (f == null)
26 f = new DefaultErrorFormatter();
Bob Nystrom 2012/05/30 23:23:51 Put all on one line or use {}.
27 fail(f.format(actual, matcher, reason));
28 }
29 }
30
31 void shouldFail(var value, IMatcher matcher, String reason) {
32 errorCount = 0;
33 errorString = '';
34 configureExpectHandler(myhandler);
35 expect(value, matcher);
36 configureExpectHandler(null);
37 expect(errorCount, equals(1));
38 expect(errorString, equalToIgnoringWhitespace(reason));
39 }
40
41 void shouldPass(var value, IMatcher matcher) {
42 errorCount = 0;
43 errorString = '';
44 configureExpectHandler(myhandler);
45 expect(value, matcher);
46 configureExpectHandler(null);
47 expect(errorCount, equals(0));
48 }
49
50 void main() {
51
52 myhandler = new MyFailureHandler();
53
54 // Core matchers
Bob Nystrom 2012/05/30 23:23:51 Instead of comments, use group() and test(). That
gram 2012/06/01 22:21:59 Done.
55
56 // isTrue
57 shouldPass(true, isTrue());
58 shouldFail(false, isTrue(), "Expected: true but: was <false>");
59
60 // isFalse
61 shouldPass(false, isFalse());
62 shouldFail(true, isFalse(), "Expected: not true but: was <true>");
63
64 // isNull
65 shouldPass(null, isNull());
66 shouldFail(false, isNull(), "Expected: null but: was <false>");
67
68 // isNotNull
69 shouldPass(false, isNotNull());
70 shouldFail(null, isNotNull(), "Expected: not null but: was <null>");
71
72 var a = new Map();
73 var b = new Map();
74
75 // same
76 shouldPass(a, same(a));
77 shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>");
78
79 // equals - only for scalars
80 shouldPass(a, equals(a));
81 shouldFail(a, equals(b), "Expected: <{}> but: was <{}>");
82
83 // anything
84 shouldPass(a, anything());
85 shouldFail(a, isNot(anything()), "Expected: not anything but: was <{}>");
86
87 // throwsException
88 shouldFail(foo, throwsException(),
89 "Expected: throws an exception but: no exception");
Bob Nystrom 2012/05/30 23:23:51 Indent another 2, here and elsewhere.
gram 2012/06/01 22:21:59 Done.
90 shouldPass(bar, throwsException());
91
92 // throwsExceptionWhich
93 shouldPass(bar, throwsExceptionWhich(hasString('X')));
94 shouldFail(bar, throwsExceptionWhich(hasString('Y')),
95 "Expected: throws an exception which needs to match with toString() "
96 "value 'Y' but: exception does not match with toString() value 'Y'");
97
98 // returnsNormally
99 shouldPass(foo, returnsNormally());
100 shouldFail(bar, returnsNormally(),
101 "Expected: return normally but: threw exception");
102
103 // isInstanceOf
104 shouldFail(0, isInstanceOf(new Type<String>('String')),
105 "Expected: an instance of String but: was <0>");
106 shouldPass('cow', isInstanceOf(new Type<String>('String')));
107
108 // hasLength
109 var c = new List();
110 shouldPass(c, hasLength());
111 shouldPass(a, hasLength());
112 shouldPass('a', hasLength());
113 shouldFail(0, hasLength(),
114 "Expected: an object with length of anything "
115 "but: was <0> has no length property");
116
117 c.add(0);
118 shouldPass(c, hasLength(1));
119 shouldFail(c, hasLength(2),
120 "Expected: an object with length of <2> "
121 "but: was <[0]> with length of <1>");
122
123 c.add(0);
124 shouldFail(c, hasLength(1),
125 "Expected: an object with length of <1> "
126 "but: was <[0, 0]> with length of <2>");
127 shouldPass(c, hasLength(2));
128
129 // hasString
130 shouldPass(10, hasString('10'));
131 shouldFail(10, hasString('11'),
132 "Expected: with toString() value '11' but: was <10>");
133
134 // Numeric Matchers
135
136 //greaterThan
137 shouldPass(10, greaterThan(9));
138 shouldFail(9, greaterThan(10),
139 "Expected: a value greater than <10> but: was <9>");
140
141 // greaterThanOrEqualTo
142 shouldPass(10, greaterThanOrEqualTo(10));
143 shouldFail(9, greaterThanOrEqualTo(10),
144 "Expected: a value greater than or equal to <10> but: was <9>");
145
146 // lessThan
147 shouldFail(10, lessThan(9), "Expected: a value less than <9> but: was <10>");
148 shouldPass(9, lessThan(10));
149
150 // lessThanOrEqualTo
151 shouldPass(10, lessThanOrEqualTo(10));
152 shouldFail(11, lessThanOrEqualTo(10),
153 "Expected: a value less than or equal to <10> but: was <11>");
154
155 // isZero
156 shouldPass(0, isZero());
157 shouldFail(1, isZero(), "Expected: a value equal to <0> but: was <1>");
158
159 // isNonZero
160 shouldFail(0, isNonZero(), "Expected: a value not equal to <0> but: was <0>");
161 shouldPass(1, isNonZero());
162
163 // isPositive
164 shouldFail(-1, isPositive(), "Expected: a value positive <0> but: was <-1>");
165 shouldPass(0, isPositive());
166 shouldPass(1, isPositive());
167
168 // isNegative
169 shouldPass(-1, isNegative());
170 shouldFail(0, isNegative(), "Expected: a value negative <0> but: was <0>");
171
172 // closeTo
173 shouldPass(0, closeTo(0, 1));
174 shouldPass(-1, closeTo(0, 1));
175 shouldPass(1, closeTo(0, 1));
176 shouldFail(1.001, closeTo(0, 1),
177 "Expected: a numeric value within <1> of <0> "
178 "but: <1.001> differed by <1.001>");
179 shouldFail(-1.001, closeTo(0, 1),
180 "Expected: a numeric value within <1> of <0> "
181 "but: <-1.001> differed by <1.001>");
182
183 // inInclusiveRange
184 shouldFail(-1, inInclusiveRange(0,2),
185 "Expected: be in range from 0 (inclusive) to 2 (inclusive) but: was <-1>");
186 shouldPass(0, inInclusiveRange(0,2));
187 shouldPass(1, inInclusiveRange(0,2));
188 shouldPass(2, inInclusiveRange(0,2));
189 shouldFail(3, inInclusiveRange(0,2),
190 "Expected: be in range from 0 (inclusive) to 2 (inclusive) but: was <3>");
191
192 // inExclusiveRange
193 shouldFail(0, inExclusiveRange(0,2),
194 "Expected: be in range from 0 (exclusive) to 2 (exclusive) but: was <0>");
195 shouldPass(1, inExclusiveRange(0,2));
196 shouldFail(2, inExclusiveRange(0,2),
197 "Expected: be in range from 0 (exclusive) to 2 (exclusive) but: was <2>");
198
199 // inOpenClosedRange
200 shouldFail(0, inOpenClosedRange(0,2),
201 "Expected: be in range from 0 (exclusive) to 2 (inclusive) but: was <0>");
202 shouldPass(1, inOpenClosedRange(0,2));
203 shouldPass(2, inOpenClosedRange(0,2));
204
205 // inClosedOpenRange
206 shouldPass(0, inClosedOpenRange(0,2));
207 shouldPass(1, inClosedOpenRange(0,2));
208 shouldFail(2, inClosedOpenRange(0,2),
209 "Expected: be in range from 0 (inclusive) to 2 (exclusive) but: was <2>");
210
211 // String Matchers
212
213 // isEmptyString
214 shouldPass('', isEmptyString());
215 shouldFail(null, isEmptyString(),
216 "Expected: empty string but: <null> not a string");
217 shouldFail(0, isEmptyString(),
218 "Expected: empty string but: <0> not a string");
219 shouldFail([], isEmptyString(),
220 "Expected: empty string but: <[]> not a string");
221 shouldFail({}, isEmptyString(),
222 "Expected: empty string but: <{}> not a string");
223 shouldFail('a', isEmptyString(), "Expected: empty string but: was 'a'");
224
225 // isInterpolated
226 shouldPass(c, isInterpolated('[0, 0]'));
227 shouldFail(c, isInterpolated('[0, 1]'),
228 "Expected: '[0, 1]' interpolated but: was <[0, 0]>");
229
230 // equalToIgnoringCase
231 shouldPass('hello', equalToIgnoringCase('HELLO'));
232 shouldFail('hi', equalToIgnoringCase('HELLO'),
233 "Expected: 'HELLO' ignoring case but: was 'hi'");
234
235 // equalToIgnoringWhitespace
236 shouldPass(' hello world ', equalToIgnoringWhitespace('hello world'));
237 shouldFail(' helloworld ', equalToIgnoringWhitespace('hello world'),
238 "Expected: 'hello world' ignoring whitespace but: was 'helloworld'");
239
240 // startsWith
241 shouldPass('hello', startsWith(''));
242 shouldPass('hello', startsWith('hell'));
243 shouldPass('hello', startsWith('hello'));
244 shouldFail('hello', startsWith('hello '),
245 "Expected: a string starting with 'hello ' but: was 'hello'");
246
247 // endsWith
248 shouldPass('hello', endsWith(''));
249 shouldPass('hello', endsWith('lo'));
250 shouldPass('hello', endsWith('hello'));
251 shouldFail('hello', endsWith(' hello'),
252 "Expected: a string ending with ' hello' but: was 'hello'");
253
254 // containsString
255 shouldPass('hello', containsString(''));
256 shouldPass('hello', containsString('h'));
257 shouldPass('hello', containsString('o'));
258 shouldPass('hello', containsString('hell'));
259 shouldPass('hello', containsString('hello'));
260 shouldFail('hello', containsString(' '),
261 "Expected: a string containing ' ' but: was 'hello'");
262
263 // stringContainsInOrder
264 shouldPass('goodbye cruel world', stringContainsInOrder(['']));
265 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye']));
266 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel']));
267 shouldPass('goodbye cruel world', stringContainsInOrder(['world']));
268 shouldPass('goodbye cruel world',
269 stringContainsInOrder(['good', 'bye', 'world']));
270 shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye', 'cruel'])) ;
Bob Nystrom 2012/05/30 23:23:51 Long line.
gram 2012/06/01 22:21:59 Done.
271 shouldPass('goodbye cruel world', stringContainsInOrder(['cruel', 'world']));
272 shouldPass('goodbye cruel world',
273 stringContainsInOrder(['goodbye', 'cruel', 'world']));
274 shouldFail('goodbye cruel world',
275 stringContainsInOrder(['goo', 'cruel', 'bye']),
276 "Expected: a string containing 'goo', 'cruel', 'bye' in order "
277 "but: was 'goodbye cruel world'");
278
279 // matches
280 shouldPass('c0d', matches('[a-z][0-9][a-z]'));
281 shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]')));
282 shouldFail('cOd', matches('[a-z][0-9][a-z]'),
283 "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'");
284
285 // Collection Matchers
286
287 // emptyCollection
288 shouldPass([], emptyCollection());
289 shouldFail([1], emptyCollection(), "Expected: be empty but: was <[1]>");
290
291 var d = [ 1, 2 ];
Bob Nystrom 2012/05/30 23:23:51 No spaces after [ or before ]
gram 2012/06/01 22:21:59 Done.
292
293 // contains
294 shouldPass(d, contains(1));
295 shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>");
296
297 var e = [1, 1, 1];
298
299 // everyElement
300 shouldFail(d, everyElement(1),
301 "Expected: every element <1> but: was <[1, 2]>");
302 shouldPass(e, everyElement(1));
303
304 // someElement
305 shouldPass(d, someElement(2));
306 shouldFail(e, someElement(2),
307 "Expected: some element <2> but: was <[1, 1, 1]>");
308
309 // orderedEquals
310 shouldPass(d, orderedEquals([1, 2]));
311 shouldFail(d, orderedEquals([2, 1]),
312 "Expected: equals <[2, 1]> ordered but: was <[1, 2]>");
313
314 // unorderedEquals
315 shouldPass(d, unorderedEquals([2, 1]));
316 shouldFail(d, unorderedEquals([1]),
317 "Expected: equals <{1}> unordered but: was <[1, 2]>");
318 shouldFail(d, unorderedEquals([3, 2, 1]),
319 "Expected: equals <{1, 2, 3}> unordered but: was <[1, 2]>");
320
321 // Map Matchers
322
323 // emptyMap
324
325 shouldPass({}, emptyMap());
326 shouldPass(a, emptyMap());
327 a['foo'] = 'bar';
328 shouldFail(a, emptyMap(), "Expected: empty but: was <{foo: bar}>");
329
330 // mapContainsKey
331 shouldPass(a, mapContainsKey('foo'));
332 shouldFail(b, mapContainsKey('foo'),
333 "Expected: contains key 'foo' but: was <{}>");
334 shouldFail(10, mapContainsKey('foo'),
335 "Expected: contains key 'foo' but: <10> not a map");
336
337 // mapContainsValue
338 shouldPass(a, mapContainsValue('bar'));
339 shouldFail(a, mapContainsValue('ba'),
340 "Expected: contains value 'ba' but: was <{foo: bar}>");
341
342 // mapContains
343 shouldPass(a, mapContains('foo', 'bar'));
344 shouldFail(a, mapContains('foo', 'ba'),
345 "Expected: contains pair 'foo' => 'ba' "
346 "but: contains key 'foo' but with value ");
347 shouldFail(a, mapContains('fo', 'bar'),
348 "Expected: contains pair 'fo' => 'bar' "
349 "but: <{foo: bar}>' doesn't contain key ''fo'");
350
351 // mapLength
352 shouldPass(a, mapLength(1));
353 shouldFail(b, mapLength(1), "Expected: map length <1> but: was <{}>");
354
355 // Operator Matchers
356
357 // anyOf
358 shouldFail(0, anyOf([equals(1), equals(2)]),
359 "Expected: (<1> or <2>) but: was <0>");
360 shouldPass(1, anyOf([equals(1), equals(2)]));
361
362 // allOf
363 shouldPass(1, allOf([lessThan(10), greaterThan(0)]));
364 shouldFail(-1, allOf([lessThan(10), greaterThan(0)]),
365 "Expected: (a value less than <10> and a value greater than <0>) "
366 "but: a value greater than <0> was <-1>");
367 }
368
369
OLDNEW
« tests/utils/dartdoc_test.dart ('K') | « tests/utils/markdown_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698