Permalink
Browse files

add fail function and use it in verify

  • Loading branch information...
1 parent d34eac4 commit 5de8a2ff32d351dcc931d0d10cdd4d67797bdc42 @ninjudd ninjudd committed Nov 20, 2013
Showing with 21 additions and 8 deletions.
  1. +10 −6 src/flatland/useful/utils.clj
  2. +11 −2 test/flatland/useful/utils_test.clj
@@ -10,14 +10,18 @@
([f x] (f x))
([f x & more] (apply f x more)))
+(defn fail
+ "Raise an exception. Takes an exception or a string with format args."
+ ([exception]
+ (throw (fix exception string? #(Exception. ^String %))))
+ ([string & args]
+ (fail (apply format string args))))
+
(defmacro verify
"Raise exception unless test returns true."
- ([test exception]
- `(when-not ~test
- (throw (fix ~exception string? #(Exception. ^String %)))))
- ([test string & args]
- `(verify ~test
- (format ~string ~@args))))
+ [test & args]
+ `(when-not ~test
+ (fail ~@args)))
(defmacro returning
"Compute a return value, then execute other forms for side effects.
@@ -93,11 +93,20 @@
(is (= -2 (incr -3)))))
(is (= 4 @count))))
+(deftest test-fail
+ (is (thrown? Throwable
+ (fail "Test")))
+ (is (thrown-with-msg? Throwable #"foo bar 2"
+ (fail "%s bar %d" "foo" 2))))
+
(deftest test-verify
(is (thrown? Throwable
(verify false "Test")))
- (is (thrown-with-msg? Throwable #"foo bar 2"
- (verify false "%s bar %d" "foo" 2))))
+ (is (thrown-with-msg? Throwable #"error 10"
+ (verify nil "error %d" 10)))
+ (testing "exception clause is not evaluated when verify succeeds"
+ (is (= nil (verify true
+ (throw (Exception.)))))))
(def memo-called (atom 0))
(defm sample-memoized [x]

0 comments on commit 5de8a2f

Please sign in to comment.