Index: pkg/expect/lib/expect.dart |
diff --git a/pkg/expect/lib/expect.dart b/pkg/expect/lib/expect.dart |
index 20d216f1a6020596e46531b46351ecd0b01b5bea..562af8d8c1936b4bbd1c482243b00c2d69afd949 100644 |
--- a/pkg/expect/lib/expect.dart |
+++ b/pkg/expect/lib/expect.dart |
@@ -346,19 +346,23 @@ class Expect { |
static void throws(void f(), |
[_CheckExceptionFn check = null, |
String reason = null]) { |
+ String msg = reason == null ? "" : "($reason)"; |
+ if (f is! _Nullary) { |
+ // Only throws from executing the funtion body should count as throwing. |
+ // The failure to even call `f` should throw outside the try/catch. |
+ _fail("Expect.throws$msg: Funciton f not callable with zero arguments"); |
+ } |
try { |
f(); |
} catch (e, s) { |
if (check != null) { |
if (!check(e)) { |
- String msg = reason == null ? "" : reason; |
- _fail("Expect.throws($msg): Unexpected '$e'\n$s"); |
+ _fail("Expect.throws$msg: Unexpected '$e'\n$s"); |
} |
} |
return; |
} |
- String msg = reason == null ? "" : reason; |
- _fail('Expect.throws($msg) fails'); |
+ _fail('Expect.throws$msg fails: Did not throw'); |
} |
static String _getMessage(String reason) |
@@ -372,6 +376,7 @@ class Expect { |
bool _identical(a, b) => identical(a, b); |
typedef bool _CheckExceptionFn(exception); |
+typedef _Nullary(); // Expect.throws argument must be this type. |
class ExpectException implements Exception { |
ExpectException(this.message); |