Skip to content

Commit ccd2099

Browse files
authored
fix match-with for cljs (#203)
* fix match-with for cljs
1 parent f6c6ed6 commit ccd2099

File tree

8 files changed

+126
-13
lines changed

8 files changed

+126
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ change log follows the conventions of
66
## 3.8.6 / unreleased
77
- fix issue when using non-composite matchers (`m/regex`, `m/pred`, etc)
88
inside `match-with`.
9+
- fix issue where `match-with` misbehaves in ClojureScript
910

1011
## 3.8.5 / 2023-03-24
1112
- fix clj-kondo lint warnings for `match?` in Clojurescript

bb.edn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
{:doc "Starts a nREPL"
1313
:task (apply clojure "-Adev -Sdeps '{:deps {cider/cider-nrepl {:mvn/version \"0.29.0\"}}}' -m nrepl.cmdline --middleware \"[cider.nrepl/cider-middleware]\"" *command-line-args*)}
1414

15+
dev:cljs
16+
{:doc "Starts a ClojureScript nREPL"
17+
:task (apply clojure "-M:cljs-test node-repl" *command-line-args*)}
18+
1519
test:clj
1620
{:doc "run Clojure clojure.test tests"
1721
:task (apply clojure "-A:dev:clj-test:test-runner" *command-line-args*)}

src/cljc/matcher_combinators/matchers.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
[& matchers]
131131
(core/->AllOf matchers))
132132

133-
#?(:cljs (defn- cljs-uri [expected]
133+
#?(:cljs (defn cljs-uri [expected]
134134
(core/->CljsUriEquals expected)))
135135

136136
(defn matcher-for

src/cljc/matcher_combinators/parser.cljc

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,95 @@
1313
function
1414
(-match [this actual]
1515
(core/match (matchers/pred this) actual))
16+
(-matcher-for
17+
([this] (matchers/pred this))
18+
([this _t->m] (matchers/pred this)))
1619

1720
;; equals base types
1821
nil
1922
(-match [this actual]
2023
(core/match (matchers/equals this) actual))
24+
(-matcher-for
25+
([_this] matchers/equals)
26+
([this t->m] (matchers/lookup-matcher this t->m)))
2127

2228
number
2329
(-match [this actual]
2430
(core/match (matchers/equals this) actual))
31+
(-matcher-for
32+
([_this] matchers/equals)
33+
([this t->m] (matchers/lookup-matcher this t->m)))
2534

2635
string
2736
(-match [this actual]
2837
(core/match (matchers/equals this) actual))
38+
(-matcher-for
39+
([_this] matchers/equals)
40+
([this t->m] (matchers/lookup-matcher this t->m)))
2941

3042
boolean
3143
(-match [this actual]
3244
(core/match (matchers/equals this) actual))
45+
(-matcher-for
46+
([_this] matchers/equals)
47+
([this t->m] (matchers/lookup-matcher this t->m)))
3348

3449
Keyword
3550
(-match [this actual]
3651
(core/match (matchers/equals this) actual))
52+
(-matcher-for
53+
([_this] matchers/equals)
54+
([this t->m] (matchers/lookup-matcher this t->m)))
3755

3856
Symbol
3957
(-match [this actual]
4058
(core/match (matchers/equals this) actual))
59+
(-matcher-for
60+
([_this] matchers/equals)
61+
([this t->m] (matchers/lookup-matcher this t->m)))
4162

4263
UUID
4364
(-match [this actual]
4465
(core/match (matchers/equals this) actual))
66+
(-matcher-for
67+
([_this] matchers/equals)
68+
([this t->m] (matchers/lookup-matcher this t->m)))
4569

4670
goog.Uri
4771
(-match [this actual]
4872
(core/match (matchers/cljs-uri this) actual))
73+
(-matcher-for
74+
([_this] matchers/cljs-uri)
75+
([this t->m] (matchers/lookup-matcher this t->m)))
4976

5077
js/Date
5178
(-match [this actual]
5279
(core/match (matchers/equals this) actual))
80+
(-matcher-for
81+
([_this] matchers/equals)
82+
([this t->m] (matchers/lookup-matcher this t->m)))
5383

5484
Var
5585
(-match [this actual]
5686
(core/match (matchers/equals this) actual))
87+
(-matcher-for
88+
([_this] matchers/equals)
89+
([this t->m] (matchers/lookup-matcher this t->m)))
5790

5891
;; equals nested types
5992
Cons
6093
(-match [this actual]
6194
(core/match (matchers/equals this) actual))
95+
(-matcher-for
96+
([_this] matchers/equals)
97+
([this t->m] (matchers/lookup-matcher this t->m)))
6298

6399
Repeat
64100
(-match [this actual]
65101
(core/match (matchers/equals this) actual))
102+
(-matcher-for
103+
([_this] matchers/equals)
104+
([this t->m] (matchers/lookup-matcher this t->m)))
66105

67106
default
68107
(-match [this actual]
@@ -73,10 +112,18 @@
73112
(or (satisfies? ISet this)
74113
(satisfies? ISequential this))
75114
(core/match (matchers/equals this) actual)))
115+
(-matcher-for
116+
([this] (if (satisfies? IMap this)
117+
matchers/embeds
118+
matchers/equals))
119+
([this t->m] (matchers/lookup-matcher this t->m)))
76120

