OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 * Expect is used for tests that do not want to make use of the | 6 * Expect is used for tests that do not want to make use of the |
7 * Dart unit test library - for example, the core language tests. | 7 * Dart unit test library - for example, the core language tests. |
8 * Third parties are discouraged from using this, and should use | 8 * Third parties are discouraged from using this, and should use |
9 * the expect() function in the unit test library instead for | 9 * the expect() function in the unit test library instead for |
10 * test assertions. | 10 * test assertions. |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 * that the correct exception is being thrown. For example, to check | 257 * that the correct exception is being thrown. For example, to check |
258 * the type of the exception you could write this: | 258 * the type of the exception you could write this: |
259 * | 259 * |
260 * Expect.throws(myThrowingFunction, (e) => e is MyException); | 260 * Expect.throws(myThrowingFunction, (e) => e is MyException); |
261 */ | 261 */ |
262 static void throws(void f(), | 262 static void throws(void f(), |
263 [_CheckExceptionFn check = null, | 263 [_CheckExceptionFn check = null, |
264 String reason = null]) { | 264 String reason = null]) { |
265 try { | 265 try { |
266 f(); | 266 f(); |
267 } catch (e) { | 267 } catch (e, s) { |
268 if (check !== null) { | 268 if (check !== null) { |
269 Expect.isTrue(check(e)); | 269 if (!check(e)) { |
270 String msg = reason == null ? "" : reason; | |
floitsch
2012/11/01 12:11:39
why not just change "reason" to have a default val
Lasse Reichstein Nielsen
2012/11/01 13:39:09
I believe I have seen places where one check is ne
| |
271 _fail("Expect.throws($msg): Unexpected '$e'\n$s"); | |
272 } | |
270 } | 273 } |
271 return; | 274 return; |
272 } | 275 } |
273 String msg = _getMessage(reason); | 276 String msg = reason == null ? "" : reason; |
274 _fail('Expect.throws($msg) fails'); | 277 _fail('Expect.throws($msg) fails'); |
275 } | 278 } |
276 | 279 |
277 static String _getMessage(String reason) | 280 static String _getMessage(String reason) |
278 => (reason === null) ? "" : ", '$reason'"; | 281 => (reason === null) ? "" : ", '$reason'"; |
279 | 282 |
280 static void _fail(String message) { | 283 static void _fail(String message) { |
281 throw new ExpectException(message); | 284 throw new ExpectException(message); |
282 } | 285 } |
283 } | 286 } |
284 | 287 |
285 typedef bool _CheckExceptionFn(exception); | 288 typedef bool _CheckExceptionFn(exception); |
286 | 289 |
287 class ExpectException implements Exception { | 290 class ExpectException implements Exception { |
288 ExpectException(this.message); | 291 ExpectException(this.message); |
289 String toString() => message; | 292 String toString() => message; |
290 String message; | 293 String message; |
291 } | 294 } |
OLD | NEW |