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

Don't disregard trailing newlines in Differ input. #70 #71

Closed
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
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in rspec-support.gemspec
gemspec

%w[rspec rspec-core rspec-expectations rspec-mocks].each do |lib|
#%w[rspec rspec-core rspec-expectations rspec-mocks].each do |lib|
%w[rspec rspec-core rspec-mocks].each do |lib|
library_path = File.expand_path("../../#{lib}", __FILE__)
if File.exist?(library_path) && !ENV['USE_GIT_REPOS']
gem lib, :path => library_path
Expand All @@ -12,6 +13,8 @@ gemspec
end
end

gem "rspec-expectations", :git => "git://github.com/phiggins/rspec-expectations", :branch => "fix-diff-specs-broken-by-differ-changes"

### dep for ci/coverage
gem 'simplecov', '~> 0.8'

Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/support/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def diff_as_string(actual, expected)
end

if hunks.last
finalize_output(output, hunks.last.diff(format).to_s)
finalize_output(output, hunks.last.diff(format).to_s.strip)
end

color_diff output
Expand Down Expand Up @@ -179,7 +179,7 @@ def object_to_string(object)
when String
object =~ /\n/ ? object : object.inspect
else
PP.pp(object,"")
PP.pp(object,"").chomp
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/support/encoded_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def <<(string)
@string << matching_encoding(string)
end

def split(regex_or_string)
@string.split(matching_encoding(regex_or_string))
def split(regex_or_string, limit=0)
@string.split(matching_encoding(regex_or_string), limit)
end

def to_s
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/support/hunk_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def diffs
end

def expected_lines
@expected.split("\n").map! { |e| e.chomp }
@expected.split("\n", -1).map! { |e| e.chomp }
end

def actual_lines
@actual.split("\n").map! { |e| e.chomp }
@actual.split("\n", -1).map! { |e| e.chomp }
end

def build_hunk(piece)
Expand Down
34 changes: 28 additions & 6 deletions spec/rspec/support/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Support
this
is
soo
@@ -9,6 +9,5 @@
@@ -9,7 +9,6 @@
equal
insert
a
Expand All @@ -49,7 +49,7 @@ module Support
actual = "Tu avec carté {count} itém has\n".encode('UTF-16LE')
expect(differ.diff(actual, expected)).to eql(<<-EOD.encode('UTF-16LE'))

@@ -1,2 +1,2 @@
@@ -1,3 +1,3 @@
-Tu avec carte {count} item has
+Tu avec carté {count} itém has
EOD
Expand All @@ -58,13 +58,13 @@ module Support
it 'handles differently encoded strings that are compatible' do
expected = "abc\n".encode('us-ascii')
actual = "강인철\n".encode('UTF-8')
expect(differ.diff(actual, expected)).to eql "\n@@ -1,2 +1,2 @@\n-abc\n+강인철\n"
expect(differ.diff(actual, expected)).to eql "\n@@ -1,3 +1,3 @@\n-abc\n+강인철\n"
end

it 'uses the default external encoding when the two strings have incompatible encodings' do
expected = "Tu avec carte {count} item has\n"
actual = "Tu avec carté {count} itém has\n".encode('UTF-16LE')
expect(differ.diff(actual, expected)).to eq("\n@@ -1,2 +1,2 @@\n-Tu avec carte {count} item has\n+Tu avec carté {count} itém has\n")
expect(differ.diff(actual, expected)).to eq("\n@@ -1,3 +1,3 @@\n-Tu avec carte {count} item has\n+Tu avec carté {count} itém has\n")
expect(differ.diff(actual, expected).encoding).to eq(Encoding.default_external)
end

Expand Down Expand Up @@ -101,7 +101,7 @@ def inspect

expected_diff = <<'EOD'

@@ -1,5 +1,5 @@
@@ -1,6 +1,6 @@
<Animal
name=bob,
- species=tortoise
Expand Down Expand Up @@ -203,6 +203,22 @@ def inspect
expect(diff).to eq expected_diff
end

it "diffs strings identical except for newlines" do
expected = "this is:\n one string\n"
actual = "this is:\n one string"

expected_diff = <<'EOD'

@@ -1,3 +1,4 @@
this is:
one string
+
EOD

diff = differ.diff(expected,actual)
expect(diff).to eq expected_diff
end

it "splits items with newlines" do
expected_diff = <<'EOD'

Expand Down Expand Up @@ -251,6 +267,12 @@ def inspect
expect(diff).to be_empty
end

it "returns an empty string with two single line strings" do
diff = differ.diff "abc", "def"

expect(diff).to be_empty
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

💭 This wasn't really related to this issue, but it was a missing test case so I threw it in just for completeness's sake.


it "returns a String if no diff is returned" do
diff = differ.diff 1, 2
expect(diff).to be_a(String)
Expand Down Expand Up @@ -289,7 +311,7 @@ def inspect
it "outputs colored diffs" do
expected = "foo bar baz\n"
actual = "foo bang baz\n"
expected_diff = "\e[0m\n\e[0m\e[34m@@ -1,2 +1,2 @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m"
expected_diff = "\e[0m\n\e[0m\e[34m@@ -1,3 +1,3 @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m"

diff = differ.diff(expected,actual)
expect(diff).to eq expected_diff
Expand Down