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 // Use the type-specialized versions when appropriate to give better |
| 267 // error messages. |
266 if (_value is String && expected is String) { | 268 if (_value is String && expected is String) { |
267 Expect.stringEquals(expected, _value); | 269 Expect.stringEquals(expected, _value); |
| 270 } else if (_value is Map && expected is Map) { |
| 271 Expect.mapEquals(expected, _value); |
| 272 } else if (_value is Set && expected is Set) { |
| 273 Expect.setEquals(expected, _value); |
268 } else { | 274 } else { |
269 Expect.equals(expected, _value); | 275 Expect.equals(expected, _value); |
270 } | 276 } |
271 } | 277 } |
272 | 278 |
273 /** | 279 /** |
274 * Asserts that the difference between [expected] and the value is within | 280 * Asserts that the difference between [expected] and the value is within |
275 * [tolerance]. If no tolerance is given, it is assumed to be the value 4 | 281 * [tolerance]. If no tolerance is given, it is assumed to be the value 4 |
276 * significant digits smaller than the expected value. | 282 * significant digits smaller than the expected value. |
277 */ | 283 */ |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 /** Error or failure message. */ | 337 /** Error or failure message. */ |
332 String message = ''; | 338 String message = ''; |
333 | 339 |
334 /** | 340 /** |
335 * One of [_PASS], [_FAIL], or [_ERROR] or [null] if the test hasn't run yet. | 341 * One of [_PASS], [_FAIL], or [_ERROR] or [null] if the test hasn't run yet. |
336 */ | 342 */ |
337 String result; | 343 String result; |
338 | 344 |
339 /** Stack trace associated with this test, or null if it succeeded. */ | 345 /** Stack trace associated with this test, or null if it succeeded. */ |
340 String stackTrace; | 346 String stackTrace; |
341 | 347 |
342 Date startTime; | 348 Date startTime; |
343 | 349 |
344 Duration runningTime; | 350 Duration runningTime; |
345 | 351 |
346 TestCase(this.id, this.description, this.test, this.callbacks); | 352 TestCase(this.id, this.description, this.test, this.callbacks); |
347 | 353 |
348 bool get isComplete() => result != null; | 354 bool get isComplete() => result != null; |
349 | 355 |
350 void pass() { | 356 void pass() { |
351 result = _PASS; | 357 result = _PASS; |
352 } | 358 } |
353 | 359 |
354 void fail(String message_, String stackTrace_) { | 360 void fail(String message_, String stackTrace_) { |
355 result = _FAIL; | 361 result = _FAIL; |
356 this.message = message_; | 362 this.message = message_; |
357 this.stackTrace = stackTrace_; | 363 this.stackTrace = stackTrace_; |
358 } | 364 } |
359 | 365 |
360 void error(String message_, String stackTrace_) { | 366 void error(String message_, String stackTrace_) { |
361 result = _ERROR; | 367 result = _ERROR; |
362 this.message = message_; | 368 this.message = message_; |
363 this.stackTrace = stackTrace_; | 369 this.stackTrace = stackTrace_; |
364 } | 370 } |
365 } | 371 } |
366 | 372 |
367 typedef void TestFunction(); | 373 typedef void TestFunction(); |
OLD | NEW |