Permalink
Browse files

Fixed bug with variadic function

  • Loading branch information...
1 parent 4b096fe commit d0a50dc9f862dd40c6f32768e83b45ab950275a8 @dnaumov committed Apr 10, 2012
Showing with 12 additions and 5 deletions.
  1. +6 −3 src/contracts/core.clj
  2. +6 −2 test/contracts/test/core.clj
@@ -34,8 +34,11 @@
x))
(defn gen-constrained-body [f post pre args]
- (let [[pre-check-results result] (map gensym ["pre-check-results" "result"])
- [normal-args ampersand rest-args] (partition-by #{'&} args)]
+ (let [[pre-check-results result] (map gensym ["pre-check-results" "result"])
+ [normal-args [maybe-amp :as maybe-rest]] (split-with #(not= % '&) args)
+ rest-args (if (= '& maybe-amp)
+ (next maybe-rest)
+ maybe-rest)]
`([~@args]
(let [~pre-check-results ~(gen-check :pre pre)
;; contract can alter the values of args, so we rebind them
@@ -48,7 +51,7 @@
([pre post]
(let [pre (cond
(and (list? pre) (every? vector? pre)) pre
- (vector? pre) (list pre)
+ (vector? pre) (list pre)
:else (list [pre]))
args (map #(vec (repeatedly (count %) (partial gensym "arg__")))
pre)
@@ -87,10 +87,14 @@
(fact "Contracts for variadic functions"
(let [f (fn [a b & more] (apply hash-map a b more))
- f' ((c/=> [a b & more] {more (comp even? count)} map?) f)]
+ f' ((c/=> [a b & more] {more (comp even? count)} map?) f)
+ g (fn [& numbers] (apply + numbers))
+ g' ((c/=> [& xs] {xs (partial every? number?)} number?) +)]
(f' :a 1) => {:a 1}
(f' :a 1 :b 2) => {:a 1 :b 2}
- (f' :a 1 :foo) => (throws AssertionError #"Pre" #"even?")))
+ (f' :a 1 :foo) => (throws AssertionError #"Pre" #"even?")
+ (g' 1 2) => 3
+ (g' 1 :foo) => (throws AssertionError #"Pre" #"every\? number\?")))
(defn constrained-inc [x] (inc x))

0 comments on commit d0a50dc

Please sign in to comment.