Skip to content

Make matchers composable - #2

Merged
toy merged 1 commit into
mainfrom
compasability
Oct 26, 2026
Merged

toy merged 1 commit into
mainfrom
compasability

Conversation

@toy

@toy toy commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Make it possible to use all provided matchers in composition using include, match, and, or, …
https://rspec.info/features/3-13/rspec-expectations/composing-matchers/
https://rspec.info/features/3-13/rspec-expectations/compound-expectations/

Comment thread spec/spec_helper.rb Outdated

@myabc myabc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tried this yet, but the code looks good.

Comment thread spec/spec_helper.rb Outdated
Comment thread spec/json_spec/matchers/be_json_eql_spec.rb Outdated

@myabc myabc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@cbliard

cbliard commented Oct 20, 2026

Copy link
Copy Markdown
Member

Just a comment, I find that when the expectations fails, the failure message does not always make it easy to understand what's wrong and how to fix it.

I tried making the "matching" case fail by replacing some values into it:

be_json_equal matcher

Changed be_json_eql({ bar: [2, 3], foo: 1 }.to_json to be_json_eql({ bar: [4, 3], foo: 1 }.to_json.

failure complicated to understand for: #include, #match,
failure easy to understand for: .and, .or

Example failure with #include:

Failure/Error: expect([json]).to include(matching)

expected ["{"foo":1,"bar":[2,3]}"] to include (equal JSON)
Diff:
@@ -1 +1 @@
-[(equal JSON)]
+["{"foo":1,"bar":[2,3]}"]

The provided json producing the failure, bar: [4, 3], does not appear in the error message making debugging it harder.

have_json_path matcher

I changed have_json_path("bar") into have_json_path("zzzbar")

failure complicated to understand for: .and, .or
failure easy to understand for: #include, #match

with #include:

Failure/Error: expect([json]).to include(matching)

expected ["{"foo":1,"bar":[2,3]}"] to include (have JSON path "zzzbar")
Diff:
@@ -1 +1 @@
-[(have JSON path "zzzbar")]
+["{"foo":1,"bar":[2,3]}"]

=> nice.

with .and:

Failure/Error: expect(json).to matching.and(matching)

 Expected JSON path "zzzbar"

...and:

 Expected JSON path "zzzbar"

not sure we can do much here: dumping the entire given json is probably not desirable as it can be very big.

have_json_size matcher

changed have_json_size(2).at_path("bar") into have_json_size(42).at_path("bar")

failure complicated to understand for: -
failure easy to understand for: .and, .or, #include, #match

have_json_type matcher

changed have_json_type(Integer).at_path("foo") to have_json_type(String).at_path("foo")

failure complicated to understand for: -
failure easy to understand for: .and, .or, #include, #match

changed have_json_type(Integer).at_path("foo") to have_json_type(Integer).at_path("baz")

failure complicated to understand for: -
failure easy to understand for: .and, .or, #include, #match

include_json matcher

changed include_json([2, 3].to_json) to include_json([2, 1234].to_json)

failure complicated to understand for: #include, #match
failure easy to understand for: .and, .or

example failure with match:

Failure/Error: expect([json]).to match([matching])

expected ["{"foo":1,"bar":[2,3]}"] to match [(include JSON)]
Diff:
@@ -1 +1 @@
-[(include JSON)]
+["{"foo":1,"bar":[2,3]}"]


I think the issue was already there before and it can still be merged as-is. Just worth knowing that sometimes the failure message can not be helpful and there is room for improvement.

Note: I only tried the positive matchers, not the negated matchers.

@toy

toy commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator Author

@cbliard I agree that description should be done better, but I would do it separately

@toy
toy merged commit 3b26222 into main Oct 26, 2026
5 checks passed
@toy
toy deleted the compasability branch October 26, 2026 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants