3 require 'diff/lcs' #necessary due to loading bug on some machines - not sure why - DaC
4 require 'diff/lcs/hunk'
5 rescue LoadError ; raise "You must gem install diff-lcs to use diffing" ; end
15 def initialize(options)
19 # This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
20 def diff_as_string(data_old, data_new)
21 data_old = data_old.split(/\n/).map! { |e| e.chomp }
22 data_new = data_new.split(/\n/).map! { |e| e.chomp }
24 diffs = Diff::LCS.diff(data_old, data_new)
25 return output if diffs.empty?
27 file_length_difference = 0
30 hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines,
31 file_length_difference)
32 file_length_difference = hunk.file_length_difference
34 # Hunks may overlap, which is why we need to be careful when our
35 # diff includes lines of context. Otherwise, we might print
37 if (context_lines > 0) and hunk.overlaps?(oldhunk)
40 output << oldhunk.diff(format)
47 #Handle the last remaining hunk
48 output << oldhunk.diff(format) << "\n"
51 def diff_as_object(target,expected)
52 diff_as_string(PP.pp(target,""), PP.pp(expected,""))
61 @options.context_lines