-
Notifications
You must be signed in to change notification settings - Fork 3
/
differ.rb
157 lines (135 loc) · 3.99 KB
/
differ.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
50
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
79
80
81
82
83
84
85
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
# frozen_string_literal: true
require 'pact/xml/errors'
require 'pact/matchers'
require 'rexml/document'
module Pact
module XML
# Diffs two XML strings
class Differ
include REXML
include Pact::Matchers
DEFAULT_OPTIONS = { allow_unexpected_keys: true, type: false }.freeze
def self.validate_xml(doc, type)
raise InvalidXmlError, "#{type} is not a valid XML" if doc.root.nil?
end
def self.validate_xmls(expected_doc, actual_doc)
validate_xml expected_doc, 'Expected'
validate_xml actual_doc, 'Actual'
end
def self.parse(xml, type)
Document.new(xml, ignore_whitespace_nodes: :all)
rescue REXML::ParseException
raise InvalidXmlError, "#{type} is not a valid XML"
end
def self.call(expected, actual, options = {})
return [] if expected.to_s.empty?
expected_doc = parse expected, 'Expected'
actual_doc = parse actual, 'Actual'
validate_xmls expected_doc, actual_doc
node_diff(
expected_doc,
actual_doc,
['$'],
DEFAULT_OPTIONS.merge(options)
)
end
def self.path_to_s(path)
path.join '.'
end
def self.get_attr_by_name(name, element)
element.attributes.get_attribute(name)&.value
end
def self.attr_diff(expected, actual, path, options)
diff = []
expected.attributes.each_attribute do |a|
diff.concat(
difference(
get_attr_by_name(a.name, expected),
get_attr_by_name(a.name, actual),
'attribute',
path,
".@#{a.name}"
)
)
end
unless options[:allow_unexpected_keys]
diff.concat(
extra_attrs(expected, actual, path)
)
end
diff
end
def self.extra_attrs(expected, actual, path)
diff = []
actual.attributes.each_attribute do |a|
diff.push a.name if (get_attr_by_name a.name, expected).nil?
end
diff.map do |x|
Difference.new(
nil,
x,
"Did not expect attribute #{x} to exist at #{path_to_s path}"
)
end
end
def self.elem_diff(expected, actual, path, options)
diff = []
expected.each_index do |i|
i_next = i + 1
expected_next = expected.elements[i_next]
actual_next = actual.elements[i_next]
diff.concat(
node_diff(
expected_next,
actual_next,
[*path, expected_next&.name],
options
)
)
end
unless options[:allow_unexpected_keys]
diff.concat(
extra_elems(expected, actual, path)
)
end
diff
end
def self.extra_elems(expected, actual, path)
actual.elements.drop(expected.elements.size).map do |x|
Difference.new(
nil,
x.name,
"Did not expect element #{x.name} to exist at #{path_to_s path}"
)
end
end
def self.nil_if_nil(obj)
obj.nil? ? 'nil' : obj
end
def self.difference(expected, actual, type, path, suffix)
return [] if expected == actual
[Difference.new(
expected,
actual,
"Expected #{type} #{nil_if_nil expected} " \
"but got #{nil_if_nil actual} " \
"at #{path_to_s path}#{suffix}"
)]
end
def self.node_diff(expected, actual, path, options)
return [] if expected.nil?
diff = difference expected.name, actual.name, 'element', path, ''
return diff if diff.any?
diff.concat(
attr_diff(expected, actual, path, options)
)
diff.concat(
elem_diff(expected, actual, path, options)
)
diff.concat(
difference(expected.text, actual.text, 'text', path, '.#text')
)
end
end
end
end