Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Adjust cucumber scenarios for diff-lcs
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Jun 28, 2020
1 parent 9d502ec commit f839e69
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
71 changes: 70 additions & 1 deletion features/custom_matchers/define_diffable_matcher.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,33 @@ Feature: define diffable matcher

When a matcher is defined as diffable, the output will include a diff of the submitted objects when the objects are more than simple primitives.

Scenario: define a diffable matcher
@skip-when-diff-lcs-1.3
Scenario: define a diffable matcher (with diff-lcs 1.4)
Given a file named "diffable_matcher_spec.rb" with:
"""ruby
RSpec::Matchers.define :be_just_like do |expected|
match do |actual|
actual == expected
end
diffable
end
RSpec.describe "two\nlines" do
it { is_expected.to be_just_like("three\nlines") }
end
"""
When I run `rspec ./diffable_matcher_spec.rb`
Then it should fail with:
"""
Diff:
@@ -1 +1 @@
-three
+two
"""

@skip-when-diff-lcs-1.4
Scenario: define a diffable matcher (with diff-lcs 1.3)
Given a file named "diffable_matcher_spec.rb" with:
"""ruby
RSpec::Matchers.define :be_just_like do |expected|
Expand All @@ -27,10 +53,53 @@ Feature: define diffable matcher
lines
"""

@skip-when-diff-lcs-1.3
Scenario: Redefine actual

Sometimes is neccessary to overwrite actual to make diffing work, e.g. if `actual` is a name of a file you want to read from. For this to work you need to overwrite `@actual` in your matcher.

Given a file named "redefine_actual_matcher_spec.rb" with:
"""ruby
RSpec::Matchers.define :have_content do |expected|
match do |actual|
@actual = File.read(actual).chomp
values_match? expected, @actual
end
diffable
end
RSpec.describe 'Compare files' do
context 'when content is equal' do
it { expect('data.txt').to have_content 'Data' }
end
context 'when files are different' do
it { expect('data.txt').to have_content "No\nData\nhere" }
end
end
"""
And a file named "data.txt" with:
"""
Data
"""
When I run `rspec ./redefine_actual_matcher_spec.rb --format documentation`
Then the exit status should not be 0
And the output should contain:
"""
2 examples, 1 failure
"""
And the output should contain:
"""
@@ -1,4 +1,6 @@
-No
Data
-here
"""

@skip-when-diff-lcs-1.4
Scenario: Redefine actual (with diff-lcs 1.3)
Given a file named "redefine_actual_matcher_spec.rb" with:
"""ruby
RSpec::Matchers.define :have_content do |expected|
Expand Down
30 changes: 29 additions & 1 deletion features/diffing.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,35 @@ Feature: diffing
string
"""

Scenario: diff for a multiline string and a regexp
@skip-when-diff-lcs-1.3
Scenario: diff for a multiline string and a regexp on diff-lcs 1.4
Given a file named "example_spec.rb" with:
"""ruby
RSpec.describe "a multiline string" do
it "is like another string" do
expected = /expected/m
actual = <<-ACTUAL
this is the
actual
string
ACTUAL
expect(actual).to match expected
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain:
"""
Diff:
@@ -1,3 +1,5 @@
-/expected/m
+this is the
+ actual
+ string
"""

@skip-when-diff-lcs-1.4
Scenario: diff for a multiline string and a regexp on diff-lcs 1.3
Given a file named "example_spec.rb" with:
"""ruby
RSpec.describe "a multiline string" do
Expand Down
15 changes: 15 additions & 0 deletions features/support/diff_lcs_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Around "@skip-when-diff-lcs-1.4" do |scenario, block|
if Diff::LCS::VERSION.to_f >= 1.4
warn "Skipping scenario #{scenario.title} on #{Diff::LCS::VERSION.to_f}"
else
block.call
end
end

Around "@skip-when-diff-lcs-1.3" do |scenario, block|
if Diff::LCS::VERSION.to_f < 1.4
warn "Skipping scenario #{scenario.title} on #{Diff::LCS::VERSION.to_f}"
else
block.call
end
end

0 comments on commit f839e69

Please sign in to comment.