### Example of Complex Clojure Test with Multiple Asserts Source: https://github.com/healthsamurai/matcho/blob/master/README.md This snippet demonstrates a traditional Clojure test setup with multiple `is` assertions, highlighting the verbosity and repetition that `matcho` aims to solve. It defines a `patch-article` function, sample `patch` and `resp` data, and a `deftest` that manually checks various properties of the response. ```Clojure (ns hello-world.core-test (:require [clojure.test :refer :all] [clojure.spec.alpha :as s] [matcho.core :refer :all :as m])) (defn patch-article [patch] {:status 200 :body (assoc {:article (merge patch {:text "nice article text"})} :article-id 1 :meta {:tags ["nature" "bears"]})}) (def patch {:title "Article about bears" :description "Very good article"}) (def resp (patch-article patch)) (s/def ::str-coll (s/coll-of string?)) (deftest general-patch-test (let [body (:body resp)] (is (< (:status resp) 300)) (is (= (:title patch) (get-in body [:article :title]))) (is (= (:description patch) (get-in body [:article :description]))) (is (s/valid? ::str-coll (get-in body [:meta :tags]))))) ``` -------------------------------- ### Using matcho Core Functions for Pattern Validation in Clojure Source: https://github.com/healthsamurai/matcho/blob/master/README.md This snippet illustrates the usage of `matcho`'s core functions: `m/valid?`, `m/assert`, `m/explain-data`, and `m/dessert`. It shows how to validate data against patterns, assert conditions in tests, get detailed error explanations, and use `dessert` for inverse assertions, demonstrating `matcho`'s flexible validation capabilities. ```Clojure (m/valid? [int? string?] [1 "test"]) ;; => true (deftest int-str-pair-test (m/assert [int? string?] [1 "test"])) (m/explain-data [int? int? string?] [1 "test"]) ;; => [{:expected "#function[clojure.core/int?]", :but "test", :path [1]} {:expected "#function[clojure.core/string?--5132]", :but nil, :path [2]}] (deftest int-str-pair-fail-test (m/assert [int? int? string?] [1 "test"])) ;; [{:expected "#function[clojure.core/int?]", :but "test", :path [1]} {:expected "#function[clojure.core/string?--5132]", :but nil, :path [2]}] [1 "test"] [[#function[clojure.core/int?] #function[clojure.core/int?] #function[clojure.core/string?--5132]]] (deftest dessert-test (m/dessert [int? int?] [1 "test"])) ;; is ok! ``` -------------------------------- ### Using Special Leaf Node Patterns in Matcho (Clojure) Source: https://github.com/healthsamurai/matcho/blob/master/README.md This example illustrates the flexibility of Matcho's pattern leaf values, which can be simple values, regular expressions, Clojure specs, or boolean-valued functions (predicates). It demonstrates `m/valid?` for basic validation, `m/assert` for asserting complex patterns, and `m/explain-data` for detailed error reporting with specs. ```Clojure (s/def ::pos-coll (s/coll-of pos?)) (deftest readme-test (is (m/valid? pos? 1)) (m/assert 1 1) (m/assert {:status #(< % 300) :body #(not (empty? %))} {:status 200 :body "hello"}) (m/assert ::pos-coll [1 2 3]) (m/assert [{:expected #"conforms.*pos-coll"}] (m/explain-data ::pos-coll [1 -1 2]))) ``` -------------------------------- ### Adding matcho Dependency to Clojure deps.edn Source: https://github.com/healthsamurai/matcho/blob/master/README.md This snippet provides the necessary configuration to add `matcho` as a project dependency in a Clojure `deps.edn` file. It specifies the `healthsamurai/matcho` artifact with a `RELEASE` version, allowing the project to use the latest stable version of the library. ```Clojure {healthsamurai/matcho {:mvn/version "RELEASE"}} ``` -------------------------------- ### Requiring matcho Core Namespace in Clojure Source: https://github.com/healthsamurai/matcho/blob/master/README.md This snippet demonstrates how to require the `matcho.core` namespace in a Clojure project. It imports `clojure.test` and `clojure.spec.alpha` along with `matcho.core`, aliasing it as `m` and referring all its public vars, making `matcho` functions and macros available for use. ```Clojure (ns hello-world.core (:require [clojure.test :refer :all] [clojure.spec.alpha :as s] [matcho.core :refer :all :as m])) ``` -------------------------------- ### Comparing Matcho Assertions with Clojure Spec Validation (Clojure) Source: https://github.com/healthsamurai/matcho/blob/master/README.md This section compares `matcho`'s assertion style with `clojure.spec`. It shows that while `clojure.spec` requires explicit spec definitions, `matcho` allows for more concise, inline pattern matching, making it suitable for quick and direct assertions without the overhead of defining formal specs. ```Clojure (def response {:status 200 :body "ok"}) (deftest with-spec-test (s/def ::status #(= 200 %)) (s/def ::body #(not-empty %)) (s/def ::response (s/keys :req-un [::status ::body])) (is (s/valid? ::response response))) (deftest without-spec-test (m/assert {:status 200 :body not-empty} response)) ``` -------------------------------- ### Declarative Pattern Matching with matcho.assert in Clojure Source: https://github.com/healthsamurai/matcho/blob/master/README.md This snippet shows how `matcho` simplifies the previous test by defining a declarative pattern. It uses `matcho.core/assert` to validate the `resp` against the `pattern`, which includes a predicate for status and nested maps for article and meta data, significantly reducing boilerplate. ```Clojure (deftest matcho-patch-test (def pattern {:status #(< % 300) :body {:article patch :meta {:tags ::str-coll}}}) (m/assert pattern resp)) ``` -------------------------------- ### Demonstrating Open-World Assumption in matcho Validations in Clojure Source: https://github.com/healthsamurai/matcho/blob/master/README.md This snippet demonstrates `matcho`'s open-world assumption, where a pattern can be a subset of the value being checked. It shows that `m/valid?` returns true if the pattern matches the specified parts, even if the value has additional keys, but returns false if a required pattern key is missing from the value. ```Clojure (m/valid? {:status 200} {:status 200 :body "ok"}) ;; => true (m/valid? {:status 200 :body string?} {:status 200}) ;; => false ``` -------------------------------- ### Enforcing Strictness in Map and Vector Matching with Matcho (Clojure) Source: https://github.com/healthsamurai/matcho/blob/master/README.md This snippet demonstrates how to use `^:matcho/strict` metadata to ensure that maps or vectors do not contain any additional, unexpected elements or keys beyond what is specified in the pattern. It highlights how strictness prevents exposure of sensitive data and notes that strictness is not inherited by nested data structures. ```Clojure (deftest user-sensitive-data-test (testing "open-world exposes sensitive data" (m/assert {:body {:username string? :age int?}} {:body {:username "bob" :age 42 :password "my-password"}})) (testing "closed-world will catch accidentially exposed password" (m/dessert {:body ^:matcho/strict {:username string? :age int?}} {:body {:username "bob" :age 42 :password "my-password"}}))) (deftest vector-strict-match (def vector-123 [1 2 3]) (m/assert [1 2] [1 2 3]) (m/dessert ^:matcho/strict [1 2] [1 2 3]) (m/assert ^:matcho/strict [1 2] [1 2]) ;; ^:matcho/strict works only for current element of the pattern and ;; not inherited by nested nodes (m/assert ^:matcho/strict {:a [1 2]} {:a [1 2 3]})) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.