Class: Specdiff::Differ::Hash
- Inherits:
-
Object
- Object
- Specdiff::Differ::Hash
show all
- Extended by:
- Colorize
- Defined in:
- lib/specdiff/differ/hash.rb
Constant Summary
collapse
- KEY_CHANGE_PERCENTAGE_THRESHOLD =
The percentage of changes that must (potentially) be a key rename in a hash for text diffing to kick in. Expressed as a fraction of 1.
0.8
- TOTAL_CHANGES_FOR_GROUPING_THRESHOLD =
The number of changes that must be detected by hashdiff before we print some extra newlines to better group extra/missing/new_values visually.
9
- NEWLINE =
"\n"
- PATH_SEPARATOR =
".".freeze
Class Method Summary
collapse
Methods included from Colorize
blue, colorize_by_line, cyan, green, red, reset_color, yellow
Class Method Details
._calculate_change_percentage(hashdiff_diff) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/specdiff/differ/hash.rb', line 51
def self._calculate_change_percentage(hashdiff_diff)
= hashdiff_diff.count { |element| element[0] == "+" }
missing_keys = hashdiff_diff.count { |element| element[0] == "-" }
new_values = hashdiff_diff.count { |element| element[0] == "~" }
potential_changed_keys = [, missing_keys].min
= - potential_changed_keys
adjusted_missing_keys = missing_keys - potential_changed_keys
non_changed_keys = + adjusted_missing_keys + new_values
total_changes = non_changed_keys + potential_changed_keys
key_change_fraction = Rational(potential_changed_keys, total_changes)
key_change_percentage = key_change_fraction.to_f
key_change_percentage
end
|
._stringify_path(path) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/specdiff/differ/hash.rb', line 164
def self._stringify_path(path)
result = +""
path.each do |component|
if component.is_a?(Numeric)
result.chomp!(PATH_SEPARATOR)
result << "[#{component}]"
elsif component.is_a?(Symbol)
result << component.to_s
else
result << component.inspect
end
result << PATH_SEPARATOR
end
result.chomp(PATH_SEPARATOR)
end
|
.diff(a, b) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/specdiff/differ/hash.rb', line 14
def self.diff(a, b)
hashdiff_diff = ::Hashdiff.diff(
a.value, b.value,
array_path: true,
use_lcs: false,
)
return hashdiff_diff if hashdiff_diff.empty?
change_percentage = _calculate_change_percentage(hashdiff_diff)
if change_percentage <= KEY_CHANGE_PERCENTAGE_THRESHOLD
hashdiff_diff
else
a_text = ::Specdiff.hashprint(a.value)
b_text = ::Specdiff.hashprint(b.value)
text_diff = ::Specdiff.diff(a_text, b_text)
if text_diff.empty?
[]
else
text_diff
end
end
end
|
.empty?(diff) ⇒ Boolean
80
81
82
|
# File 'lib/specdiff/differ/hash.rb', line 80
def self.empty?(diff)
diff.raw.empty?
end
|
.stringify(diff) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/specdiff/differ/hash.rb', line 86
def self.stringify(diff)
result = +""
return result if diff.empty?
total_changes = diff.raw.size
group_with_newlines = total_changes >= TOTAL_CHANGES_FOR_GROUPING_THRESHOLD
changes_grouped_by_type = diff.raw.group_by { |change| change[0] }
if (changes_grouped_by_type.keys - ["+", "-", "~"]).size > 0
$stderr.puts(
"Specdiff: hashdiff returned unexpected types: #{diff.raw.inspect}"
)
end
deletions = changes_grouped_by_type["-"] || []
additions = changes_grouped_by_type["+"] || []
value_changes = changes_grouped_by_type["~"] || []
result << "@@ +#{additions.size}/-#{deletions.size}/~#{value_changes.size} @@"
result << NEWLINE
deletions.each do |change|
value = change[2]
path = _stringify_path(change[1])
result << "missing key: #{path} (#{::Specdiff.diff_inspect(value)})"
result << NEWLINE
end
if deletions.any? && additions.any? && group_with_newlines
result << NEWLINE
end
additions.each do |change|
value = change[2]
path = _stringify_path(change[1])
result << " extra key: #{path} (#{::Specdiff.diff_inspect(value)})"
result << NEWLINE
end
if additions.any? && value_changes.any? && group_with_newlines
result << NEWLINE
end
value_changes.each do |change|
from = change[2]
to = change[3]
path = _stringify_path(change[1])
from_inspected = ::Specdiff.diff_inspect(from)
to_inspected = ::Specdiff.diff_inspect(to)
result << " new value: #{path} (#{from_inspected} -> #{to_inspected})"
result << NEWLINE
end
colorize_by_line(result) do |line|
if line.start_with?("missing key:")
red(line)
elsif line.start_with?(" extra key:")
green(line)
elsif line.start_with?(" new value:")
yellow(line)
elsif line.start_with?("@@")
cyan(line)
else
reset_color(line)
end
end
end
|