Skip to content

Commit

Permalink
Merge pull request #17 from JonRowe/guard_against_empty
Browse files Browse the repository at this point in the history
Fix an issue caused by detecting the encoding when first data is empty
  • Loading branch information
halostatue committed Apr 11, 2013
2 parents 498a97c + c1373fd commit 68e1a15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/diff/lcs/hunk.rb
Expand Up @@ -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
Expand Down
65 changes: 37 additions & 28 deletions spec/hunk_spec.rb
Expand Up @@ -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

0 comments on commit 68e1a15

Please sign in to comment.