]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/expectations/differs/default.rb
Show whether a trace is public or private in the trace list, so that a user can easil...
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / expectations / differs / default.rb
1 begin
2   require 'rubygems'
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
6
7 require 'pp'
8
9 module Spec
10   module Expectations
11     module Differs
12
13       # TODO add some rdoc
14       class Default
15         def initialize(options)
16           @options = options
17         end
18
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 }
23           output = ""
24           diffs = Diff::LCS.diff(data_old, data_new)
25           return output if diffs.empty?
26           oldhunk = hunk = nil  
27           file_length_difference = 0
28           diffs.each do |piece|
29             begin
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      
33               next unless oldhunk      
34               # Hunks may overlap, which is why we need to be careful when our
35               # diff includes lines of context. Otherwise, we might print
36               # redundant lines.
37               if (context_lines > 0) and hunk.overlaps?(oldhunk)
38                 hunk.unshift(oldhunk)
39               else
40                 output << oldhunk.diff(format)
41               end
42             ensure
43               oldhunk = hunk
44               output << "\n"
45             end
46           end  
47           #Handle the last remaining hunk
48           output << oldhunk.diff(format) << "\n"
49         end  
50
51         def diff_as_object(target,expected)
52           diff_as_string(PP.pp(target,""), PP.pp(expected,""))
53         end
54
55         protected
56         def format
57           @options.diff_format
58         end
59
60         def context_lines
61           @options.context_lines
62         end
63       end
64     end
65   end
66 end