Skip to content
New issue

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

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

Already on GitHub? # to your account

Exclude more un-diffable objects #273

Merged
merged 2 commits into from
Nov 8, 2024
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
13 changes: 10 additions & 3 deletions lib/super_diff/rspec/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ def diff

def worth_diffing?
comparing_inequal_values? && !comparing_primitive_values? &&
!comparing_singleline_strings?
!comparing_proc_values? && !comparing_singleline_strings?
end

def comparing_inequal_values?
!helpers.values_match?(expected, actual)
end

def comparing_primitive_values?
expected.is_a?(Symbol) || expected.is_a?(Integer) ||
[true, false, nil].include?(expected)
# strings are indeed primitives, but we still may want to diff them if
# they are multiline strings (see #comparing_singleline_strings?)
return false if expected.is_a?(String)

SuperDiff.primitive?(expected)
end

def comparing_proc_values?
expected.is_a?(Proc)
end

def comparing_singleline_strings?
Expand Down
306 changes: 306 additions & 0 deletions spec/integration/rspec/eq_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,312 @@
end
end

context 'when comparing procs (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = Proc.new { 'hello' }
expected = Proc.new { 'oh, hi!' }
expect(actual).to eq(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 eq(expected)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual '#<Proc>'
plain ' to eq '
expected '#<Proc>'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing booleans (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = true
expected = false
expect(actual).to eq(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 eq(expected)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual 'true'
plain ' to eq '
expected 'false'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing nils (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = nil
expected = 'something else'
expect(actual).to eq(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 eq(expected)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual 'nil'
plain ' to eq '
expected '"something else"'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing symbols (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = :test
expected = :different_symbol
expect(actual).to eq(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 eq(expected)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual ':test'
plain ' to eq '
expected ':different_symbol'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing numerics (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = 4
expected = 2.5
expect(actual).to eq(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 eq(expected)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual '4'
plain ' to eq '
expected '2.5'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing regexps (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = /test/
expected = /another regexp/
expect(actual).to eq(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 eq(expected)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual '/test/'
plain ' to eq '
expected '/another regexp/'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing classes (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
class Test; end
class AnotherClass; end
expect(Test.new).to eq(AnotherClass.new)
TEST
program = make_plain_test_program(snippet, color_enabled: color_enabled)

expected_output =
build_expected_output(
color_enabled: color_enabled,
snippet: 'expect(Test.new).to eq(AnotherClass.new)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual '#<Test>'
plain ' to eq '
expected '#<AnotherClass>'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing Modules (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
module Test; end
module AnotherClass; end
expect(Test).to eq(AnotherClass)
TEST
program = make_plain_test_program(snippet, color_enabled: color_enabled)

expected_output =
build_expected_output(
color_enabled: color_enabled,
snippet: 'expect(Test).to eq(AnotherClass)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual 'Test'
plain ' to eq '
expected 'AnotherClass'
plain '.'
end
end,
diff: nil
)

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

context 'when comparing strings (a kind of primitive)' do
it 'generates the correct expectation and does not generate a diff' do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = 'test'
expected = 'another string'
expect(actual).to eq(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 eq(expected)',
newline_before_expectation: false,
expectation: proc do
line do
plain 'Expected '
actual '"test"'
plain ' to eq '
expected '"another string"'
plain '.'
end
end,
diff: nil
)

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

it_behaves_like 'a matcher that supports elided diffs' do
let(:matcher) { :eq }
end
Expand Down