| 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 * A library for writing dart unit tests. | 6 * A library for writing dart unit tests. |
| 7 * | 7 * |
| 8 * To import this library, specify the relative path to | 8 * To import this library, specify the relative path to |
| 9 * lib/unittest/unittest.dart. | 9 * pkg/unittest/unittest.dart. |
| 10 * | 10 * |
| 11 * ##Concepts## | 11 * ##Concepts## |
| 12 * | 12 * |
| 13 * * Tests: Tests are specified via the top-level function [test], they can be | 13 * * Tests: Tests are specified via the top-level function [test], they can be |
| 14 * organized together using [group]. | 14 * organized together using [group]. |
| 15 * * Checks: Test expectations can be specified via [expect] | 15 * * Checks: Test expectations can be specified via [expect] |
| 16 * * Matchers: [expect] assertions are written declaratively using [Matcher]s | 16 * * Matchers: [expect] assertions are written declaratively using [Matcher]s |
| 17 * * Configuration: The framework can be adapted by calling [configure] with a | 17 * * Configuration: The framework can be adapted by calling [configure] with a |
| 18 * [Configuration]. Common configurations can be found in this package | 18 * [Configuration]. Common configurations can be found in this package |
| 19 * under: 'dom\_config.dart' (deprecated), 'html\_config.dart' (for running | 19 * under: 'dom\_config.dart' (deprecated), 'html\_config.dart' (for running |
| 20 * tests compiled to Javascript in a browser), and 'vm\_config.dart' (for | 20 * tests compiled to Javascript in a browser), and 'vm\_config.dart' (for |
| 21 * running native Dart tests on the VM). | 21 * running native Dart tests on the VM). |
| 22 * | 22 * |
| 23 * ##Examples## | 23 * ##Examples## |
| 24 * | 24 * |
| 25 * A trivial test: | 25 * A trivial test: |
| 26 * | 26 * |
| 27 * #import('path-to-dart/lib/unittest/unitest.dart'); | 27 * #import('path-to-dart/pkg/unittest/unitest.dart'); |
| 28 * main() { | 28 * main() { |
| 29 * test('this is a test', () { | 29 * test('this is a test', () { |
| 30 * int x = 2 + 3; | 30 * int x = 2 + 3; |
| 31 * expect(x, equals(5)); | 31 * expect(x, equals(5)); |
| 32 * }); | 32 * }); |
| 33 * } | 33 * } |
| 34 * | 34 * |
| 35 * Multiple tests: | 35 * Multiple tests: |
| 36 * | 36 * |
| 37 * #import('path-to-dart/lib/unittest/unitest.dart'); | 37 * #import('path-to-dart/pkg/unittest/unitest.dart'); |
| 38 * main() { | 38 * main() { |
| 39 * test('this is a test', () { | 39 * test('this is a test', () { |
| 40 * int x = 2 + 3; | 40 * int x = 2 + 3; |
| 41 * expect(x, equals(5)); | 41 * expect(x, equals(5)); |
| 42 * }); | 42 * }); |
| 43 * test('this is another test', () { | 43 * test('this is another test', () { |
| 44 * int x = 2 + 3; | 44 * int x = 2 + 3; |
| 45 * expect(x, equals(5)); | 45 * expect(x, equals(5)); |
| 46 * }); | 46 * }); |
| 47 * } | 47 * } |
| 48 * | 48 * |
| 49 * Multiple tests, grouped by category: | 49 * Multiple tests, grouped by category: |
| 50 * | 50 * |
| 51 * #import('path-to-dart/lib/unittest/unitest.dart'); | 51 * #import('path-to-dart/pkg/unittest/unitest.dart'); |
| 52 * main() { | 52 * main() { |
| 53 * group('group A', () { | 53 * group('group A', () { |
| 54 * test('test A.1', () { | 54 * test('test A.1', () { |
| 55 * int x = 2 + 3; | 55 * int x = 2 + 3; |
| 56 * expect(x, equals(5)); | 56 * expect(x, equals(5)); |
| 57 * }); | 57 * }); |
| 58 * test('test A.2', () { | 58 * test('test A.2', () { |
| 59 * int x = 2 + 3; | 59 * int x = 2 + 3; |
| 60 * expect(x, equals(5)); | 60 * expect(x, equals(5)); |
| 61 * }); | 61 * }); |
| 62 * }); | 62 * }); |
| 63 * group('group B', () { | 63 * group('group B', () { |
| 64 * test('this B.1', () { | 64 * test('this B.1', () { |
| 65 * int x = 2 + 3; | 65 * int x = 2 + 3; |
| 66 * expect(x, equals(5)); | 66 * expect(x, equals(5)); |
| 67 * }); | 67 * }); |
| 68 * }); | 68 * }); |
| 69 * } | 69 * } |
| 70 * | 70 * |
| 71 * Asynchronous tests: if callbacks expect between 0 and 2 positional arguments, | 71 * Asynchronous tests: if callbacks expect between 0 and 2 positional arguments, |
| 72 * depending on the suffix of expectAsyncX(). expectAsyncX() will wrap a | 72 * depending on the suffix of expectAsyncX(). expectAsyncX() will wrap a |
| 73 * function into a new callback and will not consider the test complete until | 73 * function into a new callback and will not consider the test complete until |
| 74 * that callback is run. A count argument can be provided to specify the number | 74 * that callback is run. A count argument can be provided to specify the number |
| 75 * of times the callback should be called (the default is 1). | 75 * of times the callback should be called (the default is 1). |
| 76 * | 76 * |
| 77 * #import('path-to-dart/lib/unittest/unitest.dart'); | 77 * #import('path-to-dart/pkg/unittest/unitest.dart'); |
| 78 * #import('dart:dom_deprecated'); | 78 * #import('dart:dom_deprecated'); |
| 79 * main() { | 79 * main() { |
| 80 * test('calllback is executed once', () { | 80 * test('calllback is executed once', () { |
| 81 * // wrap the callback of an asynchronous call with [expectAsync0] if | 81 * // wrap the callback of an asynchronous call with [expectAsync0] if |
| 82 * // the callback takes 0 arguments... | 82 * // the callback takes 0 arguments... |
| 83 * window.setTimeout(expectAsync0(() { | 83 * window.setTimeout(expectAsync0(() { |
| 84 * int x = 2 + 3; | 84 * int x = 2 + 3; |
| 85 * expect(x, equals(5)); | 85 * expect(x, equals(5)); |
| 86 * }), 0); | 86 * }), 0); |
| 87 * }); | 87 * }); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 108 * depending on the number of positional arguments of the callback. In the | 108 * depending on the number of positional arguments of the callback. In the |
| 109 * future, we plan to expose a single `expectAsync` function that can be used | 109 * future, we plan to expose a single `expectAsync` function that can be used |
| 110 * regardless of the number of positional arguments. This requires new langauge | 110 * regardless of the number of positional arguments. This requires new langauge |
| 111 * features or fixes to the current spec (e.g. see | 111 * features or fixes to the current spec (e.g. see |
| 112 * [Issue 2706](http://dartbug.com/2706)). | 112 * [Issue 2706](http://dartbug.com/2706)). |
| 113 * | 113 * |
| 114 * Meanwhile, we plan to add this alternative API for callbacks of more than 2 | 114 * Meanwhile, we plan to add this alternative API for callbacks of more than 2 |
| 115 * arguments or that take named parameters. (this is not implemented yet, | 115 * arguments or that take named parameters. (this is not implemented yet, |
| 116 * but will be coming here soon). | 116 * but will be coming here soon). |
| 117 * | 117 * |
| 118 * #import('path-to-dart/lib/unittest/unitest.dart'); | 118 * #import('path-to-dart/pkg/unittest/unitest.dart'); |
| 119 * #import('dart:dom_deprecated'); | 119 * #import('dart:dom_deprecated'); |
| 120 * main() { | 120 * main() { |
| 121 * test('calllback is executed', () { | 121 * test('calllback is executed', () { |
| 122 * // indicate ahead of time that an async callback is expected. | 122 * // indicate ahead of time that an async callback is expected. |
| 123 * var async = startAsync(); | 123 * var async = startAsync(); |
| 124 * window.setTimeout(() { | 124 * window.setTimeout(() { |
| 125 * // Guard the body of the callback, so errors are propagated | 125 * // Guard the body of the callback, so errors are propagated |
| 126 * // correctly | 126 * // correctly |
| 127 * guardAsync(() { | 127 * guardAsync(() { |
| 128 * int x = 2 + 3; | 128 * int x = 2 + 3; |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 } | 852 } |
| 853 | 853 |
| 854 /** Enable a test by ID. */ | 854 /** Enable a test by ID. */ |
| 855 void enableTest(int testId) => _setTestEnabledState(testId, true); | 855 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 856 | 856 |
| 857 /** Disable a test by ID. */ | 857 /** Disable a test by ID. */ |
| 858 void disableTest(int testId) => _setTestEnabledState(testId, false); | 858 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 859 | 859 |
| 860 /** Signature for a test function. */ | 860 /** Signature for a test function. */ |
| 861 typedef void TestFunction(); | 861 typedef void TestFunction(); |
| OLD | NEW |