Permalink
Browse files

Fixed invariants messages

  • Loading branch information...
1 parent 8517e7a commit 4218130a32bbf411c3796becdfe2d1ea899d57c6 @dnaumov committed Apr 20, 2012
Showing with 41 additions and 18 deletions.
  1. +6 −3 src/contracts/core.clj
  2. +35 −15 test/contracts/test/core.clj
View
@@ -14,6 +14,7 @@
"%2" "second"
"%3" "third"
(str (subs s 1) "th")))
+ "(clojure.core/deref (var " (subs s 25 (- (count s) 2))
"(clojure.core/deref " (str "@" (subs s 20 (dec (count s))))
s))
@@ -46,11 +47,11 @@
(= (resolve (first expr)) #'=>)))
;; TODO: rename
-(defn gen-check* [{:keys [type cond return-val pred expr]}]
+(defn gen-check* [{:keys [type cond return-val pred expr value]}]
`(if ~cond
~return-val
(throw (AssertionError.
- (report {:value ~expr
+ (report {:value ~value
:type ~type
:pred '~pred
:expr '~expr
@@ -64,6 +65,7 @@
`['~expr ~(gen-check* {:cond cond
:return-val ret
:expr expr
+ :value expr
:type type
:pred pred})])
(into {})))
@@ -120,7 +122,8 @@
:cond `(~pred ~newval)
:return-val true
:pred pred
- :expr `(deref ~target)}))))
+ :expr `(deref ~target)
+ :value newval}))))
(defmacro provide-contract [target contract]
(let [contract (normalize-contract contract)
@@ -156,18 +156,38 @@
(constrained-dec "bar") => (throws AssertionError #"Pre" #"number\?"))
-(def x (atom 1))
-(def ^:dynamic y 1)
-
-(c/provide-contracts
- (x number?)
- (#'y number?))
-
-(fact "Invariants"
- (swap! x inc) => 2
- (reset! x "str") => (throws AssertionError)
- (alter-var-root #'y inc) => 2
- (binding [y "str"]) => (throws AssertionError)
- (against-background
- (before :facts (reset! x 1))
- (before :facts (alter-var-root #'y (constantly 1)))))
+(facts "Invariants"
+
+ (fact "for Vars"
+ (against-background
+ (before :contents (do (def ^:dynamic a-var 1)
+ (c/provide-contract #'a-var number?))))
+
+ (alter-var-root #'a-var inc) => 2
+ (alter-var-root #'a-var (constantly "nan"))
+ => (throws AssertionError
+ #"Invariant"
+ #"Expecting: a-var is: number?"
+ #"Given: \"nan\"")
+ (binding [a-var "str"]) => (throws AssertionError #"\"str\"")
+ (binding [a-var 1] (set! a-var "str")) => (throws AssertionError #"\"str\""))
+
+ (fact "for other IRefs"
+ (against-background
+ (before :contents (do (def x (atom 1))
+ (def y (ref 1))
+ (def z (agent 1))
+ (c/provide-contracts
+ (x number?) (y pos?) (z odd?)))))
+ (swap! x inc) => 2
+ (reset! x "str") => (throws AssertionError
+ #"Invariant"
+ #"Expecting: @x is: number?"
+ #"\"str\"")
+
+ (dosync (alter y inc)) => 2
+ (dosync (alter y -)) => (throws AssertionError #"-2")
+
+ (do (send z -) (await z) @z) => -1
+ (do (send z inc) (Thread/sleep 100) (throw (agent-error z)))
+ => (throws AssertionError #"0")))

0 comments on commit 4218130

Please sign in to comment.