diff_matcher 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -3
- data/README.md +16 -1
- data/lib/diff_matcher/difference.rb +25 -14
- data/lib/diff_matcher/version.rb +1 -1
- data/spec/diff_matcher/difference_spec.rb +66 -0
- metadata +4 -14
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -147,7 +147,7 @@ Where, - 1 missing, + 1 additional, : 2 match_class
|
|
147
147
|
|
148
148
|
When `actual` is an array with a *limited* size use an `AllMatcher` to match
|
149
149
|
against *all* the elements in the array adhering to the limits of `:min`
|
150
|
-
and or `:max
|
150
|
+
and or `:max` or `:size` (where `:size` is a Fixnum or range of Fixnum).
|
151
151
|
|
152
152
|
``` ruby
|
153
153
|
puts DiffMatcher::difference(DiffMatcher::AllMatcher.new(Fixnum, :min=>3), [1, 2])
|
@@ -159,6 +159,15 @@ puts DiffMatcher::difference(DiffMatcher::AllMatcher.new(Fixnum, :min=>3), [1, 2
|
|
159
159
|
Where, - 1 missing, : 2 match_class
|
160
160
|
```
|
161
161
|
|
162
|
+
``` ruby
|
163
|
+
puts DiffMatcher::difference(DiffMatcher::AllMatcher.new(Fixnum, :size=>3..5), [1, 2])
|
164
|
+
[
|
165
|
+
: 1,
|
166
|
+
: 2,
|
167
|
+
- Fixnum
|
168
|
+
]
|
169
|
+
Where, - 1 missing, : 2 match_class
|
170
|
+
```
|
162
171
|
|
163
172
|
When `actual` is an array of unknown size *and* `expected` can take
|
164
173
|
multiple forms use a `Matcher` inside of an `AllMatcher` to match
|
@@ -246,6 +255,10 @@ Similar gems
|
|
246
255
|
* <http://github.com/postmodern/tdiff> (Calculates the differences between two tree-like structures)
|
247
256
|
* <http://github.com/Blargel/easy_diff> (Recursive diff, merge, and unmerge for hashes and arrays)
|
248
257
|
|
258
|
+
### JSON matchers
|
259
|
+
* <http://github.com/collectiveidea/json_spec> (Easily handle JSON in RSpec and Cucumber)
|
260
|
+
* <http://github.com/lloyd/JSONSelect> (CSS-like selectors for JSON)
|
261
|
+
|
249
262
|
|
250
263
|
Why another differ?
|
251
264
|
---
|
@@ -262,6 +275,8 @@ It has extra functionality in also being able to recursively merge hashes and ar
|
|
262
275
|
DiffMatcher can match using not only regexes but classes and procs.
|
263
276
|
And the difference string that it outputs can be formatted in several ways as needed.
|
264
277
|
|
278
|
+
As for matching JSON, the matchers above work well, but don't allow for matching patterns.
|
279
|
+
|
265
280
|
|
266
281
|
Use with rspec
|
267
282
|
---
|
@@ -50,9 +50,19 @@ module DiffMatcher
|
|
50
50
|
class AllMatcher < Matcher
|
51
51
|
def expected(e, actual)
|
52
52
|
opts = expected_opts(e)
|
53
|
+
size = opts[:size]
|
54
|
+
case size
|
55
|
+
when Fixnum
|
56
|
+
min = size
|
57
|
+
max = size
|
58
|
+
when Range
|
59
|
+
min = size.first
|
60
|
+
max = size.last
|
61
|
+
else
|
62
|
+
min = opts[:min] || 0
|
63
|
+
max = opts[:max] || 1_000_000 # MAXINT?
|
64
|
+
end
|
53
65
|
size = actual.size
|
54
|
-
min = opts[:min] || 0
|
55
|
-
max = opts[:max] || 1_000_000 # MAXINT?
|
56
66
|
size = size > min ? (size < max ? size : max) : min
|
57
67
|
[e]*size
|
58
68
|
end
|
@@ -168,7 +178,7 @@ module DiffMatcher
|
|
168
178
|
items_to_s(
|
169
179
|
expected,
|
170
180
|
(item_types_shown).inject([]) { |a, method|
|
171
|
-
a + send(method, left, right, expected
|
181
|
+
a + send(method, left, right, expected).compact.map { |item| markup(method, item) }
|
172
182
|
}
|
173
183
|
)
|
174
184
|
else
|
@@ -189,9 +199,10 @@ module DiffMatcher
|
|
189
199
|
end if expected.is_a? actual.class
|
190
200
|
end
|
191
201
|
|
192
|
-
def compare(right,
|
193
|
-
|
194
|
-
|
202
|
+
def compare(right, expected, default=nil)
|
203
|
+
case expected
|
204
|
+
when Hash, Array
|
205
|
+
right && right.keys.tap { |keys| keys.sort if expected.is_a? Array }.map { |k|
|
195
206
|
yield k
|
196
207
|
}
|
197
208
|
else
|
@@ -199,20 +210,20 @@ module DiffMatcher
|
|
199
210
|
end
|
200
211
|
end
|
201
212
|
|
202
|
-
def different(left, right,
|
203
|
-
compare(right,
|
204
|
-
"#{"#{k.inspect}=>" if
|
213
|
+
def different(left, right, expected)
|
214
|
+
compare(right, expected, difference_to_s(right, left)) { |k|
|
215
|
+
"#{"#{k.inspect}=>" if expected.is_a? Hash}#{right[k]}" if right[k] and left.has_key?(k)
|
205
216
|
}
|
206
217
|
end
|
207
218
|
|
208
|
-
def missing(left, right,
|
209
|
-
compare(left,
|
210
|
-
"#{"#{k.inspect}=>" if
|
219
|
+
def missing(left, right, expected)
|
220
|
+
compare(left, expected) { |k|
|
221
|
+
"#{"#{k.inspect}=>" if expected.is_a? Hash}#{left[k].inspect}" unless right.has_key?(k) || @optional_keys.include?(k)
|
211
222
|
}
|
212
223
|
end
|
213
224
|
|
214
|
-
def additional(left, right,
|
215
|
-
missing(right, left,
|
225
|
+
def additional(left, right, expected)
|
226
|
+
missing(right, left, expected)
|
216
227
|
end
|
217
228
|
|
218
229
|
def match?(expected, actual)
|
data/lib/diff_matcher/version.rb
CHANGED
@@ -280,6 +280,22 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
|
|
280
280
|
end
|
281
281
|
end
|
282
282
|
|
283
|
+
context "of an Array derived class," do
|
284
|
+
class ArrayChild < Array; end
|
285
|
+
expected, same, different =
|
286
|
+
ArrayChild[ 1 ],
|
287
|
+
ArrayChild[ 1 ],
|
288
|
+
ArrayChild[ 2 ]
|
289
|
+
|
290
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
291
|
+
<<-EOF, {}
|
292
|
+
[
|
293
|
+
- 1+ 2
|
294
|
+
]
|
295
|
+
Where, - 1 missing, + 1 additional
|
296
|
+
EOF
|
297
|
+
end
|
298
|
+
|
283
299
|
context "of Range," do
|
284
300
|
expected, same, different =
|
285
301
|
(1..3),
|
@@ -340,6 +356,22 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
|
|
340
356
|
end
|
341
357
|
end
|
342
358
|
end
|
359
|
+
|
360
|
+
context "of a Hash derived class," do
|
361
|
+
class HashChild < Hash; end
|
362
|
+
expected, same, different =
|
363
|
+
HashChild["a",1],
|
364
|
+
HashChild["a",1],
|
365
|
+
HashChild["a",2]
|
366
|
+
|
367
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
368
|
+
<<-EOF
|
369
|
+
{
|
370
|
+
"a"=>- 1+ 2
|
371
|
+
}
|
372
|
+
Where, - 1 missing, + 1 additional
|
373
|
+
EOF
|
374
|
+
end
|
343
375
|
end
|
344
376
|
|
345
377
|
describe "when expected is," do
|
@@ -476,6 +508,40 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
|
|
476
508
|
Where, + 1 additional, : 2 match_class
|
477
509
|
EOF
|
478
510
|
end
|
511
|
+
|
512
|
+
context "with a size restriction" do
|
513
|
+
expected, same, different =
|
514
|
+
DiffMatcher::AllMatcher.new(String, :size=>2),
|
515
|
+
%w(ay be),
|
516
|
+
%w(ay be ci)
|
517
|
+
|
518
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
519
|
+
<<-EOF
|
520
|
+
[
|
521
|
+
: "ay",
|
522
|
+
: "be",
|
523
|
+
+ "ci"
|
524
|
+
]
|
525
|
+
Where, + 1 additional, : 2 match_class
|
526
|
+
EOF
|
527
|
+
end
|
528
|
+
|
529
|
+
context "with a size restriction range" do
|
530
|
+
expected, same, different =
|
531
|
+
DiffMatcher::AllMatcher.new(String, :size=>0..2),
|
532
|
+
%w(ay be),
|
533
|
+
%w(ay be ci)
|
534
|
+
|
535
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
536
|
+
<<-EOF
|
537
|
+
[
|
538
|
+
: "ay",
|
539
|
+
: "be",
|
540
|
+
+ "ci"
|
541
|
+
]
|
542
|
+
Where, + 1 additional, : 2 match_class
|
543
|
+
EOF
|
544
|
+
end
|
479
545
|
end
|
480
546
|
|
481
547
|
context "a DiffMatcher::AllMatcher using an or-ed DiffMatcher::Matcher," do
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diff_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 7
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 2
|
8
|
-
- 2
|
9
|
-
- 0
|
10
|
-
version: 2.2.0
|
5
|
+
version: 2.2.1
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Playup
|
@@ -15,8 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2012-
|
19
|
-
default_executable:
|
13
|
+
date: 2012-05-21 00:00:00 Z
|
20
14
|
dependencies: []
|
21
15
|
|
22
16
|
description: |
|
@@ -53,7 +47,6 @@ files:
|
|
53
47
|
- lib/diff_matcher/version.rb
|
54
48
|
- spec/diff_matcher/difference_spec.rb
|
55
49
|
- spec/spec_helper.rb
|
56
|
-
has_rdoc: true
|
57
50
|
homepage: http://github.com/playup/diff_matcher
|
58
51
|
licenses: []
|
59
52
|
|
@@ -67,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
60
|
requirements:
|
68
61
|
- - ">="
|
69
62
|
- !ruby/object:Gem::Version
|
70
|
-
hash:
|
63
|
+
hash: 2195120137284672671
|
71
64
|
segments:
|
72
65
|
- 0
|
73
66
|
version: "0"
|
@@ -76,14 +69,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
69
|
requirements:
|
77
70
|
- - ">="
|
78
71
|
- !ruby/object:Gem::Version
|
79
|
-
hash: 3
|
80
|
-
segments:
|
81
|
-
- 0
|
82
72
|
version: "0"
|
83
73
|
requirements: []
|
84
74
|
|
85
75
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.
|
76
|
+
rubygems_version: 1.8.21
|
87
77
|
signing_key:
|
88
78
|
specification_version: 3
|
89
79
|
summary: Generates a diff by matching against expected values, classes, regexes and/or procs.
|