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

TODO add colour support TODO add some rdoc

Methods

Public Class methods

[Source]

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

Public Instance methods

[Source]

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

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

[Source]

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

[Validate]