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, use the pub package manager. | 8 * To import this library, use the pub package manager. |
9 * Create a pubspec.yaml file in your project and add | 9 * Create a pubspec.yaml file in your project and add |
10 * a dependency on unittest with the following lines: | 10 * a dependency on unittest with the following lines: |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 * 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 |
84 * 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 |
85 * of times the callback should be called (the default is 1). | 85 * of times the callback should be called (the default is 1). |
86 * | 86 * |
87 * import 'package:unittest/unittest.dart'; | 87 * import 'package:unittest/unittest.dart'; |
88 * import 'dart:isolate'; | 88 * import 'dart:isolate'; |
89 * main() { | 89 * main() { |
90 * test('callback is executed once', () { | 90 * test('callback is executed once', () { |
91 * // wrap the callback of an asynchronous call with [expectAsync0] if | 91 * // wrap the callback of an asynchronous call with [expectAsync0] if |
92 * // the callback takes 0 arguments... | 92 * // the callback takes 0 arguments... |
93 * var timer = new Timer(0, (_) => expectAsync0(() { | 93 * var timer = Timer.run(expectAsync0(() { |
94 * int x = 2 + 3; | 94 * int x = 2 + 3; |
95 * expect(x, equals(5)); | 95 * expect(x, equals(5)); |
96 * })); | 96 * })); |
97 * }); | 97 * }); |
98 * | 98 * |
99 * test('callback is executed twice', () { | 99 * test('callback is executed twice', () { |
100 * var callback = (_) => expectAsync0(() { | 100 * var callback = expectAsync0(() { |
101 * int x = 2 + 3; | 101 * int x = 2 + 3; |
102 * expect(x, equals(5)); | 102 * expect(x, equals(5)); |
103 * }, count: 2); // <-- we can indicate multiplicity to [expectAsync0] | 103 * }, count: 2); // <-- we can indicate multiplicity to [expectAsync0] |
104 * new Timer(0, callback); | 104 * Timer.run(callback); |
105 * new Timer(0, callback); | 105 * Timer.run(callback); |
106 * }); | 106 * }); |
107 * } | 107 * } |
108 * | 108 * |
109 * 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 |
110 * 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 |
111 * 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 |
112 * 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 |
113 * 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 |
114 * 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 |
115 * that exceptions are properly handled. | 115 * that exceptions are properly handled. |
116 * | 116 * |
117 * 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 |
118 * 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 |
119 * 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 |
120 * regardless of the number of positional arguments. This requires new langauge | 120 * regardless of the number of positional arguments. This requires new langauge |
121 * features or fixes to the current spec (e.g. see | 121 * features or fixes to the current spec (e.g. see |
122 * [Issue 2706](http://dartbug.com/2706)). | 122 * [Issue 2706](http://dartbug.com/2706)). |
123 * | 123 * |
124 * 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 |
125 * arguments or that take named parameters. (this is not implemented yet, | 125 * arguments or that take named parameters. (this is not implemented yet, |
126 * but will be coming here soon). | 126 * but will be coming here soon). |
127 * | 127 * |
128 * import 'package:unittest/unittest.dart'; | 128 * import 'package:unittest/unittest.dart'; |
129 * import 'dart:isolate'; | 129 * import 'dart:isolate'; |
130 * main() { | 130 * main() { |
131 * test('callback is executed', () { | 131 * test('callback is executed', () { |
132 * // indicate ahead of time that an async callback is expected. | 132 * // indicate ahead of time that an async callback is expected. |
133 * var async = startAsync(); | 133 * var async = startAsync(); |
134 * new Timer(0, (_) { | 134 * Timer.run(() { |
135 * // Guard the body of the callback, so errors are propagated | 135 * // Guard the body of the callback, so errors are propagated |
136 * // correctly. | 136 * // correctly. |
137 * guardAsync(() { | 137 * guardAsync(() { |
138 * int x = 2 + 3; | 138 * int x = 2 + 3; |
139 * expect(x, equals(5)); | 139 * expect(x, equals(5)); |
140 * }); | 140 * }); |
141 * // indicate that the asynchronous callback was invoked. | 141 * // indicate that the asynchronous callback was invoked. |
142 * async.complete(); | 142 * async.complete(); |
143 * }); | 143 * }); |
144 * }); | 144 * }); |
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 } | 901 } |
902 | 902 |
903 /** Enable a test by ID. */ | 903 /** Enable a test by ID. */ |
904 void enableTest(int testId) => _setTestEnabledState(testId, true); | 904 void enableTest(int testId) => _setTestEnabledState(testId, true); |
905 | 905 |
906 /** Disable a test by ID. */ | 906 /** Disable a test by ID. */ |
907 void disableTest(int testId) => _setTestEnabledState(testId, false); | 907 void disableTest(int testId) => _setTestEnabledState(testId, false); |
908 | 908 |
909 /** Signature for a test function. */ | 909 /** Signature for a test function. */ |
910 typedef void TestFunction(); | 910 typedef void TestFunction(); |
OLD | NEW |