Class: RSpec::Support::Differ
- Inherits:
-
Object
- Object
- RSpec::Support::Differ
- Defined in:
- lib/rspec_hashdiff_helper.rb
Overview
rubocop:disable Style/Documentation
Instance Method Summary collapse
- #diff_as_object(actual, expected) ⇒ Object
- #find_contain_nodes(object, current_path = []) ⇒ Object
- #format_hashes_for_diff(actual, expected) ⇒ Object
- #strip_matchers(expected) ⇒ Object
Instance Method Details
#diff_as_object(actual, expected) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rspec_hashdiff_helper.rb', line 59 def diff_as_object(actual, expected) if actual.instance_of?(Hash) && expected.instance_of?(Hash) mod_actual, mod_expected = format_hashes_for_diff(actual, expected) diff = Hashdiff.diff(mod_actual, mod_expected, delimiter: ':', array_path: true, similarity: 1, strict: false, use_lcs: false, indifferent: true) diff_str = diff.sort_by { |x| x[1].map(&:to_s) }.map do |diff_line| keys = diff_line[1] case diff_line[0] when '~' "+ #{keys.map { |key| "{#{key}:" }.join} #{diff_line[2]} #{Array.new(keys.length) { '}' }.join}\n" \ "- #{keys.map { |key| "{#{key}:" }.join} #{diff_line[3]} #{Array.new(keys.length) { '}' }.join}\n" when '+', '-' "#{diff_line[0]} #{keys.map { |key| "{#{key}:" }.join}"\ " #{diff_line[2]} #{Array.new(keys.length) { '}' }.join}\n" end end.join color_diff "\n#{diff_str}" else actual_as_string = object_to_string(actual) expected_as_string = object_to_string(expected) diff_as_string(actual_as_string, expected_as_string) end end |
#find_contain_nodes(object, current_path = []) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rspec_hashdiff_helper.rb', line 20 def find_contain_nodes(object, current_path = []) if object.instance_of?(RSpec::Matchers::BuiltIn::Match) find_contain_nodes(object.expected, current_path) elsif object.instance_of?(RSpec::Matchers::BuiltIn::ContainExactly) paths = [current_path.dup] find_contain_nodes(object.expected, current_path).each { |p| paths << p } paths elsif object.instance_of?(Hash) paths = [] object.each { |key, value| find_contain_nodes(value, current_path.dup << key).each { |p| paths << p } } paths elsif object.instance_of?(Array) paths = [] object.each_with_index { |value, index| find_contain_nodes(value, current_path.dup << index).each { |p| paths << p } } paths else [] end end |
#format_hashes_for_diff(actual, expected) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rspec_hashdiff_helper.rb', line 40 def format_hashes_for_diff(actual, expected) paths_to_contain_nodes = find_contain_nodes(expected) expected = strip_matchers(expected) paths_to_contain_nodes.each do |full_path| actual_target = actual expected_target = expected full_path.each do |step| actual_target = actual_target.respond_to?(:[]) ? actual_target[step] : nil expected_target = expected_target[step] end actual_target&.sort_by! { |v| v.instance_of?(Hash) ? v.to_s : v } expected_target.sort_by! { |v| v.instance_of?(Hash) ? v.to_s : v } end [actual, expected] end |
#strip_matchers(expected) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rspec_hashdiff_helper.rb', line 8 def strip_matchers(expected) if expected.respond_to?(:expected) strip_matchers(expected.expected) elsif expected.instance_of?(Hash) expected.transform_values { |v| strip_matchers(v) } elsif expected.instance_of?(Array) expected.map { |v| strip_matchers(v) } else expected end end |