diff_matcher 2.2.3 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +26 -0
- data/README.md +18 -1
- data/examples/tennis_score.rb +15 -0
- data/lib/diff_matcher/difference.rb +44 -7
- data/lib/diff_matcher/version.rb +1 -1
- data/spec/diff_matcher/difference_spec.rb +50 -3
- metadata +31 -40
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
multi_json (1.0.4)
|
6
|
+
rake (0.9.2.2)
|
7
|
+
rspec (2.6.0)
|
8
|
+
rspec-core (~> 2.6.0)
|
9
|
+
rspec-expectations (~> 2.6.0)
|
10
|
+
rspec-mocks (~> 2.6.0)
|
11
|
+
rspec-core (2.6.4)
|
12
|
+
rspec-expectations (2.6.0)
|
13
|
+
diff-lcs (~> 1.1.2)
|
14
|
+
rspec-mocks (2.6.0)
|
15
|
+
simplecov (0.5.4)
|
16
|
+
multi_json (~> 1.0.3)
|
17
|
+
simplecov-html (~> 0.5.3)
|
18
|
+
simplecov-html (0.5.3)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
rake (~> 0.9)
|
25
|
+
rspec (~> 2.6.0)
|
26
|
+
simplecov
|
data/README.md
CHANGED
@@ -74,6 +74,13 @@ p DiffMatcher::difference(/[a-z]/, "a")
|
|
74
74
|
# => nil
|
75
75
|
```
|
76
76
|
|
77
|
+
When `actual` is a fixnum that matches the `expected` range
|
78
|
+
|
79
|
+
``` ruby
|
80
|
+
p DiffMatcher::difference(1..3, 3)
|
81
|
+
# => nil
|
82
|
+
```
|
83
|
+
|
77
84
|
When `actual` is passed to an `expected` proc and it returns true
|
78
85
|
|
79
86
|
``` ruby
|
@@ -217,7 +224,7 @@ The items shown in a difference are prefixed as follows:
|
|
217
224
|
match regexp => "~ "
|
218
225
|
match class => ": "
|
219
226
|
match matcher => "| "
|
220
|
-
match
|
227
|
+
match range => ". "
|
221
228
|
match proc => "{ "
|
222
229
|
|
223
230
|
|
@@ -258,6 +265,7 @@ Similar gems
|
|
258
265
|
### JSON matchers
|
259
266
|
* <http://github.com/collectiveidea/json_spec> (Easily handle JSON in RSpec and Cucumber)
|
260
267
|
* <http://github.com/lloyd/JSONSelect> (CSS-like selectors for JSON)
|
268
|
+
* <http://github.com/chancancode/json_expressions> (JSON matchmaking for all your API testing needs)
|
261
269
|
|
262
270
|
|
263
271
|
Why another differ?
|
@@ -277,6 +285,15 @@ And the difference string that it outputs can be formatted in several ways as ne
|
|
277
285
|
|
278
286
|
As for matching JSON, the matchers above work well, but don't allow for matching patterns.
|
279
287
|
|
288
|
+
#### Update 2012/07/14:
|
289
|
+
|
290
|
+
[json_expressions](http://github.com/chancancode/json_expressions) (as mentioned in [Ruby5 - Episode #288](http://ruby5.envylabs.com/episodes/292-episode-288-july-13th-2012)) *does*
|
291
|
+
do pattern matching and also looks like a good alternative to diff_matcher, it has the following advantages:
|
292
|
+
* define capture symbols that can be used to extract values from the matched object
|
293
|
+
* (if a symbol is used multiple times, it will make sure all the extracted values match)
|
294
|
+
* can optionally match unordered arrays (diff_matcher only matches ordered arrays)
|
295
|
+
* because it doesn't bother generating a pretty difference string it might be faster
|
296
|
+
|
280
297
|
|
281
298
|
Use with rspec
|
282
299
|
---
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'diff_matcher'
|
2
|
+
|
3
|
+
set_scores = [
|
4
|
+
[[6,4], [6,4], [6,4]],
|
5
|
+
[[8,4], [6,9], [6,4]],
|
6
|
+
]
|
7
|
+
|
8
|
+
pat_set_scores = DiffMatcher::AllMatcher.new(
|
9
|
+
[0..6, 0..6],
|
10
|
+
:size => 2..3
|
11
|
+
)
|
12
|
+
|
13
|
+
invalid_scores = set_scores.map { |score| pat_set_scores.diff(score) }.compact
|
14
|
+
|
15
|
+
puts invalid_scores
|
@@ -181,6 +181,20 @@ module DiffMatcher
|
|
181
181
|
}.call
|
182
182
|
end
|
183
183
|
|
184
|
+
def summarize_item_str(item, method)
|
185
|
+
if [:missing, :additional].include?(method)
|
186
|
+
if item.match(/\A\[.*\]\Z/) and [:missing, :additional].include?(method)
|
187
|
+
'[...]'
|
188
|
+
elsif item.match(/\A\{.*\}\Z/)
|
189
|
+
'{...}'
|
190
|
+
else
|
191
|
+
item
|
192
|
+
end
|
193
|
+
else
|
194
|
+
item
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
184
198
|
def difference(expected, actual)
|
185
199
|
if actual.is_a? expected.class
|
186
200
|
left = diff(expected, actual)
|
@@ -188,7 +202,9 @@ module DiffMatcher
|
|
188
202
|
items_to_s(
|
189
203
|
expected,
|
190
204
|
(item_types_shown).inject([]) { |a, method|
|
191
|
-
a + send(method, left, right, expected).compact.map { |item|
|
205
|
+
a + send(method, left, right, expected).compact.map { |item|
|
206
|
+
markup(method, summarize_item_str(item, method))
|
207
|
+
}
|
192
208
|
}
|
193
209
|
)
|
194
210
|
else
|
@@ -269,22 +285,43 @@ module DiffMatcher
|
|
269
285
|
markup(match_type, actual) if matches_shown.include?(match_type)
|
270
286
|
end
|
271
287
|
|
288
|
+
def summarize_item(item)
|
289
|
+
case item
|
290
|
+
when Array
|
291
|
+
'[...]'
|
292
|
+
when Hash
|
293
|
+
'{...}'
|
294
|
+
else
|
295
|
+
item.inspect
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
272
299
|
def difference_to_s(expected, actual)
|
273
300
|
match, match_type, d = match?(expected, actual)
|
274
301
|
if match
|
275
302
|
match_to_s(expected, actual.inspect, match_type)
|
276
303
|
else
|
277
|
-
match_type
|
278
|
-
|
304
|
+
case match_type
|
305
|
+
when :match_matcher
|
306
|
+
d
|
307
|
+
else
|
308
|
+
exp, act = if [expected, actual].any? { |item| item.is_a?(Proc) }
|
309
|
+
[expected, actual].map { |item| item.inspect }
|
310
|
+
else
|
311
|
+
[expected, actual].map { |item| summarize_item(item) }
|
312
|
+
end
|
313
|
+
|
314
|
+
"#{markup(:missing, exp)}#{markup(:additional, act)}"
|
315
|
+
end
|
279
316
|
end
|
280
317
|
end
|
281
318
|
|
282
|
-
def markup(
|
283
|
-
if
|
319
|
+
def markup(match_type, item)
|
320
|
+
if match_type == :different
|
284
321
|
item.split("\n").map {|line| " #{line}"}.join("\n") if item
|
285
322
|
else
|
286
|
-
color, prefix = @color_scheme[
|
287
|
-
"#{color}#{prefix+' ' if prefix}#{BOLD if color and
|
323
|
+
color, prefix = @color_scheme[match_type]
|
324
|
+
"#{color}#{prefix+' ' if prefix}#{BOLD if color and match_type != :match_regexp}#{RESET if match_type == :match_regexp}#{item}#{RESET if color}" if item
|
288
325
|
end if item
|
289
326
|
end
|
290
327
|
end
|
data/lib/diff_matcher/version.rb
CHANGED
@@ -90,7 +90,7 @@ describe "DiffMatcher::AllMatcher[expected]" do
|
|
90
90
|
context "when actual is not an array" do
|
91
91
|
let(:actual) { 'a' }
|
92
92
|
|
93
|
-
it { should eql "\e[31m- \e[1m[
|
93
|
+
it { should eql "\e[31m- \e[1m[...]\e[0m\e[33m+ \e[1m\"a\"\e[0m" }
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
@@ -252,6 +252,40 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
|
|
252
252
|
]
|
253
253
|
Where, - 1 missing, + 1 additional
|
254
254
|
EOF
|
255
|
+
|
256
|
+
context "where actual has additional items, it summarizes the Array item with ... and" do
|
257
|
+
expected, same, different =
|
258
|
+
[ 1, 2 ],
|
259
|
+
[ 1, 2, [ 3 ] ],
|
260
|
+
[ 0, 2, [ 3 ] ]
|
261
|
+
|
262
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
263
|
+
<<-EOF, :ignore_additional=>true
|
264
|
+
[
|
265
|
+
- 1+ 0,
|
266
|
+
2,
|
267
|
+
+ [...]
|
268
|
+
]
|
269
|
+
Where, - 1 missing, + 2 additional
|
270
|
+
EOF
|
271
|
+
end
|
272
|
+
|
273
|
+
context "where actual has additional items, it summarizes the Hash item with ... and" do
|
274
|
+
expected, same, different =
|
275
|
+
[ 1, 2 ],
|
276
|
+
[ 1, 2, { :a=> [ 3 ] } ],
|
277
|
+
[ 0, 2, { :a=> [ 3 ] } ]
|
278
|
+
|
279
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
280
|
+
<<-EOF, :ignore_additional=>true
|
281
|
+
[
|
282
|
+
- 1+ 0,
|
283
|
+
2,
|
284
|
+
+ {...}
|
285
|
+
]
|
286
|
+
Where, - 1 missing, + 2 additional
|
287
|
+
EOF
|
288
|
+
end
|
255
289
|
end
|
256
290
|
|
257
291
|
context "where actual has missing items" do
|
@@ -278,6 +312,19 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
|
|
278
312
|
Where, - 1 missing
|
279
313
|
EOF
|
280
314
|
end
|
315
|
+
|
316
|
+
context "where actual is not an array, it summarizes the diff with ... and" do
|
317
|
+
expected, same, different =
|
318
|
+
[ 1 ],
|
319
|
+
[ 1 ],
|
320
|
+
{ 0 => 1 }
|
321
|
+
|
322
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
323
|
+
<<-EOF
|
324
|
+
- [...]+ {...}
|
325
|
+
Where, - 1 missing, + 1 additional
|
326
|
+
EOF
|
327
|
+
end
|
281
328
|
end
|
282
329
|
|
283
330
|
context "of an Array derived class," do
|
@@ -323,7 +370,7 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
|
|
323
370
|
Where, - 1 missing, + 1 additional
|
324
371
|
EOF
|
325
372
|
|
326
|
-
context "with
|
373
|
+
context "with values of differing classes" do
|
327
374
|
expected, same, different =
|
328
375
|
{ "a"=>{ "b"=>1 } },
|
329
376
|
{ "a"=>{ "b"=>1 } },
|
@@ -332,7 +379,7 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
|
|
332
379
|
it_behaves_like "a diff matcher", expected, same, different,
|
333
380
|
<<-EOF
|
334
381
|
{
|
335
|
-
"a"=>- {
|
382
|
+
"a"=>- {...}+ [...]
|
336
383
|
}
|
337
384
|
Where, - 1 missing, + 1 additional
|
338
385
|
EOF
|
metadata
CHANGED
@@ -1,40 +1,32 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: diff_matcher
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.3.1
|
4
5
|
prerelease:
|
5
|
-
version: 2.2.3
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Playup
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
12
|
+
date: 2012-08-02 00:00:00.000000000 +10:00
|
13
|
+
default_executable:
|
14
14
|
dependencies: []
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
- actual == expected
|
22
|
-
- actual.is_a? expected # when expected is a class
|
23
|
-
- expected.match actual # when expected is a regexp
|
24
|
-
- expected.call actual # when expected is a proc
|
25
|
-
|
15
|
+
description: ! "DiffMatcher performs recursive matches on values contained in hashes,
|
16
|
+
arrays and combinations thereof.\n\nValues in a containing object match when:\n\n
|
17
|
+
\ - actual == expected\n - actual.is_a? expected # when expected is a class\n
|
18
|
+
\ - expected.match actual # when expected is a regexp\n - expected.call actual
|
19
|
+
\ # when expected is a proc\n"
|
26
20
|
email: chris@playup.com
|
27
21
|
executables: []
|
28
|
-
|
29
22
|
extensions: []
|
30
|
-
|
31
23
|
extra_rdoc_files: []
|
32
|
-
|
33
|
-
files:
|
24
|
+
files:
|
34
25
|
- .rspec
|
35
26
|
- .travis.yml
|
36
27
|
- CHANGELOG.md
|
37
28
|
- Gemfile
|
29
|
+
- Gemfile.lock
|
38
30
|
- License.txt
|
39
31
|
- README.md
|
40
32
|
- Rakefile
|
@@ -42,40 +34,39 @@ files:
|
|
42
34
|
- diff_matcher.gemspec
|
43
35
|
- doc/diff_matcher.gif
|
44
36
|
- doc/example_output.png
|
37
|
+
- examples/tennis_score.rb
|
45
38
|
- lib/diff_matcher.rb
|
46
39
|
- lib/diff_matcher/difference.rb
|
47
40
|
- lib/diff_matcher/version.rb
|
48
41
|
- spec/diff_matcher/difference_spec.rb
|
49
42
|
- spec/spec_helper.rb
|
43
|
+
has_rdoc: true
|
50
44
|
homepage: http://github.com/playup/diff_matcher
|
51
45
|
licenses: []
|
52
|
-
|
53
46
|
post_install_message:
|
54
47
|
rdoc_options: []
|
55
|
-
|
56
|
-
require_paths:
|
48
|
+
require_paths:
|
57
49
|
- lib
|
58
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
51
|
none: false
|
60
|
-
requirements:
|
61
|
-
- -
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
|
64
|
-
segments:
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
segments:
|
65
57
|
- 0
|
66
|
-
|
67
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
hash: -173866385
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
60
|
none: false
|
69
|
-
requirements:
|
70
|
-
- -
|
71
|
-
- !ruby/object:Gem::Version
|
72
|
-
version:
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
73
65
|
requirements: []
|
74
|
-
|
75
66
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.
|
67
|
+
rubygems_version: 1.6.2
|
77
68
|
signing_key:
|
78
69
|
specification_version: 3
|
79
|
-
summary: Generates a diff by matching against expected values, classes, regexes and/or
|
70
|
+
summary: Generates a diff by matching against expected values, classes, regexes and/or
|
71
|
+
procs.
|
80
72
|
test_files: []
|
81
|
-
|