diff_matcher 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -31
- data/doc/diff_matcher.gif +0 -0
- data/doc/example_output.png +0 -0
- data/lib/diff_matcher/difference.rb +1 -2
- data/lib/diff_matcher/version.rb +1 -1
- data/spec/diff_matcher/difference_spec.rb +43 -51
- metadata +4 -3
data/README.md
CHANGED
@@ -2,6 +2,7 @@ DiffMatcher
|
|
2
2
|
===
|
3
3
|
|
4
4
|
[![build status](http://travis-ci.org/playup/diff_matcher.png)](http://travis-ci.org/playup/diff_matcher)
|
5
|
+
[![still maintained](http://stillmaintained.com/playupchris/diff_matcher.png)](http://stillmaintained.com/playupchris/diff_matcher)
|
5
6
|
|
6
7
|
Generates a diff by matching against expected values, classes, regexes and/or procs.
|
7
8
|
|
@@ -9,10 +10,24 @@ DiffMatcher performs recursive matches on values contained in hashes, arrays and
|
|
9
10
|
|
10
11
|
Values in a containing object match when:
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
``` ruby
|
14
|
+
actual.is_a? expected # when expected is a class
|
15
|
+
expected.match actual # when expected is a regexp
|
16
|
+
expected.call actual # when expected is a proc
|
17
|
+
actual == expected # when expected is anything else
|
18
|
+
```
|
19
|
+
|
20
|
+
Example:
|
21
|
+
|
22
|
+
``` ruby
|
23
|
+
puts DiffMatcher::difference(
|
24
|
+
{ :a=>{ :a1=>11 }, :b=>[ 21, 22 ], :c=>/\d/, :d=>Fixnum, :e=>lambda { |x| (4..6).include? x } },
|
25
|
+
{ :a=>{ :a1=>10, :a2=>12 }, :b=>[ 21 ], :c=>'3' , :d=>4 , :e=>5 },
|
26
|
+
:color_scheme=>:white_background
|
27
|
+
)
|
28
|
+
```
|
29
|
+
|
30
|
+
![example output](https://raw.github.com/playup/diff_matcher/master/doc/diff_matcher.gif)
|
16
31
|
|
17
32
|
|
18
33
|
Installation
|
@@ -84,7 +99,7 @@ puts DiffMatcher::difference([1], [1, 2])
|
|
84
99
|
# => [
|
85
100
|
# => + 2
|
86
101
|
# => ]
|
87
|
-
# => Where,
|
102
|
+
# => Where, + 1 additional
|
88
103
|
```
|
89
104
|
|
90
105
|
### Options
|
@@ -96,7 +111,7 @@ p DiffMatcher::difference([1], [1, 2], :ignore_additional=>true)
|
|
96
111
|
# => nil
|
97
112
|
```
|
98
113
|
|
99
|
-
`:
|
114
|
+
`:quiet=>true` shows only missing and additional items in the output
|
100
115
|
|
101
116
|
``` ruby
|
102
117
|
puts DiffMatcher::difference([Fixnum, 2], [1], :quiet=>true)
|
@@ -106,21 +121,8 @@ puts DiffMatcher::difference([Fixnum, 2], [1], :quiet=>true)
|
|
106
121
|
# => Where, - 1 missing
|
107
122
|
```
|
108
123
|
|
109
|
-
`:verbose=>true` shows all matched items in the output
|
110
|
-
|
111
|
-
``` ruby
|
112
|
-
puts DiffMatcher::difference([Fixnum, 2], [1], :verbose=>true)
|
113
|
-
# => [
|
114
|
-
# = > : 1,
|
115
|
-
# => - 2
|
116
|
-
# => ]
|
117
|
-
# => Where, - 1 missing, : 1 match_class
|
118
|
-
```
|
119
|
-
|
120
124
|
#### Prefixes
|
121
125
|
|
122
|
-
NB. The `: 1` from above includes a `:` prefix that shows the `1` was matched against a class (ie. `Fixnum`)
|
123
|
-
|
124
126
|
The items shown in a difference are prefixed as follows:
|
125
127
|
|
126
128
|
missing => "- "
|
@@ -130,6 +132,7 @@ The items shown in a difference are prefixed as follows:
|
|
130
132
|
match class => ": "
|
131
133
|
match proc => "{ "
|
132
134
|
|
135
|
+
|
133
136
|
#### Colours
|
134
137
|
|
135
138
|
Colours (defined in colour schemes) can also appear in the difference.
|
@@ -143,19 +146,9 @@ Using the `:default` colour scheme items shown in a difference are coloured as f
|
|
143
146
|
match class => blue
|
144
147
|
match proc => cyan
|
145
148
|
|
149
|
+
Other colour schemes, eg. `:color_scheme=>:white_background` will use different colour mappings.
|
146
150
|
|
147
|
-
|
148
|
-
|
149
|
-
``` ruby
|
150
|
-
puts DiffMatcher::difference(
|
151
|
-
{ :a=>{ :a1=>11 }, :b=>[ 21, 22 ], :c=>/\d/, :d=>Fixnum, :e=>lambda { |x| (4..6).includes? x },
|
152
|
-
{ :a=>{ :a1=>10, :a2=>12 }, :b=>[ 21 ], :c=>'3' , :d=>4 , :e=>5 },
|
153
|
-
:verbose=>true, :color_scheme=>:white_background
|
154
|
-
)
|
155
|
-
```
|
156
|
-
|
157
|
-
![example output](https://raw.github.com/playup/diff_matcher/master/doc/example_output.png)
|
158
|
-
|
151
|
+
|
159
152
|
|
160
153
|
Similar gems
|
161
154
|
---
|
Binary file
|
data/doc/example_output.png
CHANGED
Binary file
|
@@ -40,7 +40,6 @@ module DiffMatcher
|
|
40
40
|
def initialize(expected, actual, opts={})
|
41
41
|
@ignore_additional = opts[:ignore_additional]
|
42
42
|
@quiet = opts[:quiet]
|
43
|
-
@verbose = opts[:verbose]
|
44
43
|
@color_enabled = opts[:color_enabled] || !!opts[:color_scheme]
|
45
44
|
@color_scheme = COLOR_SCHEMES[opts[:color_scheme] || :default]
|
46
45
|
@difference = difference(expected, actual)
|
@@ -89,7 +88,7 @@ module DiffMatcher
|
|
89
88
|
ret = []
|
90
89
|
unless @quiet
|
91
90
|
ret += [:match_class, :match_proc, :match_regexp]
|
92
|
-
ret += [:match_value]
|
91
|
+
ret += [:match_value]
|
93
92
|
end
|
94
93
|
ret
|
95
94
|
}.call
|
data/lib/diff_matcher/version.rb
CHANGED
@@ -12,10 +12,10 @@ def fix_EOF_problem(s)
|
|
12
12
|
s.gsub("\n#{" " * indentation}", "\n").strip
|
13
13
|
end
|
14
14
|
|
15
|
-
describe DiffMatcher do
|
15
|
+
describe "DiffMatcher::difference(expected, actual, opts)" do
|
16
16
|
subject { DiffMatcher::difference(expected, actual, opts) }
|
17
17
|
|
18
|
-
shared_examples_for "a
|
18
|
+
shared_examples_for "a diff matcher" do |expected, same, different, difference, opts|
|
19
19
|
opts ||= {}
|
20
20
|
context "with #{opts.size > 0 ? opts_to_s(opts) : "no opts"}" do
|
21
21
|
describe "difference(#{expected.inspect}, #{same.inspect}#{opts_to_s(opts)})" do
|
@@ -26,7 +26,7 @@ describe DiffMatcher do
|
|
26
26
|
it { should be_nil }
|
27
27
|
end
|
28
28
|
|
29
|
-
describe "
|
29
|
+
describe "difference(#{expected.inspect}, #{different.inspect}#{opts_to_s(opts)})" do
|
30
30
|
let(:expected) { expected }
|
31
31
|
let(:actual ) { different }
|
32
32
|
let(:opts ) { opts }
|
@@ -48,7 +48,7 @@ describe DiffMatcher do
|
|
48
48
|
1,
|
49
49
|
2
|
50
50
|
|
51
|
-
it_behaves_like "a
|
51
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
52
52
|
<<-EOF, {}
|
53
53
|
- 1+ 2
|
54
54
|
Where, - 1 missing, + 1 additional
|
@@ -61,7 +61,7 @@ describe DiffMatcher do
|
|
61
61
|
"a",
|
62
62
|
"b"
|
63
63
|
|
64
|
-
it_behaves_like "a
|
64
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
65
65
|
<<-EOF, {}
|
66
66
|
- "a"+ "b"
|
67
67
|
Where, - 1 missing, + 1 additional
|
@@ -70,7 +70,7 @@ describe DiffMatcher do
|
|
70
70
|
context "when actual is of a different class" do
|
71
71
|
different = 0
|
72
72
|
|
73
|
-
it_behaves_like "a
|
73
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
74
74
|
<<-EOF, {}
|
75
75
|
- "a"+ 0
|
76
76
|
Where, - 1 missing, + 1 additional
|
@@ -80,7 +80,7 @@ describe DiffMatcher do
|
|
80
80
|
context "when actual is nil" do
|
81
81
|
different = nil
|
82
82
|
|
83
|
-
it_behaves_like "a
|
83
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
84
84
|
<<-EOF, {}
|
85
85
|
- "a"+ nil
|
86
86
|
Where, - 1 missing, + 1 additional
|
@@ -94,7 +94,7 @@ describe DiffMatcher do
|
|
94
94
|
nil,
|
95
95
|
false
|
96
96
|
|
97
|
-
it_behaves_like "a
|
97
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
98
98
|
<<-EOF, {}
|
99
99
|
- nil+ false
|
100
100
|
Where, - 1 missing, + 1 additional
|
@@ -107,7 +107,7 @@ describe DiffMatcher do
|
|
107
107
|
[ 1 ],
|
108
108
|
[ 2 ]
|
109
109
|
|
110
|
-
it_behaves_like "a
|
110
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
111
111
|
<<-EOF, {}
|
112
112
|
[
|
113
113
|
- 1+ 2
|
@@ -121,32 +121,23 @@ describe DiffMatcher do
|
|
121
121
|
[ 1, 2, 3 ],
|
122
122
|
[ 0, 2, 3 ]
|
123
123
|
|
124
|
-
it_behaves_like "a
|
124
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
125
125
|
<<-EOF, :ignore_additional=>true
|
126
126
|
[
|
127
127
|
- 1+ 0,
|
128
|
+
2,
|
128
129
|
+ 3
|
129
130
|
]
|
130
131
|
Where, - 1 missing, + 2 additional
|
131
132
|
EOF
|
132
133
|
|
133
|
-
it_behaves_like "a
|
134
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
134
135
|
<<-EOF, :ignore_additional=>true, :quiet=>true
|
135
136
|
[
|
136
137
|
- 1+ 0
|
137
138
|
]
|
138
139
|
Where, - 1 missing, + 1 additional
|
139
140
|
EOF
|
140
|
-
|
141
|
-
it_behaves_like "a differ", expected, same, different,
|
142
|
-
<<-EOF, :ignore_additional=>true, :verbose=>true
|
143
|
-
[
|
144
|
-
- 1+ 0,
|
145
|
-
2,
|
146
|
-
+ 3
|
147
|
-
]
|
148
|
-
Where, - 1 missing, + 2 additional
|
149
|
-
EOF
|
150
141
|
end
|
151
142
|
|
152
143
|
context "where actual has missing items" do
|
@@ -155,16 +146,16 @@ describe DiffMatcher do
|
|
155
146
|
[ 1, 2, 3 ],
|
156
147
|
[ 1, 2 ]
|
157
148
|
|
158
|
-
it_behaves_like "a
|
159
|
-
<<-EOF, {}
|
149
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
150
|
+
<<-EOF, { :quiet => true }
|
160
151
|
[
|
161
152
|
- 3
|
162
153
|
]
|
163
154
|
Where, - 1 missing
|
164
155
|
EOF
|
165
156
|
|
166
|
-
it_behaves_like "a
|
167
|
-
<<-EOF
|
157
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
158
|
+
<<-EOF
|
168
159
|
[
|
169
160
|
1,
|
170
161
|
2,
|
@@ -181,8 +172,8 @@ describe DiffMatcher do
|
|
181
172
|
{ "a"=>1 },
|
182
173
|
{ "a"=>2 }
|
183
174
|
|
184
|
-
it_behaves_like "a
|
185
|
-
<<-EOF
|
175
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
176
|
+
<<-EOF
|
186
177
|
{
|
187
178
|
"a"=>- 1+ 2
|
188
179
|
}
|
@@ -195,8 +186,8 @@ describe DiffMatcher do
|
|
195
186
|
{ "a"=>{ "b"=>1 } },
|
196
187
|
{ "a"=>[ "b", 1 ] }
|
197
188
|
|
198
|
-
it_behaves_like "a
|
199
|
-
<<-EOF
|
189
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
190
|
+
<<-EOF
|
200
191
|
{
|
201
192
|
"a"=>- {"b"=>1}+ ["b", 1]
|
202
193
|
}
|
@@ -211,8 +202,8 @@ describe DiffMatcher do
|
|
211
202
|
{ "b"=>{ "c"=>1 } }
|
212
203
|
|
213
204
|
describe "it won't match the descendents" do
|
214
|
-
it_behaves_like "a
|
215
|
-
<<-EOF
|
205
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
206
|
+
<<-EOF
|
216
207
|
{
|
217
208
|
- "a"=>{"b"=>{"c"=>1}},
|
218
209
|
+ "b"=>{"c"=>1}
|
@@ -231,8 +222,8 @@ describe DiffMatcher do
|
|
231
222
|
"a",
|
232
223
|
1
|
233
224
|
|
234
|
-
it_behaves_like "a
|
235
|
-
<<-EOF
|
225
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
226
|
+
<<-EOF
|
236
227
|
- String+ 1
|
237
228
|
Where, - 1 missing, + 1 additional
|
238
229
|
EOF
|
@@ -246,8 +237,8 @@ describe DiffMatcher do
|
|
246
237
|
"a",
|
247
238
|
"A"
|
248
239
|
|
249
|
-
it_behaves_like "a
|
250
|
-
<<-EOF
|
240
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
241
|
+
<<-EOF
|
251
242
|
- /[a-z]/+ "A"
|
252
243
|
Where, - 1 missing, + 1 additional
|
253
244
|
EOF
|
@@ -255,8 +246,8 @@ describe DiffMatcher do
|
|
255
246
|
context "and when actual is not a String," do
|
256
247
|
different = :a
|
257
248
|
|
258
|
-
it_behaves_like "a
|
259
|
-
<<-EOF
|
249
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
250
|
+
<<-EOF
|
260
251
|
- /[a-z]/+ :a
|
261
252
|
Where, - 1 missing, + 1 additional
|
262
253
|
EOF
|
@@ -271,7 +262,7 @@ describe DiffMatcher do
|
|
271
262
|
true,
|
272
263
|
"true"
|
273
264
|
|
274
|
-
it_behaves_like "a
|
265
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
275
266
|
/- #<Proc.*?>\+ \"true\"\nWhere, - 1 missing, \+ 1 additional/
|
276
267
|
end
|
277
268
|
end
|
@@ -282,11 +273,12 @@ describe DiffMatcher do
|
|
282
273
|
[ 1, 2, "3" , 4 , 5 ],
|
283
274
|
[ 0, 2, "3" , 4 , 5 ]
|
284
275
|
|
285
|
-
describe "it shows regex, class, proc matches and" do
|
286
|
-
it_behaves_like "a
|
287
|
-
<<-EOF
|
276
|
+
describe "it shows regex, class, proc matches and matches" do
|
277
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
278
|
+
<<-EOF
|
288
279
|
[
|
289
280
|
- 1+ 0,
|
281
|
+
2,
|
290
282
|
~ (3),
|
291
283
|
: 4,
|
292
284
|
{ 5
|
@@ -295,8 +287,8 @@ describe DiffMatcher do
|
|
295
287
|
EOF
|
296
288
|
end
|
297
289
|
|
298
|
-
describe "it doesn't show matches
|
299
|
-
it_behaves_like "a
|
290
|
+
describe "it doesn't show matches" do
|
291
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
300
292
|
<<-EOF, :quiet=>true
|
301
293
|
[
|
302
294
|
- 1+ 0
|
@@ -305,9 +297,9 @@ describe DiffMatcher do
|
|
305
297
|
EOF
|
306
298
|
end
|
307
299
|
|
308
|
-
describe "it shows all matches
|
309
|
-
it_behaves_like "a
|
310
|
-
<<-EOF
|
300
|
+
describe "it shows all matches" do
|
301
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
302
|
+
<<-EOF
|
311
303
|
[
|
312
304
|
- 1+ 0,
|
313
305
|
2,
|
@@ -319,9 +311,9 @@ describe DiffMatcher do
|
|
319
311
|
EOF
|
320
312
|
end
|
321
313
|
|
322
|
-
describe "it shows matches in color
|
323
|
-
it_behaves_like "a
|
324
|
-
<<-EOF
|
314
|
+
describe "it shows matches in color" do
|
315
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
316
|
+
<<-EOF , :color_scheme=>:default
|
325
317
|
\e[0m[
|
326
318
|
\e[0m \e[31m- \e[1m1\e[0m\e[33m+ \e[1m0\e[0m,
|
327
319
|
\e[0m 2,
|
@@ -333,8 +325,8 @@ describe DiffMatcher do
|
|
333
325
|
EOF
|
334
326
|
|
335
327
|
context "on a white background" do
|
336
|
-
it_behaves_like "a
|
337
|
-
<<-EOF
|
328
|
+
it_behaves_like "a diff matcher", expected, same, different,
|
329
|
+
<<-EOF , :color_scheme=>:white_background
|
338
330
|
\e[0m[
|
339
331
|
\e[0m \e[31m- \e[1m1\e[0m\e[35m+ \e[1m0\e[0m,
|
340
332
|
\e[0m 2,
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: diff_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version:
|
5
|
+
version: 2.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Playup
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-18 00:00:00 +11:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- README.md
|
40
40
|
- Rakefile
|
41
41
|
- diff_matcher.gemspec
|
42
|
+
- doc/diff_matcher.gif
|
42
43
|
- doc/example_output.png
|
43
44
|
- lib/diff_matcher.rb
|
44
45
|
- lib/diff_matcher/difference.rb
|
@@ -59,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
60
|
requirements:
|
60
61
|
- - ">="
|
61
62
|
- !ruby/object:Gem::Version
|
62
|
-
hash:
|
63
|
+
hash: 465980701
|
63
64
|
segments:
|
64
65
|
- 0
|
65
66
|
version: "0"
|