77121
js/RegExp
78122
(-match [this actual]
79-
(core/match (matchers/regex this) actual))))
123+
(core/match (matchers/regex this) actual))
124+
(-matcher-for
125+
([_this] matchers/equals)
126+
([this t->m] (matchers/lookup-matcher this t->m)))))
80127

81128
#?(:clj (do
82129
(defmacro mimic-matcher [matcher t]
@@ -105,6 +152,6 @@
105152
core/Matcher
106153
(-matcher-for
107154
([this] (matchers/pred this))
108-
([this t->m] (matchers/pred this)))
155+
([this t->m] (matchers/lookup-matcher this t->m)))
109156
(-match [this actual]
110157
(core/match (matchers/pred this) actual)))))

test/clj/matcher_combinators/matchers_test.clj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[matcher-combinators.matchers :as m]
1010
[matcher-combinators.result :as result]
1111
[matcher-combinators.test :refer [match?]]
12-
[matcher-combinators.test-helpers :as test-helpers :refer [abs-value-matcher]])
12+
[matcher-combinators.test-helpers :as test-helpers :refer [no-match? abs-value-matcher]])
1313
(:import [matcher_combinators.model Mismatch Missing InvalidMatcherContext InvalidMatcherType]))
1414

1515
(defn any? [_x] true)
@@ -245,9 +245,6 @@
245245
(is (= m/regex
246246
(m/matcher-for #"abc")))))
247247

248-
(defn no-match? [expected actual]
249-
(not (c/indicates-match? (c/match expected actual))))
250-
251248
(deftest match-with-matcher
252249
(testing "processes overrides in order"
253250
(let [matcher (m/match-with [pos? abs-value-matcher
@@ -297,6 +294,10 @@
297294
#{1})
298295
#{1 2}))
299296

297+
(is (match?
298+
(m/match-with [set? m/embeds]
299+
#{(m/pred odd?)})
300+
#{1 2}))
300301
(is (match?
301302
(m/match-with [set? m/embeds]
302303
#{odd?})

test/clj/matcher_combinators/standalone_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns matcher-combinators.standalone-test
2-
(:require [clojure.test :refer [deftest is testing use-fixtures]]
2+
(:require [clojure.test :refer [deftest is testing]]
33
[matcher-combinators.matchers :as m]
44
[matcher-combinators.standalone :as standalone]))
55

test/cljc/matcher_combinators/test_helpers.cljc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
(fn [actual] (= (Math/abs expected)
77
(Math/abs actual)))
88
(str "equal to abs value of " expected)))
9+
10+
(defn no-match? [expected actual]
11+
(not (core/indicates-match? (core/match expected actual))))

test/cljs/matcher_combinators/cljs_example_test.cljs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
(ns matcher-combinators.cljs-example-test
2-
(:require [clojure.test :refer [deftest testing is are]]
2+
(:require [clojure.test :refer [deftest testing is]]
33
[clojure.test.check.generators :as gen]
44
[clojure.test.check.properties :as prop]
55
[clojure.test.check.clojure-test :refer [defspec]]
66
[matcher-combinators.standalone :as standalone]
77
[matcher-combinators.parser]
88
[matcher-combinators.matchers :as m]
9-
[matcher-combinators.core :as c]
10-
[matcher-combinators.test]
11-
[matcher-combinators.test-helpers :as helpers])
9+
[matcher-combinators.test :refer [match? thrown-match?]]
10+
[matcher-combinators.test-helpers :refer [abs-value-matcher no-match?]])
1211
(:import [goog.Uri]))
1312

1413
(def gen-any-equatable
@@ -45,7 +44,12 @@
4544
[2 3 1])))
4645
(testing "`(sort 2)` throws and causes a mismatch"
4746
(is (not (standalone/match? (m/via sort 2)
48-
2)))))
47+
2))))
48+
(testing "via + match-with allows pre-processing `actual` before applying matching"
49+
(is (match? (m/match-with
50+
[vector? (fn [expected] (m/via sort expected))]
51+
{:payloads [1 2 3]})
52+
{:payloads (shuffle [3 2 1])}))))
4953

5054
(deftest exception-matching
5155
(is (thrown-match? ExceptionInfo
@@ -55,6 +59,12 @@
5559
(deftest passing-match
5660
(is (match? {:a 2} {:a 2 :b 1})))
5761

62+
(deftest pred-match
63+
(is (match? #{odd?}
64+
#{1}))
65+
(is (match? #{(m/pred odd?)}
66+
#{1})))
67+
5868
(comment
5969
(deftest match?-no-actual-arg
6070
(testing "fails with nice message when you don't provide an `actual` arg to `match?`"
@@ -68,3 +78,50 @@
6878
(testing "fails with nice message when you don't provide an `actual` arg to `thrown-match?`"
6979
(is (thrown-match? ExceptionInfo {:a 1})
7080
:in-wrong-place))))
81+
82+
(deftest match-with-test
83+
(testing "Example numeric test-case"
84+
(is (match? (m/match-with [number? (m/within-delta 0.05)] 1) 0.99)))
85+
86+
(testing "maps"
87+
(testing "passing case with equals override"
88+
(is (match? (m/match-with [map? m/equals]
89+
{:a :b})
90+
{:a :b})))
91+
(testing "failing case with equals override"
92+
(is (no-match? (m/match-with [map? m/equals]
93+
{:a :b})
94+
{:a :b :d :e})))
95+
(testing "passing case multiple scopes"
96+
(is (match?
97+
{:o (m/match-with [map? m/equals]
98+
{:a
99+
(m/match-with [map? m/embeds]
100+
{:b :c})})}
101+
{:o {:a {:b :c :d :e}}
102+
:p :q})))
103+
(testing "using `absent` matcher"
104+
(is (match? (m/match-with [map? m/equals]
105+
{:a m/absent
106+
:b :c})
107+
{:b :c}))
108+
(is (match? (m/match-with [map? m/embeds]
109+
{:a m/absent})
110+
{:b :c}))))
111+
112+
(testing "sets"
113+
(is (match?
114+
(m/match-with [set? m/embeds]
115+
#{1})
116+
#{1 2}))
117+
118+
(is (match?
119+
(m/match-with [set? m/embeds]
120+
#{(m/pred odd?)})
121+
#{1 2})))
122+
123+
(let [matcher (m/match-with [pos? abs-value-matcher
124+
integer? m/equals]
125+
5)]
126+
(is (match? matcher 5))
127+
(is (match? matcher -5))))

0 commit comments

Comments
 (0)