json-deep-compare 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,10 @@ module JsonDeepCompare
|
|
6
6
|
else
|
7
7
|
options[:exclusions] = []
|
8
8
|
end
|
9
|
+
if substitute_with = options[:substitute_with]
|
10
|
+
options[:substitutions] =
|
11
|
+
SubstitutionsBuilder.new(substitute_with).result
|
12
|
+
end
|
9
13
|
@root_comparisons = []
|
10
14
|
@root_comparisons << NodeComparison.new(
|
11
15
|
lval, rval, ":root", options.merge(direction: :left)
|
@@ -32,5 +36,58 @@ module JsonDeepCompare
|
|
32
36
|
def equal?
|
33
37
|
@root_comparisons.all?(&:equal?)
|
34
38
|
end
|
39
|
+
|
40
|
+
class SubstitutionsBuilder
|
41
|
+
attr_reader :result
|
42
|
+
|
43
|
+
def initialize(proc)
|
44
|
+
@proc = proc
|
45
|
+
@result = {}
|
46
|
+
root = Node.new(":root")
|
47
|
+
proc.call root
|
48
|
+
root.rules.each do |rule|
|
49
|
+
@result[rule.selector] = rule.value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Node
|
54
|
+
def initialize(selector)
|
55
|
+
@selector = selector
|
56
|
+
@sub_nodes = []
|
57
|
+
end
|
58
|
+
|
59
|
+
def method_missing(meth, *args, &block)
|
60
|
+
if block_given?
|
61
|
+
if args.size == 1
|
62
|
+
sub_node = Node.new(
|
63
|
+
@selector + " > .#{meth} :nth-child(#{args.first})"
|
64
|
+
)
|
65
|
+
else
|
66
|
+
sub_node = Node.new(@selector + " > .#{meth}")
|
67
|
+
end
|
68
|
+
yield sub_node
|
69
|
+
elsif args.size == 2
|
70
|
+
sub_node = Node.new(
|
71
|
+
@selector + " > .#{meth} :nth-child(#{args.first})"
|
72
|
+
)
|
73
|
+
sub_node.value = args.last
|
74
|
+
else
|
75
|
+
sub_node = Node.new(@selector + " > .#{meth}")
|
76
|
+
sub_node.value = args.first
|
77
|
+
end
|
78
|
+
@sub_nodes << sub_node
|
79
|
+
end
|
80
|
+
|
81
|
+
def rules
|
82
|
+
@sub_nodes.map { |sn| sn.rules }.flatten.concat([@rule]).compact
|
83
|
+
end
|
84
|
+
|
85
|
+
def value=(v)
|
86
|
+
@rule = Rule.new(@selector, v)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
Rule = Struct.new(:selector, :value)
|
91
|
+
end
|
35
92
|
end
|
36
93
|
end
|
@@ -49,7 +49,8 @@ module JsonDeepCompare
|
|
49
49
|
else
|
50
50
|
[Difference.new(
|
51
51
|
@selector, "expected to be :lval but was :rval",
|
52
|
-
lval: value_inspect(
|
52
|
+
lval: value_inspect(lval_for_equality),
|
53
|
+
rval: value_inspect(rval_for_equality)
|
53
54
|
)]
|
54
55
|
end
|
55
56
|
else
|
@@ -222,4 +222,64 @@ class DocumentComparisonTestCase < Test::Unit::TestCase
|
|
222
222
|
)
|
223
223
|
assert comparison.equal?
|
224
224
|
end
|
225
|
+
|
226
|
+
def test_failure_while_substituting
|
227
|
+
lval = {'one' => 'raw_value'}
|
228
|
+
rval = {'one' => 'actual_value'}
|
229
|
+
comparison = JsonDeepCompare::DocumentComparison.new(
|
230
|
+
lval, rval, substitutions: {":root > .one" => 'expected_value'}
|
231
|
+
)
|
232
|
+
assert !comparison.equal?
|
233
|
+
assert_match(
|
234
|
+
/":root > .one" expected to be "expected_value" but was "actual_value"/,
|
235
|
+
comparison.difference_messages
|
236
|
+
)
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_substitute_with_block
|
240
|
+
lval = {
|
241
|
+
'one' => 1,
|
242
|
+
'two' => {
|
243
|
+
'three' => 3,
|
244
|
+
'four' => 'four',
|
245
|
+
'five' => [
|
246
|
+
{'six' => 'six'}, {'seven' => 7}
|
247
|
+
],
|
248
|
+
'eight' => [
|
249
|
+
'nine',
|
250
|
+
10,
|
251
|
+
'eleven'
|
252
|
+
]
|
253
|
+
}
|
254
|
+
}
|
255
|
+
rval = {
|
256
|
+
'one' => 'one',
|
257
|
+
'two' => {
|
258
|
+
'three' => 'three',
|
259
|
+
'four' => 'four',
|
260
|
+
'five' => [
|
261
|
+
{'six' => 'six'}, {'seven' => 'seven'}
|
262
|
+
],
|
263
|
+
'eight' => [
|
264
|
+
'nine',
|
265
|
+
'ten',
|
266
|
+
'eleven'
|
267
|
+
]
|
268
|
+
}
|
269
|
+
}
|
270
|
+
comparison = JsonDeepCompare::DocumentComparison.new(
|
271
|
+
lval, rval,
|
272
|
+
substitute_with: Proc.new { |root|
|
273
|
+
root.one 'one'
|
274
|
+
root.two do |two|
|
275
|
+
two.three 'three'
|
276
|
+
two.five(2) do |five|
|
277
|
+
five.seven 'seven'
|
278
|
+
end
|
279
|
+
two.eight(2, 'ten')
|
280
|
+
end
|
281
|
+
}
|
282
|
+
)
|
283
|
+
assert comparison.equal?
|
284
|
+
end
|
225
285
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-deep-compare
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -77,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
77
|
version: '0'
|
78
78
|
segments:
|
79
79
|
- 0
|
80
|
-
hash: -
|
80
|
+
hash: -2413287472773223774
|
81
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
82
|
none: false
|
83
83
|
requirements:
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
segments:
|
88
88
|
- 0
|
89
|
-
hash: -
|
89
|
+
hash: -2413287472773223774
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
92
|
rubygems_version: 1.8.23
|