-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlet_map_test.cljc
87 lines (70 loc) · 2.94 KB
/
let_map_test.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(ns dsann.let-map-test
(:require
[clojure.test :refer [deftest is testing]]
[flatland.ordered.map :refer [ordered-map]]
[dsann.test :refer [are]]
[dsann.let-map :refer [let-map let-assoc sym-map assoc-syms]]))
(deftest test-assoc-syms
(testing "dsann.assoc-syms"
(testing "Assoc symbols to map ..."
(are =
(let [a 1 b 2] (assoc-syms {:x 1} a b)) {:x 1 :a 1 :b 2}))
(testing "Retains map type"
(are =
(type (ordered-map))
(type (let [a 1]
(assoc-syms (ordered-map) a)))))))
(deftest test-sym-map
(testing "dsann.sym-map"
(testing "Create map from symbols ..."
(are =
(let [a 1 b 2] (sym-map a b)) {:a 1 :b 2}))))
(deftest test-let-assoc
(testing "dsann.let-assoc"
(let [m {:x 1}
md {:a 1}
vd [1 2 3]]
(testing "Exactly like let-map but assocs to an existing map."
(are =
(let-assoc m a 1) {:x 1 :a 1}
(let-assoc m a 1 b 2) {:x 1 :a 1 :b 2}
(let-assoc m a 1 b (inc a)) {:x 1 :a 1 :b 2}
(let-assoc m a (range 3) b (map inc a)) {:x 1 :a '(0 1 2) :b '(1 2 3)}
(let-assoc m _a 1 b (inc _a)) {:x 1 :b 2}
(let-assoc m {:keys [a]} md b (inc a)) {:x 1 :a 1 :b 2}
(let-assoc m [first & rest] vd) {:x 1 :first 1 :rest '(2 3)}
(let-assoc m
a 1
b (let-map x 5 y 7)
c (-> b :x inc)) {:x 1 :a 1 :b {:x 5 :y 7} :c 6}))
(testing "Retains map type"
(are =
(type (let-assoc (ordered-map :a 1) b 2))
(type (ordered-map)))))))
(deftest test-let-map
(testing "dsann.let-map"
(testing "Simple maps. (Can be done with map literal also)"
(are =
(let-map a 1) {:a 1}
(let-map a 1 b 2) {:a 1 :b 2}))
(testing "Calculations on values. Cannot be done with map literal"
(are =
; compare: - no need to create the map at the end.
; (let [a 1 b (inc a)] {:a a :b b})
(let-map a 1 b (inc a)) {:a 1 :b 2}
(let-map a (range 3) b (map inc a)) {:a '(0 1 2) :b '(1 2 3)}))
(testing "Intermediate values: names starting _ are excluded from the result"
(are =
(let-map _a 1 b (inc _a)) {:b 2}))
(testing "Destructuring works if needed"
(let [m {:a 1} v [1 2 3]]
(are =
(let-map {:keys [a]} m) {:a 1}
(let-map {:keys [a]} m b (inc a)) {:a 1 :b 2}
(let-map [first & rest] v) {:first 1 :rest '(2 3)})))
(testing "Nesting is ok. Scope applies: You must extract values from nested maps if you want to use them"
(are =
(let-map
a 1
b (let-map x 5 y 7)
c (-> b :x inc)) {:a 1 :b {:x 5 :y 7} :c 6}))))