From 81367e66224e9fd0af4752f67583460df6422369 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Thu, 11 Apr 2013 17:21:55 +1000 Subject: protect against no data --- lib/diff/lcs/hunk.rb | 2 +- spec/hunk_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 -- cgit v1.2.1 From c1373fd0c4e9e398d2f474376e99500ef882d856 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Thu, 11 Apr 2013 17:27:30 +1000 Subject: fix indentation of examples --- spec/hunk_spec.rb | 59 +++++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/spec/hunk_spec.rb b/spec/hunk_spec.rb index 282946b..dc6f532 100644 --- a/spec/hunk_spec.rb +++ b/spec/hunk_spec.rb @@ -15,48 +15,44 @@ describe "Diff::LCS::Hunk" do 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 + expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp) + 1,2c1,2 + < Tu avec carté {count} itém has + --- + > Tu avec carte {count} item has -EOD + 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 @@ -64,11 +60,10 @@ EOD 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 + 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 -- cgit v1.2.1