Skip to content

Commit df6761a

Browse files
mfikesswannodette
authored andcommitted
CLJS-2033: set-validator! should check current state
1 parent 1ed7091 commit df6761a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,6 +4325,9 @@ reduces them without incurring seq initialization"
43254325
is not acceptable to the new validator, an Error will be thrown and the
43264326
validator will not be changed."
43274327
[iref val]
4328+
(when (and (some? val)
4329+
(not (val (-deref iref))))
4330+
(throw (js/Error. "Validator rejected reference state")))
43284331
(set! (.-validator iref) val))
43294332

43304333
(defn get-validator

src/test/cljs/cljs/core_test.cljs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@
4747
(testing "Testing atom validators"
4848
(is (= coll? (get-validator a)))
4949
(is (thrown? js/Error (reset! a 1)))
50+
(is (thrown? js/Error (set-validator! a number?)))
51+
(is (some? (get-validator a)))
52+
(set-validator! a nil)
53+
(is (nil? (get-validator a)))
54+
(let [e1 (ex-info "" {})]
55+
(try
56+
(set-validator! a (fn [_] (throw e1)))
57+
(catch :default e2
58+
(is (identical? e1 e2)))))
5059
(is (= {:a 1} (meta a)))
5160
(alter-meta! a assoc :b 2)
5261
(is (= {:a 1 :b 2} (meta a)))))

0 commit comments

Comments
 (0)