view src/clojure/contrib/test_contrib/test_fnmap.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 (ns clojure.contrib.test-fnmap
2 (:use clojure.contrib.fnmap
3 clojure.test))
5 (deftest acts-like-map
6 (let [m1 (fnmap get assoc :key1 1 :key2 2)]
7 (are [k v] (= v (get m1 k))
8 :key1 1
9 :key2 2
10 :nonexistent-key nil)
11 (are [k v] (= v (k m1))
12 :key1 1
13 :key2 2
14 :nonexistent-key nil)
15 (let [m2 (assoc m1 :key3 3 :key4 4)]
16 (are [k v] (= v (get m2 k))
17 :key1 1
18 :key2 2
19 :key3 3
20 :key4 4
21 :nonexistent-key nil))))
23 (defn assoc-validate [m key value]
24 (if (integer? value)
25 (assoc m key value)
26 (throw (Exception. "Only integers allowed in this map!"))))
28 (deftest validators
29 (let [m (fnmap get assoc-validate)]
30 (is (= 2 (:key2 (assoc m :key2 2))))
31 (is (thrown? Exception (assoc m :key3 3.14)))))
33 (defn get-transform [m key]
34 (when-let [value (m key)]
35 (- value)))
37 (deftest transforms
38 (let [m (fnmap get-transform assoc)]
39 (is (= -2 (:key2 (assoc m :key2 2))))))