Skip to content

Commit

Permalink
CLJ-1052: assoc should throw exception if missing val for last key
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information
jafingerhut authored and stuarthalloway committed Oct 20, 2012
1 parent 12afbe8 commit 9aa3bca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/clj/clojure/core.clj
Expand Up @@ -188,7 +188,10 @@
([map key val & kvs] ([map key val & kvs]
(let [ret (assoc map key val)] (let [ret (assoc map key val)]
(if kvs (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))))) ret)))))


;;;;;;;;;;;;;;;;; metadata ;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;; metadata ;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
10 changes: 10 additions & 0 deletions test/clojure/test_clojure/data_structures.clj
Expand Up @@ -951,3 +951,13 @@
{z3b v4b, x1 5} [z3b v4a, z3a v4b, x1 5] {z3b v4b, x1 5} [z3b v4a, z3a v4b, x1 5]
{x1 v4a, w5a v4c, v4a z3b, y2 2} [x1 v4a, w5a v4a, w5b v4b, {x1 v4a, w5a v4c, v4a z3b, y2 2} [x1 v4a, w5a v4a, w5b v4b,
v4a z3a, y2 2, v4b z3b, w5c v4c]))) 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.