Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(605)

Side by Side Diff: pkg/expect/lib/expect.dart

Issue 692463002: Make Expect.throws not accept arguments that fail to be called. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't call f. It might succeede through noSuchMethod. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 * This library contains an Expect class with static methods that can be used 6 * This library contains an Expect class with static methods that can be used
7 * for simple unit-tests. 7 * for simple unit-tests.
8 */ 8 */
9 library expect; 9 library expect;
10 10
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 * Calls the function [f] and verifies that it throws an exception. 339 * Calls the function [f] and verifies that it throws an exception.
340 * The optional [check] function can provide additional validation 340 * The optional [check] function can provide additional validation
341 * that the correct exception is being thrown. For example, to check 341 * that the correct exception is being thrown. For example, to check
342 * the type of the exception you could write this: 342 * the type of the exception you could write this:
343 * 343 *
344 * Expect.throws(myThrowingFunction, (e) => e is MyException); 344 * Expect.throws(myThrowingFunction, (e) => e is MyException);
345 */ 345 */
346 static void throws(void f(), 346 static void throws(void f(),
347 [_CheckExceptionFn check = null, 347 [_CheckExceptionFn check = null,
348 String reason = null]) { 348 String reason = null]) {
349 String msg = reason == null ? "" : "($reason)";
350 if (f is! _Nullary) {
351 // Only throws from executing the funtion body should count as throwing.
352 // The failure to even call `f` should throw outside the try/catch.
353 _fail("Expect.throws$msg: Funciton f not callable with zero arguments");
354 }
349 try { 355 try {
350 f(); 356 f();
351 } catch (e, s) { 357 } catch (e, s) {
352 if (check != null) { 358 if (check != null) {
353 if (!check(e)) { 359 if (!check(e)) {
354 String msg = reason == null ? "" : reason; 360 _fail("Expect.throws$msg: Unexpected '$e'\n$s");
355 _fail("Expect.throws($msg): Unexpected '$e'\n$s");
356 } 361 }
357 } 362 }
358 return; 363 return;
359 } 364 }
360 String msg = reason == null ? "" : reason; 365 _fail('Expect.throws$msg fails: Did not throw');
361 _fail('Expect.throws($msg) fails');
362 } 366 }
363 367
364 static String _getMessage(String reason) 368 static String _getMessage(String reason)
365 => (reason == null) ? "" : ", '$reason'"; 369 => (reason == null) ? "" : ", '$reason'";
366 370
367 static void _fail(String message) { 371 static void _fail(String message) {
368 throw new ExpectException(message); 372 throw new ExpectException(message);
369 } 373 }
370 } 374 }
371 375
372 bool _identical(a, b) => identical(a, b); 376 bool _identical(a, b) => identical(a, b);
373 377
374 typedef bool _CheckExceptionFn(exception); 378 typedef bool _CheckExceptionFn(exception);
379 typedef _Nullary(); // Expect.throws argument must be this type.
375 380
376 class ExpectException implements Exception { 381 class ExpectException implements Exception {
377 ExpectException(this.message); 382 ExpectException(this.message);
378 String toString() => message; 383 String toString() => message;
379 String message; 384 String message;
380 } 385 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698