OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library unittest; | 5 library unittest; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'src/configuration.dart'; | 10 import 'src/configuration.dart'; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 /// is expected to be called exactly [count] times. If [max] is `-1`, the | 153 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
154 /// callback is allowed to be called any number of times greater than [count]. | 154 /// callback is allowed to be called any number of times greater than [count]. |
155 /// | 155 /// |
156 /// Both [id] and [reason] are optional and provide extra information about the | 156 /// Both [id] and [reason] are optional and provide extra information about the |
157 /// callback when debugging. [id] should be the name of the callback, while | 157 /// callback when debugging. [id] should be the name of the callback, while |
158 /// [reason] should be the reason the callback is expected to be called. | 158 /// [reason] should be the reason the callback is expected to be called. |
159 Function expectAsync(Function callback, | 159 Function expectAsync(Function callback, |
160 {int count: 1, int max: 0, String id, String reason}) => | 160 {int count: 1, int max: 0, String id, String reason}) => |
161 new ExpectedFunction(callback, count, max, id: id, reason: reason).func; | 161 new ExpectedFunction(callback, count, max, id: id, reason: reason).func; |
162 | 162 |
| 163 // Functions used to check how many arguments a callback takes. |
| 164 typedef T Func0<T>(); |
| 165 typedef T Func1<T, A>(A a); |
| 166 typedef T Func2<T, A, B>(A a, B b); |
| 167 typedef T Func3<T, A, B, C>(A a, B b, C c); |
| 168 typedef T Func4<T, A, B, C, D>(A a, B b, C c, D d); |
| 169 typedef T Func5<T, A, B, C, D, E>(A a, B b, C c, D d, E e); |
| 170 typedef T Func6<T, A, B, C, D, E, F>(A a, B b, C c, D d, E e, F f); |
| 171 |
| 172 /// Informs the framework that the given [callback] of arity 0 is expected to be |
| 173 /// called [count] number of times (by default 1). |
| 174 /// |
| 175 /// Returns a wrapped function that should be used as a replacement of the |
| 176 /// original callback. |
| 177 /// |
| 178 /// The test framework will wait for the callback to run the [count] times |
| 179 /// before it considers the current test to be complete. |
| 180 /// |
| 181 /// [max] can be used to specify an upper bound on the number of calls; if this |
| 182 /// is exceeded the test will fail. If [max] is `0` (the default), the callback |
| 183 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
| 184 /// callback is allowed to be called any number of times greater than [count]. |
| 185 /// |
| 186 /// Both [id] and [reason] are optional and provide extra information about the |
| 187 /// callback when debugging. [id] should be the name of the callback, while |
| 188 /// [reason] should be the reason the callback is expected to be called. |
| 189 /// |
| 190 /// This method takes callbacks with zero arguments. See also |
| 191 /// [expectAsync1], [expectAsync2], [expectAsync3], [expectAsync4], |
| 192 /// [expectAsync5], and [expectAsync6] for callbacks with different arity. |
| 193 Func0<dynamic/*=T*/ > expectAsync0/*<T>*/(dynamic/*=T*/ callback(), |
| 194 {int count: 1, int max: 0, String id, String reason}) { |
| 195 return new ExpectedFunction/*<T>*/(callback, count, max, |
| 196 id: id, reason: reason) |
| 197 .max0; |
| 198 } |
| 199 |
| 200 /// Informs the framework that the given [callback] of arity 1 is expected to be |
| 201 /// called [count] number of times (by default 1). |
| 202 /// |
| 203 /// Returns a wrapped function that should be used as a replacement of the |
| 204 /// original callback. |
| 205 /// |
| 206 /// The test framework will wait for the callback to run the [count] times |
| 207 /// before it considers the current test to be complete. |
| 208 /// |
| 209 /// [max] can be used to specify an upper bound on the number of calls; if this |
| 210 /// is exceeded the test will fail. If [max] is `0` (the default), the callback |
| 211 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
| 212 /// callback is allowed to be called any number of times greater than [count]. |
| 213 /// |
| 214 /// Both [id] and [reason] are optional and provide extra information about the |
| 215 /// callback when debugging. [id] should be the name of the callback, while |
| 216 /// [reason] should be the reason the callback is expected to be called. |
| 217 /// |
| 218 /// This method takes callbacks with one argument. See also |
| 219 /// [expectAsync0], [expectAsync2], [expectAsync3], [expectAsync4], |
| 220 /// [expectAsync5], and [expectAsync6] for callbacks with different arity. |
| 221 Func1<dynamic/*=T*/, dynamic/*=A*/ > expectAsync1/*<T, A>*/( |
| 222 dynamic/*=T*/ callback(dynamic/*=A*/ a), |
| 223 {int count: 1, |
| 224 int max: 0, |
| 225 String id, |
| 226 String reason}) { |
| 227 return new ExpectedFunction/*<T>*/(callback, count, max, |
| 228 id: id, reason: reason) |
| 229 .max1; |
| 230 } |
| 231 |
| 232 /// Informs the framework that the given [callback] of arity 2 is expected to be |
| 233 /// called [count] number of times (by default 1). |
| 234 /// |
| 235 /// Returns a wrapped function that should be used as a replacement of the |
| 236 /// original callback. |
| 237 /// |
| 238 /// The test framework will wait for the callback to run the [count] times |
| 239 /// before it considers the current test to be complete. |
| 240 /// |
| 241 /// [max] can be used to specify an upper bound on the number of calls; if this |
| 242 /// is exceeded the test will fail. If [max] is `0` (the default), the callback |
| 243 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
| 244 /// callback is allowed to be called any number of times greater than [count]. |
| 245 /// |
| 246 /// Both [id] and [reason] are optional and provide extra information about the |
| 247 /// callback when debugging. [id] should be the name of the callback, while |
| 248 /// [reason] should be the reason the callback is expected to be called. |
| 249 /// |
| 250 /// This method takes callbacks with two arguments. See also |
| 251 /// [expectAsync0], [expectAsync1], [expectAsync3], [expectAsync4], |
| 252 /// [expectAsync5], and [expectAsync6] for callbacks with different arity. |
| 253 Func2<dynamic/*=T*/, dynamic/*=A*/, dynamic/*=B*/ > expectAsync2/*<T, A, B>*/( |
| 254 dynamic/*=T*/ callback(dynamic/*=A*/ a, dynamic/*=B*/ b), |
| 255 {int count: 1, |
| 256 int max: 0, |
| 257 String id, |
| 258 String reason}) { |
| 259 return new ExpectedFunction/*<T>*/(callback, count, max, |
| 260 id: id, reason: reason) |
| 261 .max2; |
| 262 } |
| 263 |
| 264 /// Informs the framework that the given [callback] of arity 3 is expected to be |
| 265 /// called [count] number of times (by default 1). |
| 266 /// |
| 267 /// Returns a wrapped function that should be used as a replacement of the |
| 268 /// original callback. |
| 269 /// |
| 270 /// The test framework will wait for the callback to run the [count] times |
| 271 /// before it considers the current test to be complete. |
| 272 /// |
| 273 /// [max] can be used to specify an upper bound on the number of calls; if this |
| 274 /// is exceeded the test will fail. If [max] is `0` (the default), the callback |
| 275 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
| 276 /// callback is allowed to be called any number of times greater than [count]. |
| 277 /// |
| 278 /// Both [id] and [reason] are optional and provide extra information about the |
| 279 /// callback when debugging. [id] should be the name of the callback, while |
| 280 /// [reason] should be the reason the callback is expected to be called. |
| 281 /// |
| 282 /// This method takes callbacks with three arguments. See also |
| 283 /// [expectAsync0], [expectAsync1], [expectAsync2], [expectAsync4], |
| 284 /// [expectAsync5], and [expectAsync6] for callbacks with different arity. |
| 285 Func3<dynamic/*=T*/, dynamic/*=A*/, dynamic/*=B*/, dynamic/*=C*/ > |
| 286 expectAsync3/*<T, A, B, C>*/( |
| 287 dynamic/*=T*/ callback( |
| 288 dynamic/*=A*/ a, dynamic/*=B*/ b, dynamic/*=C*/ c), |
| 289 {int count: 1, |
| 290 int max: 0, |
| 291 String id, |
| 292 String reason}) { |
| 293 return new ExpectedFunction/*<T>*/(callback, count, max, |
| 294 id: id, reason: reason) |
| 295 .max3; |
| 296 } |
| 297 |
| 298 /// Informs the framework that the given [callback] of arity 4 is expected to be |
| 299 /// called [count] number of times (by default 1). |
| 300 /// |
| 301 /// Returns a wrapped function that should be used as a replacement of the |
| 302 /// original callback. |
| 303 /// |
| 304 /// The test framework will wait for the callback to run the [count] times |
| 305 /// before it considers the current test to be complete. |
| 306 /// |
| 307 /// [max] can be used to specify an upper bound on the number of calls; if this |
| 308 /// is exceeded the test will fail. If [max] is `0` (the default), the callback |
| 309 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
| 310 /// callback is allowed to be called any number of times greater than [count]. |
| 311 /// |
| 312 /// Both [id] and [reason] are optional and provide extra information about the |
| 313 /// callback when debugging. [id] should be the name of the callback, while |
| 314 /// [reason] should be the reason the callback is expected to be called. |
| 315 /// |
| 316 /// This method takes callbacks with four arguments. See also |
| 317 /// [expectAsync0], [expectAsync1], [expectAsync2], [expectAsync3], |
| 318 /// [expectAsync5], and [expectAsync6] for callbacks with different arity. |
| 319 Func4<dynamic/*=T*/, dynamic/*=A*/, dynamic/*=B*/, dynamic/*=C*/, |
| 320 dynamic/*=D*/ > |
| 321 expectAsync4/*<T, A, B, C, D>*/( |
| 322 dynamic/*=T*/ callback( |
| 323 dynamic/*=A*/ a, dynamic/*=B*/ b, dynamic/*=C*/ c, dynamic/*=D*/ d), |
| 324 {int count: 1, |
| 325 int max: 0, |
| 326 String id, |
| 327 String reason}) { |
| 328 return new ExpectedFunction/*<T>*/(callback, count, max, |
| 329 id: id, reason: reason) |
| 330 .max4; |
| 331 } |
| 332 |
| 333 /// Informs the framework that the given [callback] of arity 5 is expected to be |
| 334 /// called [count] number of times (by default 1). |
| 335 /// |
| 336 /// Returns a wrapped function that should be used as a replacement of the |
| 337 /// original callback. |
| 338 /// |
| 339 /// The test framework will wait for the callback to run the [count] times |
| 340 /// before it considers the current test to be complete. |
| 341 /// |
| 342 /// [max] can be used to specify an upper bound on the number of calls; if this |
| 343 /// is exceeded the test will fail. If [max] is `0` (the default), the callback |
| 344 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
| 345 /// callback is allowed to be called any number of times greater than [count]. |
| 346 /// |
| 347 /// Both [id] and [reason] are optional and provide extra information about the |
| 348 /// callback when debugging. [id] should be the name of the callback, while |
| 349 /// [reason] should be the reason the callback is expected to be called. |
| 350 /// |
| 351 /// This method takes callbacks with five arguments. See also |
| 352 /// [expectAsync0], [expectAsync1], [expectAsync2], [expectAsync3], |
| 353 /// [expectAsync4], and [expectAsync6] for callbacks with different arity. |
| 354 Func5<dynamic/*=T*/, dynamic/*=A*/, dynamic/*=B*/, dynamic/*=C*/, dynamic/*=D*/, |
| 355 dynamic/*=E*/ > |
| 356 expectAsync5/*<T, A, B, C, D, E>*/( |
| 357 dynamic/*=T*/ callback(dynamic/*=A*/ a, dynamic/*=B*/ b, |
| 358 dynamic/*=C*/ c, dynamic/*=D*/ d, dynamic/*=E*/ e), |
| 359 {int count: 1, |
| 360 int max: 0, |
| 361 String id, |
| 362 String reason}) { |
| 363 return new ExpectedFunction/*<T>*/(callback, count, max, |
| 364 id: id, reason: reason) |
| 365 .max5; |
| 366 } |
| 367 |
| 368 /// Informs the framework that the given [callback] of arity 6 is expected to be |
| 369 /// called [count] number of times (by default 1). |
| 370 /// |
| 371 /// Returns a wrapped function that should be used as a replacement of the |
| 372 /// original callback. |
| 373 /// |
| 374 /// The test framework will wait for the callback to run the [count] times |
| 375 /// before it considers the current test to be complete. |
| 376 /// |
| 377 /// [max] can be used to specify an upper bound on the number of calls; if this |
| 378 /// is exceeded the test will fail. If [max] is `0` (the default), the callback |
| 379 /// is expected to be called exactly [count] times. If [max] is `-1`, the |
| 380 /// callback is allowed to be called any number of times greater than [count]. |
| 381 /// |
| 382 /// Both [id] and [reason] are optional and provide extra information about the |
| 383 /// callback when debugging. [id] should be the name of the callback, while |
| 384 /// [reason] should be the reason the callback is expected to be called. |
| 385 /// |
| 386 /// This method takes callbacks with six arguments. See also |
| 387 /// [expectAsync0], [expectAsync1], [expectAsync2], [expectAsync3], |
| 388 /// [expectAsync4], and [expectAsync5] for callbacks with different arity. |
| 389 Func6<dynamic/*=T*/, dynamic/*=A*/, dynamic/*=B*/, dynamic/*=C*/, dynamic/*=D*/, |
| 390 dynamic/*=E*/, dynamic/*=F*/ > |
| 391 expectAsync6/*<T, A, B, C, D, E, F>*/( |
| 392 dynamic/*=T*/ callback(dynamic/*=A*/ a, dynamic/*=B*/ b, |
| 393 dynamic/*=C*/ c, dynamic/*=D*/ d, dynamic/*=E*/ e, dynamic/*=F*/ f), |
| 394 {int count: 1, |
| 395 int max: 0, |
| 396 String id, |
| 397 String reason}) { |
| 398 return new ExpectedFunction/*<T>*/(callback, count, max, |
| 399 id: id, reason: reason) |
| 400 .max6; |
| 401 } |
| 402 |
163 /// Indicate that [callback] is expected to be called until [isDone] returns | 403 /// Indicate that [callback] is expected to be called until [isDone] returns |
164 /// true. | 404 /// true. |
165 /// | 405 /// |
166 /// [isDone] is called after each time the function is run. Only when it returns | 406 /// [isDone] is called after each time the function is run. Only when it returns |
167 /// true will the callback be considered complete. Using [expectAsyncUntil] will | 407 /// true will the callback be considered complete. Using [expectAsyncUntil] will |
168 /// also ensure that errors that occur within [callback] are tracked and | 408 /// also ensure that errors that occur within [callback] are tracked and |
169 /// reported. [callback] may take up to six optional or required positional | 409 /// reported. [callback] may take up to six optional or required positional |
170 /// arguments; named arguments are not supported. | 410 /// arguments; named arguments are not supported. |
171 /// | 411 /// |
172 /// Both [id] and [reason] are optional and provide extra information about the | 412 /// Both [id] and [reason] are optional and provide extra information about the |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 void handleExternalError(e, String message, [stackTrace]) { | 500 void handleExternalError(e, String message, [stackTrace]) { |
261 var msg = '$message\nCaught $e'; | 501 var msg = '$message\nCaught $e'; |
262 | 502 |
263 if (currentTestCase != null) { | 503 if (currentTestCase != null) { |
264 _currentTestCase.error(msg, stackTrace); | 504 _currentTestCase.error(msg, stackTrace); |
265 } else { | 505 } else { |
266 environment.uncaughtErrorMessage = "$msg: $stackTrace"; | 506 environment.uncaughtErrorMessage = "$msg: $stackTrace"; |
267 } | 507 } |
268 } | 508 } |
269 | 509 |
| 510 typedef bool _TestFilter(InternalTestCase arg); |
| 511 |
270 /// Remove any tests that match [testFilter]. | 512 /// Remove any tests that match [testFilter]. |
271 /// | 513 /// |
272 /// [testFilter] can be a predicate function, a [RegExp], or a [String]. If it's | 514 /// [testFilter] can be a predicate function, a [RegExp], or a [String]. If it's |
273 /// a function, it's called with each [TestCase]. If it's a [String], it's | 515 /// a function, it's called with each [TestCase]. If it's a [String], it's |
274 /// parsed as a [RegExp] and matched against each [TestCase.description]. | 516 /// parsed as a [RegExp] and matched against each [TestCase.description]. |
275 /// | 517 /// |
276 /// This is different from enabling or disabling tests in that it removes the | 518 /// This is different from enabling or disabling tests in that it removes the |
277 /// tests completely. | 519 /// tests completely. |
278 void filterTests(testFilter) { | 520 void filterTests(testFilter) { |
279 var filterFunction; | 521 _TestFilter filterFunction; |
280 if (testFilter is String) { | 522 if (testFilter is String) { |
281 var re = new RegExp(testFilter); | 523 var re = new RegExp(testFilter); |
282 filterFunction = (t) => re.hasMatch(t.description); | 524 filterFunction = (t) => re.hasMatch(t.description); |
283 } else if (testFilter is RegExp) { | 525 } else if (testFilter is RegExp) { |
284 filterFunction = (t) => testFilter.hasMatch(t.description); | 526 filterFunction = (t) => testFilter.hasMatch(t.description); |
285 } else if (testFilter is Function) { | 527 } else if (testFilter is _TestFilter) { |
286 filterFunction = testFilter; | 528 filterFunction = testFilter; |
287 } | 529 } |
288 environment.testCases.retainWhere(filterFunction); | 530 environment.testCases.retainWhere(filterFunction); |
289 } | 531 } |
290 | 532 |
291 /// Runs all queued tests, one at a time. | 533 /// Runs all queued tests, one at a time. |
292 void runTests() { | 534 void runTests() { |
293 _requireNotRunning(); | 535 _requireNotRunning(); |
294 _ensureInitialized(false); | 536 _ensureInitialized(false); |
295 environment.currentTestCaseIndex = 0; | 537 environment.currentTestCaseIndex = 0; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 /// Creates a test environment running in its own zone scope. | 673 /// Creates a test environment running in its own zone scope. |
432 /// | 674 /// |
433 /// This allows for multiple invocations of the unittest library in the same | 675 /// This allows for multiple invocations of the unittest library in the same |
434 /// application instance. This is useful when, for example, creating a test | 676 /// application instance. This is useful when, for example, creating a test |
435 /// runner application which needs to create a new pristine test environment on | 677 /// runner application which needs to create a new pristine test environment on |
436 /// each invocation to run a given set of tests. | 678 /// each invocation to run a given set of tests. |
437 withTestEnvironment(callback()) { | 679 withTestEnvironment(callback()) { |
438 return runZoned(callback, | 680 return runZoned(callback, |
439 zoneValues: {#unittest.environment: new TestEnvironment()}); | 681 zoneValues: {#unittest.environment: new TestEnvironment()}); |
440 } | 682 } |
OLD | NEW |