|
129 | 129 | (-> (p/path-matches-clause [:x :y :z] (p/match-const "b"))
|
130 | 130 | (p/bind-match 'rebind))))))
|
131 | 131 |
|
| 132 | +(t/deftest reduce-lhs-test |
| 133 | + (t/is (empty? (p/reduce-lhs []))) |
| 134 | + (t/is (= {:x "x"} (p/reduce-lhs [{:x "x"}]))) |
| 135 | + (t/is (= {:x "x" :y 'y} (p/reduce-lhs [{:x "x"} {:y 'y}]))) |
| 136 | + (t/is (= {:x "other"} (p/reduce-lhs [{:x "X"} {:x "other"}]))) |
| 137 | + (t/is (= (list {:x "x" :y "y"} :guard [:some-guard]) |
| 138 | + (p/reduce-lhs [(list {:x "x"} :guard [:some-guard]) |
| 139 | + {:y "y"}]))) |
| 140 | + (t/is (= (list {:x "x" :y "y"}) |
| 141 | + (p/reduce-lhs [{:x "x"} (list {:y "y"})]))) |
| 142 | + |
| 143 | + (t/is (= (list {:x "x" :y '_ :a 42 :z '_} :guard [:guard-1 :guard-2 :guard-3]) |
| 144 | + (p/reduce-lhs [{:x "x"} |
| 145 | + (list {:y '_} :guard [:guard-1]) |
| 146 | + (list {:y '_} :guard [:guard-2]) |
| 147 | + {:a 42} |
| 148 | + (list {:z '_} :guard [:guard-3])])))) |
| 149 | + |
| 150 | + |
132 | 151 | ;; Imported test from active.clojure.match-test
|
133 | 152 | (def one-data
|
134 | 153 | {:kind "one" :x "x" :y "y" :z "z" :w "w"})
|
|
211 | 230 |
|
212 | 231 |
|
213 | 232 | ;;; FIXME these tests all fail because compare-fn patterns dont work.
|
214 |
| -;; (p/defpattern one-guard |
215 |
| -;; [(:kind #"one") |
216 |
| -;; (:x (:compare-fn #(= % (last ["a" "b" "c" "x"]))) :as x) |
217 |
| -;; (:y (:compare-fn #(= % (:y {:x "x" :y "y" :z "z"})))) |
218 |
| -;; (:z :as z) |
219 |
| -;; :w]) |
220 |
| - |
221 |
| -;; (def example-guard-matcher |
222 |
| -;; (p/map-matcher |
223 |
| -;; one-guard [x y z w] |
224 |
| -;; two [a b c Z Y X foo] |
225 |
| -;; :else false)) |
226 |
| - |
227 |
| -;; (def predicate-matcher |
228 |
| -;; (p/map-matcher |
229 |
| -;; ;; The order is important |
230 |
| -;; [(:x (:compare-fn #(string? %)))] ::string |
231 |
| -;; [(:x (:compare-fn (fn [x] (boolean? x))))] ::boolean |
232 |
| -;; [(:x (:compare-fn even?))] ::even |
233 |
| -;; [(:x (:compare-fn odd?))] ::odd)) |
234 |
| - |
235 |
| -;; (t/deftest map-matcher-predicate-test |
236 |
| -;; (t/is (= ::even (predicate-matcher {:x 42}))) |
237 |
| -;; (t/is (= ::odd (predicate-matcher {:x 41}))) |
238 |
| -;; (t/is (= ::string (predicate-matcher {:x "string"}))) |
239 |
| -;; (t/is (= ::boolean (predicate-matcher {:x true})))) |
240 |
| - |
241 |
| -;; (p/defpattern predicate-pattern |
242 |
| -;; [(:x (:compare-fn even?))]) |
243 |
| - |
244 |
| -;; (t/deftest map-matcher-polymorphism-test |
245 |
| -;; (t/testing "works with a pattern record" |
246 |
| -;; (t/is (= ::even |
247 |
| -;; ((p/map-matcher predicate-pattern ::even) |
248 |
| -;; {:x 42})))) |
249 |
| - |
250 |
| -;; (t/testing "works with pattern syntax" |
251 |
| -;; (t/is (= ::even |
252 |
| -;; ((p/map-matcher [(:x (:compare-fn even?))] ::even) |
253 |
| -;; {:x 42}))))) |
| 233 | +(p/defpattern one-guard |
| 234 | + [(:kind #"one") |
| 235 | + (:x (:compare-fn #(= % (last ["a" "b" "c" "x"]))) :as x) |
| 236 | + (:y (:compare-fn #(= % (:y {:x "x" :y "y" :z "z"})))) |
| 237 | + (:z :as z) |
| 238 | + :w]) |
| 239 | + |
| 240 | +(macroexpand-1 '(old-match/map-matcher [(:x (:compare-fn even?)) |
| 241 | + :y] x)) |
| 242 | + |
| 243 | +(def example-guard-matcher |
| 244 | + (p/map-matcher |
| 245 | + one-guard [x y z w] |
| 246 | + two [a b c Z Y X foo] |
| 247 | + :else false)) |
| 248 | + |
| 249 | +(def predicate-matcher |
| 250 | + (p/map-matcher |
| 251 | + ;; The order is important |
| 252 | + [(:x (:compare-fn #(string? %)))] ::string |
| 253 | + [(:x (:compare-fn (fn [x] (boolean? x))))] ::boolean |
| 254 | + [(:x (:compare-fn even?))] ::even |
| 255 | + [(:x (:compare-fn odd?))] ::odd)) |
| 256 | + |
| 257 | +(t/deftest map-matcher-predicate-test |
| 258 | + (t/is (= ::even (predicate-matcher {:x 42}))) |
| 259 | + (t/is (= ::odd (predicate-matcher {:x 41}))) |
| 260 | + (t/is (= ::string (predicate-matcher {:x "string"}))) |
| 261 | + (t/is (= ::boolean (predicate-matcher {:x true})))) |
| 262 | + |
| 263 | +(p/defpattern predicate-pattern |
| 264 | + [(:x (:compare-fn even?))]) |
| 265 | + |
| 266 | +(t/deftest map-matcher-polymorphism-test |
| 267 | + (t/testing "works with a pattern record" |
| 268 | + (t/is (= ::even |
| 269 | + ((p/map-matcher predicate-pattern ::even) |
| 270 | + {:x 42})))) |
| 271 | + |
| 272 | + (t/testing "works with pattern syntax" |
| 273 | + (t/is (= ::even |
| 274 | + ((p/map-matcher [(:x (:compare-fn even?))] ::even) |
| 275 | + {:x 42}))))) |
254 | 276 |
|
255 | 277 | ;; FIXME this should work
|
256 | 278 | ;; (t/deftest closes-over-outer-variables-test
|
|
0 commit comments