Skip to content

Commit

Permalink
Rename to nested-equals
Browse files Browse the repository at this point in the history
  • Loading branch information
gilvan-reis committed Feb 1, 2024
1 parent 6f8cef9 commit d04a7fc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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.9.0 / 2024-01-26
- Add `strictly-equals` matcher, which always uses `equals` matcher at every level of nesting.
## 3.9.0 / 2024-02-01
- Add `nested-equals` matcher, which always uses `equals` matcher at every level of nesting.

## 3.8.8 / 2023-09-04
- refine abbreviation logic to not descend into fully mismatched data, because
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ for a specific value, e.g.
- Note: Given that the default matcher for maps is `embeds`, nested maps continue being matched with embeds (instead of also being matched with `equals`). Check out 'Overriding default matchers' below for instructions on how to match nested maps with equals too.
- sequence: matches when the `expected` sequences's matchers match the given sequence. Similar to midje's `(just expected)`
- set: matches when all the elements in the given set can be matched with a matcher in `expected` set and each matcher is used exactly once.
- `strictly equals`: overrides map's default matchers to `equals`, using it for nested structures (see Overriding default matchers, below)
- `nested-equals`: overrides map's default matchers to `equals`, using it for nested structures (see Overriding default matchers, below)
- `embeds` operates over maps, sequences, and sets
- map: matches when the map contains some of the same key/values as the `expected` map.
- sequence: order-agnostic matcher that will match when provided a subset of the `expected` sequence. Similar to midje's `(contains expected :in-any-order :gaps-ok)`
Expand Down Expand Up @@ -265,11 +265,11 @@ For example, if you want to do exact map matching you need to use a log of `m/eq
{:a {:b {:c 1 :extra-c 0} :extra-b 0} :extra-a 0})))
```

For convenience we've also added the built-in matcher `strictly-equals` to reduce this verbosity:
For convenience we've also added the built-in matcher `nested-equals` to reduce this verbosity:

``` clojure
(deftest exact-map-matching-with-match-with
(is (match? (m/strictly-equals {:a {:b {:c odd?}}}))
(is (match? (m/nested-equals {:a {:b {:c odd?}}}))
{:a {:b {:c 1}}}))
```

Expand Down
2 changes: 1 addition & 1 deletion src/cljc/matcher_combinators/matchers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

(declare match-with)

(defn strictly-equals
(defn nested-equals
"A matcher that always uses the `equals` matcher at every level of nesting.
Useful given that matchers usually only change the first level of the data
Expand Down
18 changes: 9 additions & 9 deletions test/clj/matcher_combinators/matchers_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,16 @@
:d (m/embeds {:e {:inner-e {:x 1 :y 2}}})}})
actual)))))

(deftest strictly-equals-matcher
(deftest nested-equals-matcher
(testing "nested maps"
(testing "passing case"
(is (match? (m/strictly-equals {:user1 {:id 5 :name "hennix"}
(is (match? (m/nested-equals {:user1 {:id 5 :name "hennix"}
:user2 {:id 3 :name "flynt"}})
{:user1 {:id 5 :name "hennix"}
:user2 {:id 3 :name "flynt"}})))

(testing "`strictly-equals` fails when nested maps have extra keys"
(is (no-match? (m/strictly-equals {:user1 {:id 5}
(testing "`nested-equals` fails when nested maps have extra keys"
(is (no-match? (m/nested-equals {:user1 {:id 5}
:user2 {:id 3}})
{:user1 {:id 5 :name "hennix"}
:user2 {:id 3 :name "flynt"}})))
Expand All @@ -381,24 +381,24 @@
{:user1 {:id 5 :name "hennix"}
:user2 {:id 3 :name "flynt"}})))

(testing "`strictly-equals` is similar to using `equals` in each map"
(testing "`nested-equals` is similar to using `equals` in each map"
(is (no-match? (m/equals {:user1 (m/equals {:id 5})
:user2 (m/equals {:id 3})})
{:user1 {:id 5 :name "hennix"}
:user2 {:id 3 :name "flynt"}}))))

(testing "functions"
(testing "`strictly-equals` does not apply `equals` to functions"
(is (match? (m/strictly-equals {:x odd?})
(testing "`nested-equals` does not apply `equals` to functions"
(is (match? (m/nested-equals {:x odd?})
{:x 1})))

(testing "`equals` should be aplied directly to the function to fail"
(is (no-match? (m/equals {:x (m/equals odd?)})
{:x 1}))))

(testing "regex"
(testing "`strictly-equals` does not apply `equals` to regex"
(is (match? (m/strictly-equals {:x (m/regex #"\w+")})
(testing "`nested-equals` does not apply `equals` to regex"
(is (match? (m/nested-equals {:x (m/regex #"\w+")})
{:x "abc"})))

(testing "`equals` should be aplied directly to the regex to fail"
Expand Down

0 comments on commit d04a7fc

Please sign in to comment.