Class Spec::Expectations::Differs::Default
In: lib/spec/expectations/differs/default.rb
Parent: Object

TODO add some rdoc

Methods

Public Class methods

[Source]

    # File lib/spec/expectations/differs/default.rb, line 15
15:         def initialize(format=:unified,context_lines=nil,colour=nil)
16: 
17:           context_lines ||= 3
18:           colour        ||= false
19: 
20:           @format,@context_lines,@colour = format,context_lines,colour
21:         end

Public Instance methods

[Source]

    # File lib/spec/expectations/differs/default.rb, line 55
55:         def diff_as_object(target,expected)
56:           diff_as_string(PP.pp(target,""), PP.pp(expected,""))
57:         end

This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)

[Source]

    # File lib/spec/expectations/differs/default.rb, line 24
24:         def diff_as_string(data_old, data_new)
25:           data_old = data_old.split(/\n/).map! { |e| e.chomp }
26:           data_new = data_new.split(/\n/).map! { |e| e.chomp }
27:           output = ""
28:           diffs = Diff::LCS.diff(data_old, data_new)
29:           return output if diffs.empty?
30:           oldhunk = hunk = nil  
31:           file_length_difference = 0
32:           diffs.each do |piece|
33:             begin
34:               hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, @context_lines,
35:                                          file_length_difference)
36:               file_length_difference = hunk.file_length_difference      
37:               next unless oldhunk      
38:               # Hunks may overlap, which is why we need to be careful when our
39:               # diff includes lines of context. Otherwise, we might print
40:               # redundant lines.
41:               if (@context_lines > 0) and hunk.overlaps?(oldhunk)
42:                 hunk.unshift(oldhunk)
43:               else
44:                 output << oldhunk.diff(@format)
45:               end
46:             ensure
47:               oldhunk = hunk
48:               output << "\n"
49:             end
50:           end  
51:           #Handle the last remaining hunk
52:           output << oldhunk.diff(@format) << "\n"
53:         end

[Validate]