Permalink
Browse files

Correct error messages

  • Loading branch information...
dnaumov committed Apr 7, 2012
1 parent 9c2a7ac commit 44d575d4a620acbb43312962c8933a1b68c6dc00
Showing with 18 additions and 5 deletions.
  1. +6 −1 src/contracts/core.clj
  2. +12 −4 test/contracts/test/core.clj
View
@@ -19,14 +19,18 @@
(and (seq? expr)
(= (resolve (first expr)) #'=>)))
+(def current-var (atom nil))
+
(defn gen-check [type exprs+preds]
(into {}
(for [[expr pred] exprs+preds]
(if (combinator-expr? pred)
`['~expr (~pred ~expr)]
`['~expr (if (~pred ~expr)
~expr
- (throw (AssertionError. (report ~expr {:type ~type :pred '~pred}))))]))))
+ (throw (AssertionError. (report ~expr {:type ~type
+ :pred '~pred
+ :var ~(deref current-var)}))))]))))
(defmacro =>
([pre post]
@@ -52,6 +56,7 @@
(match expr
[pre '=> post] (list* `=> (map normalize [pre post]))
:else expr))]
+ (reset! current-var (resolve sym))
`(alter-var-root (var ~sym) ~(normalize contract))))
(defmacro provide-contracts [& clauses]
@@ -2,7 +2,6 @@
(:use [contracts.core :as c :exclude [=>]] :reload)
(:use [midje.sweet]))
-
(fact "Simple contracts with explicit arguments"
(let [inc' ((c/=> [x] {x number?} pos?) inc)
*' ((c/=> [x y] {x even?, y odd?} pos?) *)]
@@ -14,7 +13,6 @@
(*' 3 3) => (throws AssertionError #"Pre" #"even?")
(*' 2 -3) => (throws AssertionError #"Post")))
-
(fact "Checking arbitrary expressions"
(let [f (fn [x y] (- (+ x y)))
f' ((c/=> [x y] {x number?, y number?, (+ x y) pos?} odd?) f)]
@@ -62,8 +60,6 @@
(g inc) => (throws AssertionError #"Pre" #"number?")
(h inc) => (throws AssertionError #"Post" #"string?"))))
-
-
(future-fact "Different contracts for different arities"
(let [f (fn
([x] (dec x))
@@ -82,3 +78,15 @@
(f' 2 -3) => (throws AssertionError #"Post")
;; </copypasted>
))
+
+
+(defn constrained-inc [x] (inc x))
+(defn constrained-dec [x] (dec x))
+
+(provide-contracts
+ (constrained-inc (c/=> number? number?))
+ (constrained-dec [number? => number?]))
+
+(fact "provide-contracts and error messages"
+ (constrained-inc "foo") => (throws AssertionError #"#'contracts.test.core/constrained-inc")
+ (constrained-dec "bar") => (throws AssertionError #"Pre" #"number?"))

0 comments on commit 44d575d

Please sign in to comment.