diff --git a/.gitignore b/.gitignore index ea53ce9..1d5bde7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ pom.xml.asc /.nrepl-port .hgignore .hg/ +.clj-kondo/* +.shadow-cljs/* diff --git a/CHANGELOG.md b/CHANGELOG.md index 27d735c..7295ec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/cljc/matcher_combinators/matchers.cljc b/src/cljc/matcher_combinators/matchers.cljc index c8fb00c..5eaeda7 100644 --- a/src/cljc/matcher_combinators/matchers.cljc +++ b/src/cljc/matcher_combinators/matchers.cljc @@ -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) @@ -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) diff --git a/test/clj/matcher_combinators/matchers_test.clj b/test/clj/matcher_combinators/matchers_test.clj index e628388..755928b 100644 --- a/test/clj/matcher_combinators/matchers_test.clj +++ b/test/clj/matcher_combinators/matchers_test.clj @@ -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) @@ -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)))) @@ -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] @@ -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"}])))