rspec_hashdiff_helper 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rspec_hashdiff_helper.rb +91 -0
  3. metadata +72 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6691eb2a2f62e9ded52f11fa362814abbd0f40d9de115204deaa386dbb17d81a
4
+ data.tar.gz: c2391bf2b180f2619d58044ecea1dc9410123ebe0c1d821a4891fc33b315bdfc
5
+ SHA512:
6
+ metadata.gz: db77db2a44b9c34a97cf4af1eb549c9c0d73a55eb0f5d578132d9e5bc03625f2241a1d4a7c3d65203ce3ea0ee531210cad944b302b8f106a0e58e9b43e81720a
7
+ data.tar.gz: b3bb274130e403f4a0560e2b7d2ca55f2a844be7284cd8e79024d1c238d83b13764cd3a4887af4d8081047ef37cab2b2d3363f269c5d0c11716472537e04a582
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hashdiff'
4
+
5
+ module RSpec
6
+ module Support
7
+ class Differ # rubocop:disable Style/Documentation
8
+ def strip_matchers(expected)
9
+ if expected.respond_to?(:expected)
10
+ strip_matchers(expected.expected)
11
+ elsif expected.instance_of?(Hash)
12
+ expected.transform_values { |v| strip_matchers(v) }
13
+ elsif expected.instance_of?(Array)
14
+ expected.map { |v| strip_matchers(v) }
15
+ else
16
+ expected
17
+ end
18
+ end
19
+
20
+ def find_contain_nodes(object, current_path = [])
21
+ if object.instance_of?(RSpec::Matchers::BuiltIn::Match)
22
+ find_contain_nodes(object.expected, current_path)
23
+ elsif object.instance_of?(RSpec::Matchers::BuiltIn::ContainExactly)
24
+ paths = [current_path.dup]
25
+ find_contain_nodes(object.expected, current_path).each { |p| paths << p }
26
+ paths
27
+ elsif object.instance_of?(Hash)
28
+ paths = []
29
+ object.each { |key, value| find_contain_nodes(value, current_path.dup << key).each { |p| paths << p } }
30
+ paths
31
+ elsif object.instance_of?(Array)
32
+ paths = []
33
+ object.each_with_index { |value, index| find_contain_nodes(value, current_path.dup << index).each { |p| paths << p } }
34
+ paths
35
+ else
36
+ []
37
+ end
38
+ end
39
+
40
+ def format_hashes_for_diff(actual, expected)
41
+ paths_to_contain_nodes = find_contain_nodes(expected)
42
+ expected = strip_matchers(expected)
43
+
44
+ paths_to_contain_nodes.each do |full_path|
45
+ actual_target = actual
46
+ expected_target = expected
47
+ full_path.each do |step|
48
+ actual_target = actual_target.respond_to?(:[]) ? actual_target[step] : nil
49
+ expected_target = expected_target[step]
50
+ end
51
+
52
+ actual_target&.sort_by! { |v| v.instance_of?(Hash) ? v.to_s : v }
53
+ expected_target.sort_by! { |v| v.instance_of?(Hash) ? v.to_s : v }
54
+ end
55
+
56
+ [actual, expected]
57
+ end
58
+
59
+ def diff_as_object(actual, expected)
60
+ if actual.instance_of?(Hash) && expected.instance_of?(Hash)
61
+ mod_actual, mod_expected = format_hashes_for_diff(actual, expected)
62
+ diff = Hashdiff.diff(mod_actual,
63
+ mod_expected,
64
+ delimiter: ':',
65
+ array_path: true,
66
+ similarity: 1,
67
+ strict: false,
68
+ use_lcs: false,
69
+ indifferent: true)
70
+
71
+ diff_str = diff.sort_by { |x| x[1].map(&:to_s) }.map do |diff_line|
72
+ keys = diff_line[1]
73
+ case diff_line[0]
74
+ when '~'
75
+ "+ #{keys.map { |key| "{#{key}:" }.join} #{diff_line[2]} #{Array.new(keys.length) { '}' }.join}\n" \
76
+ "- #{keys.map { |key| "{#{key}:" }.join} #{diff_line[3]} #{Array.new(keys.length) { '}' }.join}\n"
77
+ when '+', '-'
78
+ "#{diff_line[0]} #{keys.map { |key| "{#{key}:" }.join}"\
79
+ " #{diff_line[2]} #{Array.new(keys.length) { '}' }.join}\n"
80
+ end
81
+ end.join
82
+ color_diff "\n#{diff_str}"
83
+ else
84
+ actual_as_string = object_to_string(actual)
85
+ expected_as_string = object_to_string(expected)
86
+ diff_as_string(actual_as_string, expected_as_string)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_hashdiff_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sid Shanker
8
+ - Robert Uhl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2024-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashdiff
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description:
43
+ email:
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/rspec_hashdiff_helper.rb
49
+ homepage:
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.5.3
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A library for rspec that displays better hash diffs
72
+ test_files: []