-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcase.cljc
More file actions
212 lines (202 loc) · 6.81 KB
/
case.cljc
File metadata and controls
212 lines (202 loc) · 6.81 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(ns clojure.core-test.case
(:require [clojure.test :as t :refer [are deftest is testing]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists] :as p]))
(when-var-exists case
;; Note that this eclectic group of test value types tests the :hashes path in Clojure
(defn positive-tests
[x]
(case x
sym :sym-result
:kw :kw-result
"string" :string-result
1 :integer-result
2N :big-integer-result
3.0 :double-result
4.0M :big-decimal-result
#?@(:cljs []
:default
[1/2 :ratio-result])
\a :character-result ; CLJS reader will convert \a to "a"
true :boolean-true-result
false :boolean-false-result
nil :nil-result
##Inf :inf-result
##-Inf :negative-inf-result
#?(:cljs ('(list of syms)) ; CLJS wants to eval the inner list, even though it shouldn't
:default ((list of syms))) :list-of-syms-result ; if you need to match a list, wrap it in another list
[:vec :of :kws] :vec-of-kws-result
{:a :map :of :kws} :map-of-kws-result
#{:a :set :of :kws} :set-of-kws-result
(:either :this :or :that) :one-of-multiple-result
:default))
;; this tests the :ints path
(defn all-nums-tests
[x]
(case x
(0 1) :zero-one
2 :two
3 :three
(4 5) :four-five
6 :six
7 :seven
(8 9) :eight-nine
:default))
;; this tests the :identity path
(defn all-keywords-tests
[x]
(case x
(:zero :one) :zero-one
:two :two
:three :three
(:four :five) :four-five
:six :six
:seven :seven
(:eight :nine) :eight-nine
:default))
(defn negative-tests
[x]
(case x
(range 0 5) :bad-range-result ; range not eval'd. matches 'range, 0, or 5.
(1 2 3 4) :real-range-result ; can't include 0 because previous clause
'foo :quote-foo-result ; reader expands to `(quote foo)`, a list of alternatives
##NaN :nan-result
;; empty default clause, so will throw if no matches
))
(deftest test-case
(testing "positive test cases"
(are [x expected] (= expected (positive-tests x))
'sym :sym-result
:kw :kw-result
"string" :string-result
#?@(:cljs ; all numbers are double-precision floating point in CLJS
[1 :integer-result
1.0 :integer-result
1N :integer-result
1.0M :integer-result
2 :big-integer-result
2N :big-integer-result
2.0 :big-integer-result
2.0M :big-integer-result
3 :double-result
3N :double-result
3.0 :double-result
3.0M :double-result
4 :big-decimal-result
4N :big-decimal-result
4.0 :big-decimal-result
4.0M :big-decimal-result]
;; In Python, 1 and True share the same hash value, so it is not
;; possible to distinguish between them in a map. The "winning"
;; value is always the value which is inserted into the map last.
:lpy
[1 :boolean-true-result
1.0 :boolean-true-result
1N :boolean-true-result
1.0M :boolean-true-result
2 :big-integer-result
2N :big-integer-result
2.0 :big-integer-result
2.0M :big-integer-result
3 :double-result
3N :double-result
3.0 :double-result
3.0M :double-result
4 :big-decimal-result
4N :big-decimal-result
4.0 :big-decimal-result
4.0M :big-decimal-result]
:phel
[1 :integer-result
1N :integer-result
1.0 :default
1.0M :default
2 :big-integer-result
2N :big-integer-result
2.0 :default
2.0M :default
3 :default
3N :default
3.0 :double-result
3.0M :double-result ; big-decimal not supported (uses float)
4 :default
4N :default
4.0 :big-decimal-result ; big-decimal not supported (uses float)
4.0M :big-decimal-result]
:default
[1 :integer-result
1N :integer-result ; JVM sees ints and big ints as equal for int-sized values
1.0 :default ; but doubles and big-doubles aren't the same
1.0M :default
2 :big-integer-result
2N :big-integer-result
2.0 :default
2.0M :default
3 :default
3N :default
3.0 :double-result
3.0M :default ; does discriminate between double and big-decimal
4 :default
4N :default
4.0 :default
4.0M :big-decimal-result])
#?@(:cljs []
:default
[1/2 :ratio-result])
\a :character-result ; CLJS reader will convert \a to "a"
#?@(:cljs ["a" :character-result] :default []) ; CLJS matches this to `\a`
true :boolean-true-result
false :boolean-false-result
nil :nil-result
##Inf :inf-result
##-Inf :negative-inf-result
'(list of syms) :list-of-syms-result
[:vec :of :kws] :vec-of-kws-result
'(:vec :of :kws) :vec-of-kws-result ; `case` allows vec pattern to match a list
#?@(:cljs []
:default ['[list of syms] :list-of-syms-result])
{:a :map :of :kws} :map-of-kws-result
#{:a :set :of :kws} :set-of-kws-result
:either :one-of-multiple-result
:this :one-of-multiple-result
:or :one-of-multiple-result
:that :one-of-multiple-result
:match-nuthin :default)
(are [x expected] (= expected (all-nums-tests x))
0 :zero-one
1 :zero-one
2 :two
3 :three
4 :four-five
5 :four-five
6 :six
7 :seven
8 :eight-nine
9 :eight-nine
10 :default
-1 :default)
(are [x expected] (= expected (all-keywords-tests x))
:zero :zero-one
:one :zero-one
:two :two
:three :three
:four :four-five
:five :four-five
:six :six
:seven :seven
:eight :eight-nine
:nine :eight-nine
:ten :default
:negative-one :default))
(testing "negative test cases"
(are [x expected] (= expected (negative-tests x))
'range :bad-range-result
0 :bad-range-result
1 :real-range-result
2 :real-range-result
3 :real-range-result
4 :real-range-result
5 :bad-range-result
'quote :quote-foo-result
'foo :quote-foo-result)
(is (p/thrown? (negative-tests ##NaN)))
(is (p/thrown? (negative-tests :something-not-found))))))