Permalink
Browse files

`throws` checker should not spew its guts when given non-throwable

  • Loading branch information...
1 parent f774acf commit 322ee9a81eaf817d75b63dc336f749a77e9edf51 @marick committed Apr 26, 2012
Showing with 18 additions and 10 deletions.
  1. +12 −8 src/midje/checkers/simple.clj
  2. +6 −2 test/midje/checkers/t_simple.clj
@@ -81,12 +81,16 @@
So, for example, you can write this:
(fact (foo) => (throws #\"one part\" #\"another part\"))"
[& desiderata]
- (checker [^ICapturedThrowable wrapped-throwable]
- (let [throwable (.throwable wrapped-throwable)
- evaluations (map (partial throwable-as-desired? throwable) desiderata)
- failures (filter extended-false? evaluations)]
- ;; It might be nice to return some sort of composite failure, but I bet
- ;; just returning the first one is fine, especially since I expect people
- ;; will use the class as the first desiderata.
- (or (empty? failures) (first failures)))))
+ (checker [wrapped-throwable]
+ (if-not (instance? ICapturedThrowable wrapped-throwable)
+ false
+ (let [throwable (.throwable wrapped-throwable)
+ evaluations (map (partial throwable-as-desired? throwable)
+ desiderata)
+ failures (filter extended-false? evaluations)]
+ ;; It might be nice to return some sort of composite
+ ;; failure, but I bet just returning the first one is fine,
+ ;; especially since I expect people will use the class as
+ ;; the first desiderata.
+ (or (empty? failures) (first failures))))))
)
@@ -136,12 +136,16 @@
(throw-exception "msg") => (throws Error #"^m" #"g$")
;; Note that both regexps should match.
(throw-exception "msg") =deny=> (throws Error #"m" #"h"))
-
-
(fact "`throws` matches any exception that is an instance of expected"
(throw (NullPointerException.)) => (throws Exception))
+(after-silently
+ (fact "`throws` fails when not given an exception"
+ 1 => (throws Exception))
+ (fact
+ @reported => (just checker-fails)))
+
;; Unexpected exceptions
(after-silently
(facts

0 comments on commit 322ee9a

Please sign in to comment.