Skip to content

Commit

Permalink
Fixed circular alter of merge-with
Browse files Browse the repository at this point in the history
  • Loading branch information
fogus committed Oct 1, 2010
1 parent f021de6 commit 3fa2626
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/fogus/me/trammel.clj
Expand Up @@ -263,16 +263,19 @@
~name)))

(defn- apply-contract [f]
(fn [m & args]
(if-let [contract (-> m meta :contract)]
((partial contract identity) (apply f m args))
(apply f m args))))
(if (:hooked (meta f))
f
(with-meta
(fn [m & args]
(if-let [contract (-> m meta :contract)]
((partial contract identity) (apply f m args))
(apply f m args)))
{:hooked true})))

(alter-var-root (var assoc) apply-contract)
(alter-var-root (var dissoc) apply-contract)
(alter-var-root (var merge) apply-contract)
(alter-var-root (var merge) apply-contract)
(alter-var-root (var merge-with) (fn [f] (fn [f & maps] (apply (apply-contract merge-with) f maps))))
(alter-var-root (var merge-with) (fn [f] (let [mw (apply-contract f)] (fn [f & maps] (apply mw f maps)))))
(alter-var-root (var into) apply-contract)
(alter-var-root (var conj) apply-contract)
(alter-var-root (var assoc-in) apply-contract)
Expand Down
2 changes: 1 addition & 1 deletion test/fogus/me/impl_tests.clj
Expand Up @@ -12,7 +12,7 @@
;; remove this notice, or any other, from this software.

(ns fogus.me.impl-tests
(:use fogus.me.trammel)
#_(:use fogus.me.trammel)
(:use [clojure.test :only [deftest is]]))

(def *expectations-table*
Expand Down
6 changes: 2 additions & 4 deletions test/fogus/me/invariant_tests.clj
Expand Up @@ -15,6 +15,7 @@
(:use [fogus.me.trammel :only [defconstrainedrecord]])
(:use [clojure.test :only [deftest is]]))


(defconstrainedrecord Foo [a 1 b 2]
[(every? number? [a b])]
Object
Expand All @@ -31,7 +32,4 @@
(is (= (:b (new-Foo :a 42 :b 108 :c 36)) 108))
(is (= (:c (new-Foo :a 42 :b 108 :c 36)) 36))
(is (thrown? Error (new-Foo :a :b)))
(is (thrown? Error (new-Foo :a 42 :b nil)))
(is (= 1 (:a ((:factory (meta (new-Foo))) :c :b))))
(is (= 2 (:b ((:factory (meta (new-Foo))) :c :b))))
(is (= 0 (:c ((:factory (meta (new-Foo))) :c 0)))))
(is (thrown? Error (new-Foo :a 42 :b nil))))

0 comments on commit 3fa2626

Please sign in to comment.