Skip to content

Commit

Permalink
Fix match-with when used with non-composite matchers (#213)
Browse files Browse the repository at this point in the history
* fix for non-nested matchers used in match-with
  • Loading branch information
philomates authored Jul 18, 2023
1 parent c96c46e commit f6c6ed6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pom.xml.asc
/.nrepl-port
.hgignore
.hg/
.clj-kondo/*
.shadow-cljs/*
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This
change log follows the conventions of
[keepachangelog.com](http://keepachangelog.com/).

## 3.8.6 / unreleased
- fix issue when using non-composite matchers (`m/regex`, `m/pred`, etc)
inside `match-with`.

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

Expand Down
7 changes: 4 additions & 3 deletions src/cljc/matcher_combinators/matchers.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns matcher-combinators.matchers
(:require #?(:cljs [matcher-combinators.core :as core :refer [Absent]]
(:require #?(:cljs [matcher-combinators.core :as core :refer [Matcher]]
:clj [matcher-combinators.core :as core])
[clojure.string :as string]
[matcher-combinators.utils :as utils])
#?(:clj (:import [matcher_combinators.core Absent])))
#?(:clj (:import [matcher_combinators.core Matcher])))

(defn- non-internal-record? [v]
(and (record? v)
Expand Down Expand Up @@ -223,7 +223,8 @@
(and (record? value) (coll? (:expected value)))
(update value :expected match-with-elements overrides)

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

(map? value)
Expand Down
29 changes: 28 additions & 1 deletion test/clj/matcher_combinators/matchers_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[matcher-combinators.result :as result]
[matcher-combinators.test :refer [match?]]
[matcher-combinators.test-helpers :as test-helpers :refer [abs-value-matcher]])
(:import [matcher_combinators.model Mismatch Missing InvalidMatcherType]))
(:import [matcher_combinators.model Mismatch Missing InvalidMatcherContext InvalidMatcherType]))

(defn any? [_x] true)

Expand Down Expand Up @@ -42,6 +42,8 @@
(instance? Missing actual))
(defn invalid-type? [actual]
(instance? InvalidMatcherType actual))
(defn invalid-matcher-context? [actual]
(instance? InvalidMatcherContext actual))

(defn one-mismatch? [mismatch-list]
(= 1 (count (filter #(or (mismatch? %) (missing? %)) mismatch-list))))
Expand Down Expand Up @@ -300,6 +302,26 @@
#{odd?})
#{1 2}))))

(testing "non-nested matchers"
(testing "irrelevant match-with doesn't affect results"
(is (match? (m/match-with [map? m/equals]
[:key (m/regex #"valu*")])
[:key "value"]))
(is (match? [:key (m/regex #"valu*")]
[:key "value"]))

(is (match? (m/match-with [map? m/equals]
[:key (m/pred even?)])
[:key 2]))
(is (match? [:key (m/pred even?)]
[:key 2]))

(is (match? (m/match-with [vector? m/equals]
{:key (m/regex #"value")})
{:key "value"}))
(is (match? {:key (m/regex #"value")}
{:key "value"}))))

(testing "multiple scopes"
(let [expected
{:a (m/match-with [map? m/equals]
Expand Down Expand Up @@ -431,6 +453,11 @@
::result/value {:expected "seq-of expects a non-empty sequence" :actual []}
::result/weight number?}
(c/match (m/seq-of integer?) [])))
(testing "`seq-of` + `absent` fails because absent should only be used in maps"
(is (match? {::result/type :mismatch
::result/value [invalid-matcher-context?]}
(c/match (m/seq-of m/absent)
[1]))))
(is (match? (m/seq-of {:name string? :id (partial instance? java.util.UUID)})
[{:name "Michael"
:id #uuid "c70e35eb-9eb6-4e3d-b5da-1f7f80932db9"}])))
Expand Down

0 comments on commit f6c6ed6

Please sign in to comment.