Skip to content
Browse files

CLJ-1052: assoc should throw exception if missing val for last key

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent 12afbe8 commit 9aa3bcaf63daeeaf44cf1ca40e9db72083a2b829 @jafingerhut jafingerhut committed with stuarthalloway Oct 5, 2012
Showing with 14 additions and 1 deletion.
  1. +4 −1 src/clj/clojure/core.clj
  2. +10 −0 test/clojure/test_clojure/data_structures.clj
View
5 src/clj/clojure/core.clj
@@ -188,7 +188,10 @@
([map key val & kvs]
(let [ret (assoc map key val)]
(if kvs
- (recur ret (first kvs) (second kvs) (nnext kvs))
+ (if (next kvs)
+ (recur ret (first kvs) (second kvs) (nnext kvs))
+ (throw (IllegalArgumentException.
+ "assoc expects even number of arguments after map/vector, found odd number")))
ret)))))
;;;;;;;;;;;;;;;;; metadata ;;;;;;;;;;;;;;;;;;;;;;;;;;;
View
10 test/clojure/test_clojure/data_structures.clj
@@ -951,3 +951,13 @@
{z3b v4b, x1 5} [z3b v4a, z3a v4b, x1 5]
{x1 v4a, w5a v4c, v4a z3b, y2 2} [x1 v4a, w5a v4a, w5b v4b,
v4a z3a, y2 2, v4b z3b, w5c v4c])))
+
+
+(deftest test-assoc
+ (are [x y] (= x y)
+ [4] (assoc [] 0 4)
+ [5 -7] (assoc [] 0 5 1 -7)
+ {:a 1} (assoc {} :a 1)
+ {:a 2 :b -2} (assoc {} :b -2 :a 2))
+ (is (thrown? IllegalArgumentException (assoc [] 0 5 1)))
+ (is (thrown? IllegalArgumentException (assoc {} :b -2 :a))))

0 comments on commit 9aa3bca

Please sign in to comment.
Something went wrong with that request. Please try again.