summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Rowe <hello@jonrowe.co.uk>2013-04-11 17:21:55 +1000
committerJon Rowe <hello@jonrowe.co.uk>2013-04-11 17:26:07 +1000
commit81367e66224e9fd0af4752f67583460df6422369 (patch)
tree24e03106a1bb376629692d79ba4d419e4b390c3c
parent498a97c9089ea377d85f03470ecef1c1a5c6330a (diff)
downloaddiff-lcs-81367e66224e9fd0af4752f67583460df6422369.tar.gz
protect against no data
-rw-r--r--lib/diff/lcs/hunk.rb2
-rw-r--r--spec/hunk_spec.rb14
2 files changed, 15 insertions, 1 deletions
diff --git a/lib/diff/lcs/hunk.rb b/lib/diff/lcs/hunk.rb
index 6c5ea6a..05c3fb6 100644
--- a/lib/diff/lcs/hunk.rb
+++ b/lib/diff/lcs/hunk.rb
@@ -12,7 +12,7 @@ class Diff::LCS::Hunk
# 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
diff --git a/spec/hunk_spec.rb b/spec/hunk_spec.rb
index 0741b87..282946b 100644
--- a/spec/hunk_spec.rb
+++ b/spec/hunk_spec.rb
@@ -59,5 +59,19 @@ Tu avec carte {count} item has
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.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