Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix an issue caused by detecting the encoding when first data ia empty #17

Merged
merged 2 commits into from

2 participants

@JonRowe

If the first data passed to Diff::LCS::Hunk is empty then we get an error due to the encoding detection introduced in 1.2.2.
This attempts to use the old data as an encoding source, falls back to the new data as an encoding source and then finally to a string literal.

@JonRowe JonRowe referenced this pull request in rspec/rspec-expectations
Merged

Upgrade to diff-lcs 1.2.2 #239

@halostatue
Owner

I'll merge this as soon as I file a bug with Rubinius for the failed build in 1.9 mode—I need to be able to make a test case for them to see the problem in a somewhat better way. I'm treating it similar to the MacRuby issue; everyone else gets it right, why doesn't rbx?

That should be by the afternoon, and I'll do a release this evening.

@halostatue halostatue merged commit 68e1a15 into from
@JonRowe

Hey thanks! Could you ping me (via mention or whatever) when you've rolled a release? We're awaiting it over on RSpec :)

@halostatue
Owner

@JonRowe the release is complete.

@JonRowe

Thanks @halostatue! Appreciate it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 11, 2013
  1. @JonRowe

    protect against no data

    JonRowe authored
  2. @JonRowe

    fix indentation of examples

    JonRowe authored
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 29 deletions.
  1. +1 −1  lib/diff/lcs/hunk.rb
  2. +37 −28 spec/hunk_spec.rb
View
2  lib/diff/lcs/hunk.rb
@@ -12,7 +12,7 @@ def initialize(data_old, data_new, piece, flag_context, file_length_difference)
# At first, a hunk will have just one Block in it
@blocks = [ Diff::LCS::Block.new(piece) ]
if String.method_defined?(:encoding)
- @preferred_data_encoding = data_old[0].encoding
+ @preferred_data_encoding = data_old.fetch(0, data_new.fetch(0,'') ).encoding
end
@data_old = data_old
@data_new = data_new
View
65 spec/hunk_spec.rb
@@ -15,49 +15,58 @@ def h(v)
let(:hunk) { Diff::LCS::Hunk.new(old_data, new_data, pieces[0], 3, 0) }
it 'should be able to produce a unified diff from the two pieces' do
- expected =
-(<<-EOD.encode('UTF-16LE').chomp)
-@@ -1,2 +1,2 @@
--Tu avec carté {count} itém has
-+Tu avec carte {count} item has
-EOD
+ expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
+ @@ -1,2 +1,2 @@
+ -Tu avec carté {count} itém has
+ +Tu avec carte {count} item has
+ EOD
expect(hunk.diff(:unified).to_s == expected).to eql true
end
it 'should be able to produce a context diff from the two pieces' do
- expected =
-(<<-EOD.encode('UTF-16LE').chomp)
-***************
-*** 1,2 ****
-!Tu avec carté {count} itém has
---- 1,2 ----
-!Tu avec carte {count} item has
-EOD
+ expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
+ ***************
+ *** 1,2 ****
+ !Tu avec carté {count} itém has
+ --- 1,2 ----
+ !Tu avec carte {count} item has
+ EOD
expect(hunk.diff(:context).to_s == expected).to eql true
end
it 'should be able to produce an old diff from the two pieces' do
- expected =
-(<<-EOD.encode('UTF-16LE').chomp)
-1,2c1,2
-< Tu avec carté {count} itém has
----
-> Tu avec carte {count} item has
-
-EOD
+ expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
+ 1,2c1,2
+ < Tu avec carté {count} itém has
+ ---
+ > Tu avec carte {count} item has
+
+ EOD
expect(hunk.diff(:old).to_s == expected).to eql true
end
it 'should be able to produce a reverse ed diff from the two pieces' do
- expected =
-(<<-EOD.encode('UTF-16LE').chomp)
-c1,2
-Tu avec carte {count} item has
-.
+ expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
+ c1,2
+ Tu avec carte {count} item has
+ .
-EOD
+ EOD
expect(hunk.diff(:reverse_ed).to_s == expected).to eql true
end
+
+ context 'with empty first data set' do
+ let(:old_data) { [] }
+
+ it 'should be able to produce a unified diff' do
+ expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
+ @@ -1 +1,2 @@
+ +Tu avec carte {count} item has
+ EOD
+ expect(hunk.diff(:unified).to_s == expected).to eql true
+ end
+ end
+
end
end
Something went wrong with that request. Please try again.