Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Condense built-in and DSL RSpec matcher inspection output #203

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/super_diff/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def self.rspec_version
ObjectInspection::InspectionTreeBuilders::KindOf,
ObjectInspection::InspectionTreeBuilders::ObjectHavingAttributes,
# ObjectInspection::InspectionTreeBuilders::Primitive,
ObjectInspection::InspectionTreeBuilders::ValueWithin
ObjectInspection::InspectionTreeBuilders::ValueWithin,
ObjectInspection::InspectionTreeBuilders::RSpecMatcher
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ module InspectionTreeBuilders
:Primitive,
"super_diff/rspec/object_inspection/inspection_tree_builders/primitive"
)
autoload(
:RSpecMatcher,
"super_diff/rspec/object_inspection/inspection_tree_builders/rspec_matcher"
)
autoload(
:ValueWithin,
"super_diff/rspec/object_inspection/inspection_tree_builders/value_within"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module SuperDiff
module RSpec
module ObjectInspection
module InspectionTreeBuilders
class RSpecMatcher < SuperDiff::ObjectInspection::InspectionTreeBuilders::Base
def self.applies_to?(value)
value.is_a?(::RSpec::Matchers::BuiltIn::BaseMatcher) ||
value.is_a?(::RSpec::Matchers::DSL::Matcher)
end

def call
SuperDiff::ObjectInspection::InspectionTree.new do
add_text { |object| "#<#{object.description}>" }
end
end
end
end
end
end
end
86 changes: 86 additions & 0 deletions spec/integration/rspec/unhandled_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require "spec_helper"

RSpec.describe "Integration with built-in RSpec matchers", type: :integration do
context "when the expected value contains a built-in matcher" do
context "that fails" do
it "produces the correct failure message when used in the negative" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
expected = hash_including(
number: be_a(Numeric)
)
actual = {
number: 4
}
expect(actual).not_to match(expected)
TEST
program =
make_plain_test_program(snippet, color_enabled: color_enabled)

expected_output =
build_expected_output(
color_enabled: color_enabled,
snippet: "expect(actual).not_to match(expected)",
expectation:
proc do
line do
plain "Expected "
actual "{ number: 4 }"
plain " not to match "
expected "#<a hash including (number: #<be a kind of Numeric>)>"
plain "."
end
end
)

expect(program).to produce_output_when_run(expected_output).in_color(
color_enabled
)
end
end

it "produces the correct failure message when used in the positive" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
expected = hash_including(
number: be_a(Numeric)
)
actual = {
number: "not a number"
}
expect(actual).to match(expected)
TEST
program =
make_plain_test_program(snippet, color_enabled: color_enabled)

expected_output =
build_expected_output(
color_enabled: color_enabled,
snippet: "expect(actual).to match(expected)",
expectation:
proc do
line do
plain "Expected "
actual %|{ number: "not a number" }|
plain " to match "
expected "#<a hash including (number: #<be a kind of Numeric>)>"
plain "."
end
end,
diff:
proc do
plain_line " {"
expected_line "- number: #<be a kind of Numeric>"
actual_line %|+ number: "not a number"|
plain_line " }"
end
)

expect(program).to produce_output_when_run(expected_output).in_color(
color_enabled
)
end
end
end
end
end
91 changes: 91 additions & 0 deletions spec/unit/rspec/object_inspection/rspec_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require "spec_helper"

RSpec.describe SuperDiff, type: :unit do
describe ".inspect_object", "for RSpec matchers" do
context "given a custom matcher" do
let(:custom_matcher) do
proc do |expected, &block_arg|
declarations =
proc do
match do |actual|
actual.is_a?(Integer) && actual >= 0 &&
(Math.sqrt(actual) % 1).zero?
end
description { "be a perfect square" }
end
RSpec::Matchers::DSL::Matcher.new(
:be_a_square,
declarations,
RSpec::Matchers::DSL,
*expected,
&block_arg
)
end
end

context "given as_lines: false" do
it "returns the matcher's description string" do
string =
described_class.inspect_object(
custom_matcher.call(4),
as_lines: false
)
expect(string).to eq("#<be a perfect square>")
end
end

context "given as_lines: true" do
it "returns an inspected version of the matcher as multiple lines" do
tiered_lines =
described_class.inspect_object(
custom_matcher.call(4),
as_lines: true,
type: :delete,
indentation_level: 1
)
expect(tiered_lines).to match(
[
an_object_having_attributes(
type: :delete,
indentation_level: 1,
value: "#<be a perfect square>"
)
]
)
end
end
end

context "given a built-in matcher" do
let(:matcher) { be_a(Numeric) }

context "given as_lines: false" do
it "returns the matcher's description string" do
string = described_class.inspect_object(matcher, as_lines: false)
expect(string).to eq("#<be a kind of Numeric>")
end
end

context "given as_lines: true" do
it "returns an inspected version of the matcher as multiple lines" do
tiered_lines =
described_class.inspect_object(
matcher,
as_lines: true,
type: :delete,
indentation_level: 1
)
expect(tiered_lines).to match(
[
an_object_having_attributes(
type: :delete,
indentation_level: 1,
value: "#<be a kind of Numeric>"
)
]
)
end
end
end
end
end