# File lib/spec/runner/differs/default.rb, line 49
49: def diff_as_hash(target, expected)
50: contains_hash = false
51: contains_array = false
52:
53: extra_expected_keys = expected.keys - target.keys
54: extra_target_keys = target.keys - expected.keys
55:
56: o = "\n"
57:
58: o << "Expected hash contains keys that target hash does not: " << extra_expected_keys.inspect << "\n" if !extra_expected_keys.empty?
59: o << "Target hash contains keys that expected hash does not: " << extra_target_keys.inspect << "\n" if !extra_target_keys.empty?
60:
61: expected.delete_if do |key, value|
62: contains_hash = true if value.is_a?(Hash)
63: contains_array = true if value.is_a?(Array)
64: target[key] == value
65: end
66:
67: expected.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |key|
68: o << "Expected the key #{key.inspect} to be #{expected[key].inspect}, but was #{target[key].inspect}\n"
69: end
70:
71: o << "\n"
72:
73: if contains_hash || contains_array
74: o << diff_as_object(target, expected)
75: else
76: o
77: end
78: end
# File lib/spec/runner/differs/default.rb, line 45
45: def diff_as_object(target,expected)
46: diff_as_string(PP.pp(target,""), PP.pp(expected,""))
47: end
This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
# File lib/spec/runner/differs/default.rb, line 14
14: def diff_as_string(data_new, data_old)
15: data_old = data_old.split(/\n/).map! { |e| e.chomp }
16: data_new = data_new.split(/\n/).map! { |e| e.chomp }
17: output = ""
18: diffs = Diff::LCS.diff(data_old, data_new)
19: return output if diffs.empty?
20: oldhunk = hunk = nil
21: file_length_difference = 0
22: diffs.each do |piece|
23: begin
24: hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines,
25: file_length_difference)
26: file_length_difference = hunk.file_length_difference
27: next unless oldhunk
28: # Hunks may overlap, which is why we need to be careful when our
29: # diff includes lines of context. Otherwise, we might print
30: # redundant lines.
31: if (context_lines > 0) and hunk.overlaps?(oldhunk)
32: hunk.unshift(oldhunk)
33: else
34: output << oldhunk.diff(format)
35: end
36: ensure
37: oldhunk = hunk
38: output << "\n"
39: end
40: end
41: #Handle the last remaining hunk
42: output << oldhunk.diff(format) << "\n"
43: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.