Object
# File lib/rspec/expectations/differ.rb, line 41 41: def diff_as_object(actual, expected) 42: actual_as_string = object_to_string(actual) 43: expected_as_string = object_to_string(expected) 44: diff = diff_as_string(actual_as_string, expected_as_string) 45: 46: if diff.empty? 47: "#{actual}.==(#{expected}) returned false even though the diff " "between #{actual} and #{expected} is empty. Check the " "implementation of #{actual}.==." 48: else 49: diff 50: end 51: end
This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
# File lib/rspec/expectations/differ.rb, line 9 9: def diff_as_string(data_new, data_old) 10: data_old = data_old.split(/\n/).map! { |e| e.chomp } 11: data_new = data_new.split(/\n/).map! { |e| e.chomp } 12: diffs = Diff::LCS.diff(data_old, data_new) 13: output = "" 14: return output if diffs.empty? 15: oldhunk = hunk = nil 16: file_length_difference = 0 17: diffs.each do |piece| 18: begin 19: hunk = Diff::LCS::Hunk.new( 20: data_old, data_new, piece, context_lines, file_length_difference 21: ) 22: file_length_difference = hunk.file_length_difference 23: next unless oldhunk 24: # Hunks may overlap, which is why we need to be careful when our 25: # diff includes lines of context. Otherwise, we might print 26: # redundant lines. 27: if (context_lines > 0) and hunk.overlaps?(oldhunk) 28: hunk.unshift(oldhunk) 29: else 30: output << oldhunk.diff(format) 31: end 32: ensure 33: oldhunk = hunk 34: output << "\n" 35: end 36: end 37: #Handle the last remaining hunk 38: output << oldhunk.diff(format) << "\n" 39: end
# File lib/rspec/expectations/differ.rb, line 61 61: def context_lines 62: 3 63: end
# File lib/rspec/expectations/differ.rb, line 57 57: def format 58: :unified 59: end
# File lib/rspec/expectations/differ.rb, line 65 65: def object_to_string(object) 66: case object 67: when Hash 68: object.keys.sort_by { |k| k.to_s }.map do |k| 69: %(#{PP.singleline_pp(k, "")} => #{PP.singleline_pp(object[k], "")}) 70: end.join(",\n") 71: when String 72: object =~ /\n/ ? object : object.inspect 73: else 74: PP.pp(object,"") 75: end 76: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.