| 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, use the pub package manager. |
| 9 * pkg/unittest/unittest.dart. | 9 * Create a pubspec.yaml file in your project and add |
| 10 * a dependency on unittest with the following lines: |
| 11 * dependencies: |
| 12 * unittest: |
| 13 * sdk: unittest |
| 14 * |
| 15 * Then run 'pub install' from your project directory or using |
| 16 * the DartEditor. |
| 17 * |
| 18 * Please see [Pub Getting Started](http://pub.dartlang.org/doc) |
| 19 * for more details about the pub package manager. |
| 10 * | 20 * |
| 11 * ##Concepts## | 21 * ##Concepts## |
| 12 * | 22 * |
| 13 * * Tests: Tests are specified via the top-level function [test], they can be | 23 * * Tests: Tests are specified via the top-level function [test], they can be |
| 14 * organized together using [group]. | 24 * organized together using [group]. |
| 15 * * Checks: Test expectations can be specified via [expect] | 25 * * Checks: Test expectations can be specified via [expect] |
| 16 * * Matchers: [expect] assertions are written declaratively using [Matcher]s | 26 * * Matchers: [expect] assertions are written declaratively using [Matcher]s |
| 17 * * Configuration: The framework can be adapted by calling [configure] with a | 27 * * Configuration: The framework can be adapted by calling [configure] with a |
| 18 * [Configuration]. Common configurations can be found in this package | 28 * [Configuration]. Common configurations can be found in this package |
| 19 * under: 'dom\_config.dart' (deprecated), 'html\_config.dart' (for running | 29 * under: 'dom\_config.dart' (deprecated), 'html\_config.dart' (for running |
| 20 * tests compiled to Javascript in a browser), and 'vm\_config.dart' (for | 30 * tests compiled to Javascript in a browser), and 'vm\_config.dart' (for |
| 21 * running native Dart tests on the VM). | 31 * running native Dart tests on the VM). |
| 22 * | 32 * |
| 23 * ##Examples## | 33 * ##Examples## |
| 24 * | 34 * |
| 25 * A trivial test: | 35 * A trivial test: |
| 26 * | 36 * |
| 27 * #import('path-to-dart/pkg/unittest/unitest.dart'); | 37 * import 'package:unittest/unittest.dart'; |
| 28 * main() { | 38 * main() { |
| 29 * test('this is a test', () { | 39 * test('this is a test', () { |
| 30 * int x = 2 + 3; | 40 * int x = 2 + 3; |
| 31 * expect(x, equals(5)); | 41 * expect(x, equals(5)); |
| 32 * }); | 42 * }); |
| 33 * } | 43 * } |
| 34 * | 44 * |
| 35 * Multiple tests: | 45 * Multiple tests: |
| 36 * | 46 * |
| 37 * #import('path-to-dart/pkg/unittest/unitest.dart'); | 47 * import 'package:unittest/unittest.dart'; |
| 38 * main() { | 48 * main() { |
| 39 * test('this is a test', () { | 49 * test('this is a test', () { |
| 40 * int x = 2 + 3; | 50 * int x = 2 + 3; |
| 41 * expect(x, equals(5)); | 51 * expect(x, equals(5)); |
| 42 * }); | 52 * }); |
| 43 * test('this is another test', () { | 53 * test('this is another test', () { |
| 44 * int x = 2 + 3; | 54 * int x = 2 + 3; |
| 45 * expect(x, equals(5)); | 55 * expect(x, equals(5)); |
| 46 * }); | 56 * }); |
| 47 * } | 57 * } |
| 48 * | 58 * |
| 49 * Multiple tests, grouped by category: | 59 * Multiple tests, grouped by category: |
| 50 * | 60 * |
| 51 * #import('path-to-dart/pkg/unittest/unitest.dart'); | 61 * import 'package:unittest/unittest.dart'; |
| 52 * main() { | 62 * main() { |
| 53 * group('group A', () { | 63 * group('group A', () { |
| 54 * test('test A.1', () { | 64 * test('test A.1', () { |
| 55 * int x = 2 + 3; | 65 * int x = 2 + 3; |
| 56 * expect(x, equals(5)); | 66 * expect(x, equals(5)); |
| 57 * }); | 67 * }); |
| 58 * test('test A.2', () { | 68 * test('test A.2', () { |
| 59 * int x = 2 + 3; | 69 * int x = 2 + 3; |
| 60 * expect(x, equals(5)); | 70 * expect(x, equals(5)); |
| 61 * }); | 71 * }); |
| 62 * }); | 72 * }); |
| 63 * group('group B', () { | 73 * group('group B', () { |
| 64 * test('this B.1', () { | 74 * test('this B.1', () { |
| 65 * int x = 2 + 3; | 75 * int x = 2 + 3; |
| 66 * expect(x, equals(5)); | 76 * expect(x, equals(5)); |
| 67 * }); | 77 * }); |
| 68 * }); | 78 * }); |
| 69 * } | 79 * } |
| 70 * | 80 * |
| 71 * Asynchronous tests: if callbacks expect between 0 and 2 positional arguments, | 81 * Asynchronous tests: if callbacks expect between 0 and 2 positional arguments, |
| 72 * depending on the suffix of expectAsyncX(). expectAsyncX() will wrap a | 82 * depending on the suffix of expectAsyncX(). expectAsyncX() will wrap a |
| 73 * function into a new callback and will not consider the test complete until | 83 * 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 | 84 * 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). | 85 * of times the callback should be called (the default is 1). |
| 76 * | 86 * |
| 77 * #import('path-to-dart/pkg/unittest/unitest.dart'); | 87 * import 'package:unittest/unittest.dart'; |
| 78 * #import('dart:html'); | 88 * import 'dart:isolate'; |
| 79 * main() { | 89 * main() { |
| 80 * test('calllback is executed once', () { | 90 * test('callback is executed once', () { |
| 81 * // wrap the callback of an asynchronous call with [expectAsync0] if | 91 * // wrap the callback of an asynchronous call with [expectAsync0] if |
| 82 * // the callback takes 0 arguments... | 92 * // the callback takes 0 arguments... |
| 83 * window.setTimeout(expectAsync0(() { | 93 * var timer = new Timer(0, (_) => expectAsync0(() { |
| 84 * int x = 2 + 3; | 94 * int x = 2 + 3; |
| 85 * expect(x, equals(5)); | 95 * expect(x, equals(5)); |
| 86 * }), 0); | 96 * })); |
| 87 * }); | 97 * }); |
| 88 * | 98 * |
| 89 * test('calllback is executed twice', () { | 99 * test('callback is executed twice', () { |
| 90 * var callback = expectAsync0(() { | 100 * var callback = (_) => expectAsync0(() { |
| 91 * int x = 2 + 3; | 101 * int x = 2 + 3; |
| 92 * expect(x, equals(5)); | 102 * expect(x, equals(5)); |
| 93 * }, count: 2); // <-- we can indicate multiplicity to [expectAsync0] | 103 * }, count: 2); // <-- we can indicate multiplicity to [expectAsync0] |
| 94 * window.setTimeout(callback, 0); | 104 * new Timer(0, callback); |
| 95 * window.setTimeout(callback, 0); | 105 * new Timer(0, callback); |
| 96 * }); | 106 * }); |
| 97 * } | 107 * } |
| 98 * | 108 * |
| 99 * expectAsyncX() will wrap the callback code in a try/catch handler to handle | 109 * expectAsyncX() will wrap the callback code in a try/catch handler to handle |
| 100 * exceptions (treated as test failures). There may be times when the number of | 110 * exceptions (treated as test failures). There may be times when the number of |
| 101 * times a callback should be called is non-deterministic. In this case a dummy | 111 * times a callback should be called is non-deterministic. In this case a dummy |
| 102 * callback can be created with expectAsync0((){}) and this can be called from | 112 * callback can be created with expectAsync0((){}) and this can be called from |
| 103 * the real callback when it is finally complete. In this case the body of the | 113 * the real callback when it is finally complete. In this case the body of the |
| 104 * callback should be protected within a call to guardAsync(); this will ensure | 114 * callback should be protected within a call to guardAsync(); this will ensure |
| 105 * that exceptions are properly handled. | 115 * that exceptions are properly handled. |
| 106 * | 116 * |
| 107 * Note: due to some language limitations we have to use different functions | 117 * Note: due to some language limitations we have to use different functions |
| 108 * depending on the number of positional arguments of the callback. In the | 118 * 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 | 119 * 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 | 120 * regardless of the number of positional arguments. This requires new langauge |
| 111 * features or fixes to the current spec (e.g. see | 121 * features or fixes to the current spec (e.g. see |
| 112 * [Issue 2706](http://dartbug.com/2706)). | 122 * [Issue 2706](http://dartbug.com/2706)). |
| 113 * | 123 * |
| 114 * Meanwhile, we plan to add this alternative API for callbacks of more than 2 | 124 * 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, | 125 * arguments or that take named parameters. (this is not implemented yet, |
| 116 * but will be coming here soon). | 126 * but will be coming here soon). |
| 117 * | 127 * |
| 118 * #import('path-to-dart/pkg/unittest/unitest.dart'); | 128 * import 'package:unittest/unittest.dart'; |
| 119 * #import('dart:html'); | 129 * import 'dart:isolate'; |
| 120 * main() { | 130 * main() { |
| 121 * test('calllback is executed', () { | 131 * test('callback is executed', () { |
| 122 * // indicate ahead of time that an async callback is expected. | 132 * // indicate ahead of time that an async callback is expected. |
| 123 * var async = startAsync(); | 133 * var async = startAsync(); |
| 124 * window.setTimeout(() { | 134 * new Timer(0, (_) { |
| 125 * // Guard the body of the callback, so errors are propagated | 135 * // Guard the body of the callback, so errors are propagated |
| 126 * // correctly | 136 * // correctly. |
| 127 * guardAsync(() { | 137 * guardAsync(() { |
| 128 * int x = 2 + 3; | 138 * int x = 2 + 3; |
| 129 * expect(x, equals(5)); | 139 * expect(x, equals(5)); |
| 130 * }); | 140 * }); |
| 131 * // indicate that the asynchronous callback was invoked. | 141 * // indicate that the asynchronous callback was invoked. |
| 132 * async.complete(); | 142 * async.complete(); |
| 133 * }), 0); | 143 * }); |
| 134 * }); | 144 * }); |
| 145 * } |
| 135 * | 146 * |
| 136 */ | 147 */ |
| 137 #library('unittest'); | 148 #library('unittest'); |
| 138 | 149 |
| 139 #import('dart:isolate'); | 150 #import('dart:isolate'); |
| 140 | 151 |
| 141 #source('collection_matchers.dart'); | 152 #source('collection_matchers.dart'); |
| 142 #source('config.dart'); | 153 #source('config.dart'); |
| 143 #source('core_matchers.dart'); | 154 #source('core_matchers.dart'); |
| 144 #source('description.dart'); | 155 #source('description.dart'); |
| (...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 886 } | 897 } |
| 887 | 898 |
| 888 /** Enable a test by ID. */ | 899 /** Enable a test by ID. */ |
| 889 void enableTest(int testId) => _setTestEnabledState(testId, true); | 900 void enableTest(int testId) => _setTestEnabledState(testId, true); |
| 890 | 901 |
| 891 /** Disable a test by ID. */ | 902 /** Disable a test by ID. */ |
| 892 void disableTest(int testId) => _setTestEnabledState(testId, false); | 903 void disableTest(int testId) => _setTestEnabledState(testId, false); |
| 893 | 904 |
| 894 /** Signature for a test function. */ | 905 /** Signature for a test function. */ |
| 895 typedef void TestFunction(); | 906 typedef void TestFunction(); |
| OLD | NEW |