Chromium Code Reviews| 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 // TODO(rnystrom): This code is gradually moving from a Java/JUnit style to | 5 // TODO(rnystrom): This code is gradually moving from a Java/JUnit style to |
| 6 // something closer to JS/Jasmine. Eventually, the UnitTestSuite class can go | 6 // something closer to JS/Jasmine. Eventually, the UnitTestSuite class can go |
| 7 // away completely (or become private to this library) and the only exposed API | 7 // away completely (or become private to this library) and the only exposed API |
| 8 // will be group()/test()/expect(). Until then, both ways are supported, which | 8 // will be group()/test()/expect(). Until then, both ways are supported, which |
| 9 // is why things look a bit weird in here. | 9 // is why things look a bit weird in here. |
| 10 | 10 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 } | 89 } |
| 90 | 90 |
| 91 /** Subclasses should override this method to register tests. */ | 91 /** Subclasses should override this method to register tests. */ |
| 92 void setUpTestSuite() {} | 92 void setUpTestSuite() {} |
| 93 | 93 |
| 94 /** Enqueues a synchronous test. */ | 94 /** Enqueues a synchronous test. */ |
| 95 void addTest(TestFunction body) { | 95 void addTest(TestFunction body) { |
| 96 test(null, body); | 96 test(null, body); |
| 97 } | 97 } |
| 98 | 98 |
| 99 /** Adds the tests defined by the given TestSet to this suite. */ | |
| 100 void addTestSet(TestSet test) { | |
|
Siggi Cherem (dart-lang)
2011/10/26 00:51:33
bye bye! :)
| |
| 101 test._bindToSuite(this); | |
| 102 test.setup(); | |
| 103 } | |
| 104 | |
| 105 /** Adds the tests defined by the given TestSets to this suite. */ | |
| 106 void addTestSets(Iterable<TestSet> tests) { | |
| 107 for (TestSet test in tests) { | |
| 108 addTestSet(test); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 /** Enqueues an asynchronous test that waits for [callbacks] callbacks. */ | 99 /** Enqueues an asynchronous test that waits for [callbacks] callbacks. */ |
| 113 void addAsyncTest(TestFunction body, int callbacks) { | 100 void addAsyncTest(TestFunction body, int callbacks) { |
| 114 asyncTest(null, callbacks, body); | 101 asyncTest(null, callbacks, body); |
| 115 } | 102 } |
| 116 | 103 |
| 117 /** Runs all queued tests, one at a time. */ | 104 /** Runs all queued tests, one at a time. */ |
| 118 void runTests() { | 105 void runTests() { |
| 119 window.dynamic/*TODO(5389254)*/.postMessage('unittest-suite-start', '*'); | 106 window.dynamic/*TODO(5389254)*/.postMessage('unittest-suite-start', '*'); |
| 120 // Isolate.bind makes sure the closure runs in the same isolate (i.e. this | 107 // Isolate.bind makes sure the closure runs in the same isolate (i.e. this |
| 121 // one) where it has been created. | 108 // one) where it has been created. |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 | 348 |
| 362 /** | 349 /** |
| 363 * Wraps an value and provides an "==" operator that can be used to verify that | 350 * Wraps an value and provides an "==" operator that can be used to verify that |
| 364 * the value matches a given expectation. | 351 * the value matches a given expectation. |
| 365 */ | 352 */ |
| 366 class Expectation { | 353 class Expectation { |
| 367 final _value; | 354 final _value; |
| 368 | 355 |
| 369 Expectation(this._value); | 356 Expectation(this._value); |
| 370 | 357 |
| 371 /** Asserts that the value is equivalent to the given expected value. */ | 358 /** Asserts that the value is equivalent to [expected]. */ |
| 372 void equals(expected) { | 359 void equals(expected) { |
| 373 Expect.equals(expected, _value); | 360 Expect.equals(expected, _value); |
| 374 } | 361 } |
| 375 | 362 |
| 376 /** | 363 /** |
| 377 * Failure if the difference between expected and actual is greater than the | 364 * Asserts that the difference between [expected] and the value is within |
| 378 * given tolerance. If no tolerance is given, tolerance is assumed to be the | 365 * [tolerance]. If no tolerance is given, it is assumed to be the value 4 |
| 379 * value 4 significant digits smaller than the value given for expected. | 366 * significant digits smaller than the expected value. |
| 380 */ | 367 */ |
| 381 void approxEquals(num expected, | 368 void approxEquals(num expected, |
| 382 [num tolerance = null, String reason = null]) { | 369 [num tolerance = null, String reason = null]) { |
| 383 Expect.approxEquals(expected, _value, tolerance: tolerance, reason: reason); | 370 Expect.approxEquals(expected, _value, tolerance: tolerance, reason: reason); |
| 384 } | 371 } |
| 385 | 372 |
| 386 /** Asserts that the value is null. */ | 373 /** Asserts that the value is [null]. */ |
| 387 void isNull() { | 374 void isNull() { |
| 388 Expect.equals(null, _value); | 375 Expect.equals(null, _value); |
| 389 } | 376 } |
| 390 | 377 |
| 391 /** Asserts that the value is not null. */ | 378 /** Asserts that the value is not [null]. */ |
| 392 void isNotNull() { | 379 void isNotNull() { |
| 393 Expect.notEquals(null, _value); | 380 Expect.notEquals(null, _value); |
| 394 } | 381 } |
| 395 | 382 |
| 396 /** Asserts that the value is true. */ | 383 /** Asserts that the value is [true]. */ |
| 397 void isTrue() { | 384 void isTrue() { |
| 398 Expect.equals(true, _value); | 385 Expect.equals(true, _value); |
| 399 } | 386 } |
| 400 | 387 |
| 401 /** Asserts that the value is null. */ | 388 /** Asserts that the value is [false]. */ |
| 402 void isFalse() { | 389 void isFalse() { |
| 403 Expect.equals(false, _value); | 390 Expect.equals(false, _value); |
| 404 } | 391 } |
| 405 | 392 |
| 406 /** Asserts that the value has the same elements as the given collection. */ | 393 /** Asserts that the value has the same elements as [expected]. */ |
| 407 void equalsCollection(Collection expected) { | 394 void equalsCollection(Collection expected) { |
| 408 Expect.listEquals(expected, _value); | 395 Expect.listEquals(expected, _value); |
| 409 } | 396 } |
| 410 } | |
| 411 | 397 |
| 412 /** | 398 /** |
| 413 * A TestSet lets you break a test suite down into a collection of classes to | 399 * Checks that every element of [expected] is also in [actual], and that |
| 414 * keep things manageable. It exposes the same interface as UnitTestSuite | 400 * every element of [actual] is also in [expected]. |
| 415 * (test(), group(), expect(), etc.) but defers to a parent suite that owns it. | 401 */ |
| 416 */ | 402 void equalsSet(Iterable expected) { |
| 417 class TestSet { | 403 Expect.setEquals(expected, _value); |
| 418 UnitTestSuite _currentSuite = null; | |
| 419 | |
| 420 // TODO(rnystrom): Remove this when default constructors are supported. | |
| 421 TestSet(); | |
| 422 | |
| 423 void _bindToSuite(UnitTestSuite suite) { | |
| 424 _currentSuite = suite; | |
| 425 } | |
| 426 | |
| 427 /** Override this to define the specifications for this test set. */ | |
| 428 void setup() { | |
| 429 // Do nothing. | |
| 430 } | |
| 431 | |
| 432 /** Enqueues a synchronous test. */ | |
| 433 void addTest(TestFunction test) { | |
| 434 _currentSuite.addTest(test); | |
| 435 } | |
| 436 | |
| 437 /** Adds the tests defined by the given TestSet to this suite. */ | |
| 438 void addTestSet(TestSet test) { | |
| 439 _currentSuite.addTestSet(test); | |
| 440 } | |
| 441 | |
| 442 /** Adds the tests defined by the given TestSets to this suite. */ | |
| 443 void addTestSets(Iterable<TestSet> tests) { | |
| 444 _currentSuite.addTestSets(tests); | |
| 445 } | |
| 446 | |
| 447 /** Enqueues an asynchronous test that waits for [callbacks] callbacks. */ | |
| 448 void addAsyncTest(TestFunction test, int callbacks) { | |
| 449 _currentSuite.addAsyncTest(test, callbacks); | |
| 450 } | 404 } |
| 451 } | 405 } |
| 452 | 406 |
| 453 /** Summarizes information about a single test case. */ | 407 /** Summarizes information about a single test case. */ |
| 454 class TestCase { | 408 class TestCase { |
| 455 /** Identifier for this test. */ | 409 /** Identifier for this test. */ |
| 456 final id; | 410 final id; |
| 457 | 411 |
| 458 /** A description of what the test is specifying. */ | 412 /** A description of what the test is specifying. */ |
| 459 final String description; | 413 final String description; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 </tr>"""; | 478 </tr>"""; |
| 525 if (stackTrace != null) { | 479 if (stackTrace != null) { |
| 526 message += | 480 message += |
| 527 "<tr><td></td><td colspan='2'><pre>${stackTrace}</pre></td></tr>"; | 481 "<tr><td></td><td colspan='2'><pre>${stackTrace}</pre></td></tr>"; |
| 528 } | 482 } |
| 529 fail = true; | 483 fail = true; |
| 530 } | 484 } |
| 531 } | 485 } |
| 532 | 486 |
| 533 typedef void TestFunction(); | 487 typedef void TestFunction(); |
| OLD | NEW |