OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Description text of the current test group. If multiple groups are nested, | 6 * Description text of the current test group. If multiple groups are nested, |
7 * this will contain all of their text concatenated. | 7 * this will contain all of their text concatenated. |
8 */ | 8 */ |
9 String _currentGroup = ''; | 9 String _currentGroup = ''; |
10 | 10 |
11 /** Tests executed in this suite. */ | 11 /** Tests executed in this suite. */ |
12 List<TestCase> _tests; | 12 List<TestCase> _tests; |
13 | 13 |
14 /** | 14 /** |
15 * Callback used to run tests. Entrypoints can replace this with their own | 15 * Callback used to run tests. Entrypoints can replace this with their own |
16 * if they want. | 16 * if they want. |
17 */ | 17 */ |
18 Function _testRunner; | 18 Function _testRunner; |
19 | 19 |
20 /** Whether this is run within dartium layout tests. */ | 20 /** Whether this is run within dartium layout tests. */ |
21 bool _isLayoutTest = false; | 21 bool _isLayoutTest = false; |
22 | 22 |
23 /** Current test being executed. */ | 23 /** Current test being executed. */ |
24 int _currentTest = 0; | 24 int _currentTest = 0; |
25 | 25 |
26 /** Total number of callbacks that have been executed in the current test. */ | 26 /** Total number of callbacks that have been executed in the current test. */ |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 int i = 0; | 92 int i = 0; |
93 void invokeNext() { | 93 void invokeNext() { |
94 closures[i](); | 94 closures[i](); |
95 i++; | 95 i++; |
96 if (i < length) { | 96 if (i < length) { |
97 window.setTimeout(invokeNext, 0); | 97 window.setTimeout(invokeNext, 0); |
98 } | 98 } |
99 } | 99 } |
100 window.setTimeout(invokeNext, 0); | 100 window.setTimeout(invokeNext, 0); |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 /** | 104 /** |
105 * Creates a new named group of tests. Calls to group() or test() within the | 105 * Creates a new named group of tests. Calls to group() or test() within the |
106 * body of the function passed to this will inherit this group's description. | 106 * body of the function passed to this will inherit this group's description. |
107 */ | 107 */ |
108 void group(String description, void body()) { | 108 void group(String description, void body()) { |
109 _ensureInitialized(); | 109 _ensureInitialized(); |
110 | 110 |
111 // Concatenate the new group. | 111 // Concatenate the new group. |
112 final oldGroup = _currentGroup; | 112 final oldGroup = _currentGroup; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 /** | 237 /** |
238 * Lazily initializes the test library if not already initialized. | 238 * Lazily initializes the test library if not already initialized. |
239 */ | 239 */ |
240 _ensureInitialized() { | 240 _ensureInitialized() { |
241 if (_state != _UNINITIALIZED) return; | 241 if (_state != _UNINITIALIZED) return; |
242 | 242 |
243 _tests = <TestCase>[]; | 243 _tests = <TestCase>[]; |
244 _currentGroup = ''; | 244 _currentGroup = ''; |
245 _state = _READY; | 245 _state = _READY; |
246 _testRunner = _nextBatch; | 246 _testRunner = _nextBatch; |
247 | 247 |
248 _platformInitialize(); | 248 _platformInitialize(); |
249 | 249 |
250 // Immediately queue the suite up. It will run after a timeout (i.e. after | 250 // Immediately queue the suite up. It will run after a timeout (i.e. after |
251 // main() has returned). | 251 // main() has returned). |
252 _platformDefer(_runTests); | 252 _platformDefer(_runTests); |
253 } | 253 } |
254 | 254 |
255 /** | 255 /** |
256 * Wraps an value and provides an "==" operator that can be used to verify that | 256 * Wraps an value and provides an "==" operator that can be used to verify that |
257 * the value matches a given expectation. | 257 * the value matches a given expectation. |
258 */ | 258 */ |
259 class Expectation { | 259 class Expectation { |
260 final _value; | 260 final _value; |
261 | 261 |
262 Expectation(this._value); | 262 Expectation(this._value); |
263 | 263 |
264 /** Asserts that the value is equivalent to [expected]. */ | 264 /** Asserts that the value is equivalent to [expected]. */ |
265 void equals(expected) { | 265 void equals(expected) { |
266 // If comparing two maps, see that their keys and values match. | |
Siggi Cherem (dart-lang)
2012/02/09 02:18:23
seems like you are not synced up to latest! (my ch
Bob Nystrom
2012/02/10 00:31:47
Done.
| |
267 if (_value is Map && expected is Map) { | |
Siggi Cherem (dart-lang)
2012/02/09 02:18:23
Let's add this as mapEquals in corelib/src/expect.
Bob Nystrom
2012/02/10 00:31:47
Done.
| |
268 // Make sure all of the values are present in both and match. | |
269 for (final key in expected.getKeys()) { | |
270 if (!_value.containsKey(key)) { | |
271 Expect.fail("Expect map to contain key '$key'."); | |
272 } | |
273 | |
274 Expect.equals(expected[key], _value[key]); | |
275 } | |
276 | |
277 // Make sure the actual map doesn't have any extra keys. | |
278 for (final key in _value.getKeys()) { | |
279 if (!expected.containsKey(key)) { | |
280 Expect.fail("Actual map contains unexpected key '$key'."); | |
281 } | |
282 } | |
283 | |
284 // If we got here, they are equal. | |
285 return; | |
286 } | |
287 | |
266 Expect.equals(expected, _value); | 288 Expect.equals(expected, _value); |
267 } | 289 } |
268 | 290 |
269 /** | 291 /** |
270 * Asserts that the difference between [expected] and the value is within | 292 * Asserts that the difference between [expected] and the value is within |
271 * [tolerance]. If no tolerance is given, it is assumed to be the value 4 | 293 * [tolerance]. If no tolerance is given, it is assumed to be the value 4 |
272 * significant digits smaller than the expected value. | 294 * significant digits smaller than the expected value. |
273 */ | 295 */ |
274 void approxEquals(num expected, | 296 void approxEquals(num expected, |
275 [num tolerance = null, String reason = null]) { | 297 [num tolerance = null, String reason = null]) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 /** Error or failure message. */ | 349 /** Error or failure message. */ |
328 String message = ''; | 350 String message = ''; |
329 | 351 |
330 /** | 352 /** |
331 * One of [_PASS], [_FAIL], or [_ERROR] or [null] if the test hasn't run yet. | 353 * One of [_PASS], [_FAIL], or [_ERROR] or [null] if the test hasn't run yet. |
332 */ | 354 */ |
333 String result; | 355 String result; |
334 | 356 |
335 /** Stack trace associated with this test, or null if it succeeded. */ | 357 /** Stack trace associated with this test, or null if it succeeded. */ |
336 String stackTrace; | 358 String stackTrace; |
337 | 359 |
338 Date startTime; | 360 Date startTime; |
339 | 361 |
340 Duration runningTime; | 362 Duration runningTime; |
341 | 363 |
342 TestCase(this.id, this.description, this.test, this.callbacks); | 364 TestCase(this.id, this.description, this.test, this.callbacks); |
343 | 365 |
344 bool get isComplete() => result != null; | 366 bool get isComplete() => result != null; |
345 | 367 |
346 void pass() { | 368 void pass() { |
347 result = _PASS; | 369 result = _PASS; |
348 } | 370 } |
349 | 371 |
350 void fail(String message_, String stackTrace_) { | 372 void fail(String message_, String stackTrace_) { |
351 result = _FAIL; | 373 result = _FAIL; |
352 this.message = message_; | 374 this.message = message_; |
353 this.stackTrace = stackTrace_; | 375 this.stackTrace = stackTrace_; |
354 } | 376 } |
355 | 377 |
356 void error(String message_, String stackTrace_) { | 378 void error(String message_, String stackTrace_) { |
357 result = _ERROR; | 379 result = _ERROR; |
358 this.message = message_; | 380 this.message = message_; |
359 this.stackTrace = stackTrace_; | 381 this.stackTrace = stackTrace_; |
360 } | 382 } |
361 } | 383 } |
362 | 384 |
363 typedef void TestFunction(); | 385 typedef void TestFunction(); |
OLD | NEW |