Skip to content

Commit f6c6ed6

Browse files
authored
Fix match-with when used with non-composite matchers (#213)
* fix for non-nested matchers used in match-with
1 parent c96c46e commit f6c6ed6

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ pom.xml.asc
1010
/.nrepl-port
1111
.hgignore
1212
.hg/
13+
.clj-kondo/*
14+
.shadow-cljs/*

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This
33
change log follows the conventions of
44
[keepachangelog.com](http://keepachangelog.com/).
55

6+
## 3.8.6 / unreleased
7+
- fix issue when using non-composite matchers (`m/regex`, `m/pred`, etc)
8+
inside `match-with`.
9+
610
## 3.8.5 / 2023-03-24
711
- fix clj-kondo lint warnings for `match?` in Clojurescript
812

src/cljc/matcher_combinators/matchers.cljc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(ns matcher-combinators.matchers
2-
(:require #?(:cljs [matcher-combinators.core :as core :refer [Absent]]
2+
(:require #?(:cljs [matcher-combinators.core :as core :refer [Matcher]]
33
:clj [matcher-combinators.core :as core])
44
[clojure.string :as string]
55
[matcher-combinators.utils :as utils])
6-
#?(:clj (:import [matcher_combinators.core Absent])))
6+
#?(:clj (:import [matcher_combinators.core Matcher])))
77

88
(defn- non-internal-record? [v]
99
(and (record? v)
@@ -223,7 +223,8 @@
223223
(and (record? value) (coll? (:expected value)))
224224
(update value :expected match-with-elements overrides)
225225

226-
(= Absent (type value))
226+
;; non-nested matcher like `(m/equals 1)` or `(m/regex #"hi")`
227+
(instance? Matcher value)
227228
value
228229

229230
(map? value)

test/clj/matcher_combinators/matchers_test.clj

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[matcher-combinators.result :as result]
1111
[matcher-combinators.test :refer [match?]]
1212
[matcher-combinators.test-helpers :as test-helpers :refer [abs-value-matcher]])
13-
(:import [matcher_combinators.model Mismatch Missing InvalidMatcherType]))
13+
(:import [matcher_combinators.model Mismatch Missing InvalidMatcherContext InvalidMatcherType]))
1414

1515
(defn any? [_x] true)
1616

@@ -42,6 +42,8 @@
4242
(instance? Missing actual))
4343
(defn invalid-type? [actual]
4444
(instance? InvalidMatcherType actual))
45+
(defn invalid-matcher-context? [actual]
46+
(instance? InvalidMatcherContext actual))
4547

4648
(defn one-mismatch? [mismatch-list]
4749
(= 1 (count (filter #(or (mismatch? %) (missing? %)) mismatch-list))))
@@ -300,6 +302,26 @@
300302
#{odd?})
301303
#{1 2}))))
302304

305+
(testing "non-nested matchers"
306+
(testing "irrelevant match-with doesn't affect results"
307+
(is (match? (m/match-with [map? m/equals]
308+
[:key (m/regex #"valu*")])
309+
[:key "value"]))
310+
(is (match? [:key (m/regex #"valu*")]
311+
[:key "value"]))
312+
313+
(is (match? (m/match-with [map? m/equals]
314+
[:key (m/pred even?)])
315+
[:key 2]))
316+
(is (match? [:key (m/pred even?)]
317+
[:key 2]))
318+
319+
(is (match? (m/match-with [vector? m/equals]
320+
{:key (m/regex #"value")})
321+
{:key "value"}))
322+
(is (match? {:key (m/regex #"value")}
323+
{:key "value"}))))
324+
303325
(testing "multiple scopes"
304326
(let [expected
305327
{:a (m/match-with [map? m/equals]
@@ -431,6 +453,11 @@
431453
::result/value {:expected "seq-of expects a non-empty sequence" :actual []}
432454
::result/weight number?}
433455
(c/match (m/seq-of integer?) [])))
456+
(testing "`seq-of` + `absent` fails because absent should only be used in maps"
457+
(is (match? {::result/type :mismatch
458+
::result/value [invalid-matcher-context?]}
459+
(c/match (m/seq-of m/absent)
460+
[1]))))
434461
(is (match? (m/seq-of {:name string? :id (partial instance? java.util.UUID)})
435462
[{:name "Michael"
436463
:id #uuid "c70e35eb-9eb6-4e3d-b5da-1f7f80932db9"}])))

0 commit comments

Comments
 (0)