diff_matcher 2.7.0 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85c693faa10896940ac3eebf11393668fd78b0e1
4
- data.tar.gz: 38be21bd9cd7d420fc53d2a1378156eefcb7c841
3
+ metadata.gz: 269ffc7ffb370219902eab20adbb33123e12de62
4
+ data.tar.gz: eeae047d47e866671da62f7fb2f6c5723cf9e935
5
5
  SHA512:
6
- metadata.gz: 37da42959f13bd42a3c736b959c8f9685bfc0f44f3a14ae330a3586658f71cceefcad2e5ff223bcfd6d1958b0b25b39f6d1efbe631f852dd0e28a1af0abd2ff6
7
- data.tar.gz: f2ac31ed19ecdc51f28a0a5ca93a81a4a7d4292a717a770556adc60c43512d763ea00f2c23460216cad04296cb5980f4805958f9459bc956eefe60e970335df3
6
+ metadata.gz: 826869c482e6c5a60b9b6132e19dd64296be1d7aafa12d66e580d309f624806ee43724bc3cebc2ff78039eca8362e6cde7fe798eba9b351f570b26240ca206cd
7
+ data.tar.gz: b76a7c7ceec440089b683436969a73b88730a28e670ce0e4565dd79d0f0192f116fae79c4f3bcf1d7dd0a48a5648b29a24b247c66cce5ae1f144601d954a42b3
@@ -10,9 +10,14 @@ rvm:
10
10
  - jruby
11
11
  - ree
12
12
  - 2.0.0
13
- - 2.1.1
13
+ - 2.1
14
+ - 2.2
15
+ - 2.3.0
14
16
  - ruby-head
15
17
 
18
+ before_install:
19
+ - gem install bundler -v '1.11.2'
20
+
16
21
  matrix:
17
22
  fast_finish: true
18
23
  allow_failures:
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ### v2.7.1
4
+
5
+ * Use `failure_message` from RSpec matchers in diff output. (denyago)
6
+
3
7
  ### v2.7.0
4
8
 
5
9
  * Add rspec custom matcher `be_json_matching` for Rspec v2.x & v3.x
data/README.md CHANGED
@@ -5,21 +5,23 @@ DiffMatcher
5
5
  [![Gem Version](https://badge.fury.io/rb/diff_matcher.png)](http://badge.fury.io/rb/diff_matcher)
6
6
  [![Dependency Status](https://gemnasium.com/diff-matcher/diff_matcher.png)](https://gemnasium.com/diff-matcher/diff_matcher)
7
7
  [![still maintained](http://stillmaintained.com/diff-matcher/diff_matcher.png)](http://stillmaintained.com/diff-matcher/diff_matcher)
8
+ [![diff_matcher API Documentation](https://www.omniref.com/ruby/gems/diff_matcher.png)](https://www.omniref.com/ruby/gems/diff_matcher)
8
9
 
9
10
  Generates a diff by matching against user-defined matchers written in ruby.
10
11
 
11
12
  DiffMatcher matches input data (eg. from a JSON API) against values,
12
- ranges, classes, regexes, procs, custom matchers and/or easily composed,
13
+ ranges, classes, regexes, procs, custom matchers, RSpec matchers and/or easily composed,
13
14
  nested combinations thereof to produce an easy to read diff string.
14
15
 
15
16
  Actual input values are matched against expected matchers in the following way:
16
17
 
17
18
  ``` ruby
18
- actual.is_a? expected # when expected is a class
19
- expected.match actual # when expected is a regexp
20
- expected.call actual # when expected is a proc
21
- actual == expected # when expected is anything else
22
- expected.diff actual # when expected is a built-in DiffMatcher
19
+ actual.is_a? expected # when expected is a class
20
+ expected.match actual # when expected is a regexp
21
+ expected.call actual # when expected is a proc
22
+ expected.matches? actual # when expected implements an RSpec Matcher interface
23
+ actual == expected # when expected is anything else
24
+ expected.diff actual # when expected is a built-in DiffMatcher
23
25
  ```
24
26
 
25
27
  Using these building blocks, more complicated nested matchers can be
@@ -200,6 +202,43 @@ puts DiffMatcher::difference(expected, [1, 2.00, "3"])
200
202
  # => Where, - 1 missing, + 1 additional, | 2 match_matcher
201
203
  ```
202
204
 
205
+ When `actual` is matched against some custom logic, an object with an RSpec Matcher
206
+ interface can be used. (NB. `#mathches?(actual)`, `#description` and `#failure_message`
207
+ methods must be implemented.)
208
+
209
+ So you can use one of the RSpec matchers or you can implement your own.
210
+
211
+ ```ruby
212
+ class BeAWeekend
213
+ def matches?(day)
214
+ @day = day
215
+ %w{Sat Sun}.include?(day)
216
+ end
217
+
218
+ def description
219
+ 'be a weekend day'
220
+ end
221
+
222
+ def failure_message
223
+ "expected #{@day} to #{description}"
224
+ end
225
+ end
226
+
227
+ be_a_weekend = BeAWeekend.new
228
+ puts DiffMatcher::difference(be_a_weekend, 'Mon')
229
+ # => - expected Mon to be a weekend day+ "Mon"
230
+ # => Where, - 1 missing, + 1 additional
231
+
232
+
233
+ all_be_weekends = DiffMatcher::AllMatcher.new(be_a_weekend)
234
+ puts DiffMatcher::difference(all_be_weekends, ['Sat', 'Mon'])
235
+ # => [
236
+ # => R "Sat" be a weekend day,
237
+ # => - expected Mon to be a weekend day+ "Mon"
238
+ # => ]
239
+ # => Where, - 1 missing, + 1 additional, R 1 match_rspec
240
+ ```
241
+
203
242
  ### Matcher options
204
243
 
205
244
  Matcher options can be passed to `DiffMatcher::difference` or `DiffMatcher::Matcher#diff`
@@ -284,6 +323,7 @@ in the following way:
284
323
  match matcher => "| "
285
324
  match range => ". "
286
325
  match proc => "{ "
326
+ match rspec => "R "
287
327
 
288
328
 
289
329
  #### Colours
@@ -300,6 +340,7 @@ Using the `:default` colour scheme items shown in a difference are coloured as f
300
340
  match matcher => blue
301
341
  match range => cyan
302
342
  match proc => cyan
343
+ match rspec => cyan
303
344
 
304
345
  Other colour schemes, eg. `:color_scheme=>:white_background` will use different colour mappings.
305
346
 
@@ -88,7 +88,8 @@ module DiffMatcher
88
88
  :match_class => [BLUE , ":"],
89
89
  :match_matcher => [BLUE , "|"],
90
90
  :match_range => [CYAN , "."],
91
- :match_proc => [CYAN , "{"]
91
+ :match_proc => [CYAN , "{"],
92
+ :match_rspec => [CYAN , "R"]
92
93
  }
93
94
 
94
95
  class << self
@@ -186,7 +187,7 @@ module DiffMatcher
186
187
  @matches_shown ||= lambda {
187
188
  ret = []
188
189
  unless @quiet
189
- ret += [:match_matcher, :match_class, :match_range, :match_proc, :match_regexp]
190
+ ret += [:match_matcher, :match_class, :match_range, :match_proc, :match_regexp, :match_rspec]
190
191
  ret += [:match_value]
191
192
  end
192
193
  ret
@@ -273,7 +274,12 @@ module DiffMatcher
273
274
  when Range ; [expected.include?(actual) , :match_range ]
274
275
  when Proc ; [expected.call(actual) , :match_proc ]
275
276
  when Regexp ; [actual.is_a?(String) && actual.match(expected) , :match_regexp ]
276
- else [actual == expected , :match_value ]
277
+ else
278
+ if expected.respond_to?(:matches?)
279
+ [expected.matches?(actual), :match_rspec]
280
+ else
281
+ [actual == expected, :match_value]
282
+ end
277
283
  end
278
284
  end
279
285
 
@@ -294,6 +300,7 @@ module DiffMatcher
294
300
 
295
301
  def match_to_s(expected, actual, match_type)
296
302
  actual = match_regexp_to_s(expected, actual) if match_type == :match_regexp
303
+ actual = "#{actual} #{expected.description}" if match_type == :match_rspec
297
304
  markup(match_type, actual) if matches_shown.include?(match_type)
298
305
  end
299
306
 
@@ -308,6 +315,16 @@ module DiffMatcher
308
315
  end
309
316
  end
310
317
 
318
+ def summarize_rspec_matcher(matcher)
319
+ if matcher.respond_to?(:failure_message)
320
+ matcher.failure_message
321
+ elsif matcher.respond_to?(:failure_message_for_should)
322
+ matcher.failure_message_for_should
323
+ else
324
+ summarize_item(matcher)
325
+ end
326
+ end
327
+
311
328
  def difference_to_s(expected, actual)
312
329
  match, match_type, d = match?(expected, actual)
313
330
  if match
@@ -316,6 +333,8 @@ module DiffMatcher
316
333
  case match_type
317
334
  when :match_matcher
318
335
  d
336
+ when :match_rspec
337
+ "#{markup(:missing, summarize_rspec_matcher(expected))}#{markup(:additional, summarize_item(actual))}"
319
338
  else
320
339
  exp, act = if [expected, actual].any? { |item| item.is_a?(Proc) }
321
340
  [expected, actual].map { |item| item.inspect }
@@ -1,3 +1,3 @@
1
1
  module DiffMatcher
2
- VERSION = "2.7.0"
2
+ VERSION = "2.7.1"
3
3
  end
@@ -470,6 +470,36 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
470
470
  end
471
471
  end
472
472
 
473
+ context 'a duck-type responding to #matches?' do
474
+ matchable = Class.new do
475
+ def initialize(expected)
476
+ @expected = expected
477
+ end
478
+
479
+ def matches?(actual)
480
+ actual == @expected
481
+ end
482
+
483
+ def failure_message
484
+ "the matcher should be eq '#{@expected}'"
485
+ end
486
+
487
+ def description
488
+ "be eq #{@expected}"
489
+ end
490
+ end
491
+ expected, same, different =
492
+ matchable.new('foo'),
493
+ 'foo',
494
+ 'bar'
495
+
496
+ it_behaves_like "a diff matcher", expected, same, different,
497
+ <<-EOF
498
+ - the matcher should be eq 'foo'+ "bar"
499
+ Where, - 1 missing, + 1 additional
500
+ EOF
501
+ end
502
+
473
503
  context "a proc," do
474
504
  expected, same, different =
475
505
  lambda { |x| [FalseClass, TrueClass].include? x.class },
@@ -654,12 +684,25 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
654
684
  end
655
685
 
656
686
  context "when expected has multiple items," do
687
+ matchable = Class.new do
688
+ def initialize(expected)
689
+ @expected = expected
690
+ end
691
+
692
+ def matches?(actual)
693
+ actual == @expected
694
+ end
695
+
696
+ def description
697
+ "be == #{@expected}"
698
+ end
699
+ end
657
700
  expected, same, different =
658
- [ 1, 2, /\d/, Fixnum, 4..6 , lambda { |x| x % 6 == 0 } ],
659
- [ 1, 2, "3" , 4 , 5 , 6 ],
660
- [ 0, 2, "3" , 4 , 5 , 6 ]
701
+ [ 1, 2, /\d/, Fixnum, 4..6 , lambda { |x| x % 6 == 0 }, matchable.new(7)],
702
+ [ 1, 2, "3" , 4 , 5 , 6 , 7 ],
703
+ [ 0, 2, "3" , 4 , 5 , 6 , 7 ]
661
704
 
662
- describe "it shows regex, class, range, proc matches and matches" do
705
+ describe "it shows regex, class, range, proc matches, matches and RSpec matcher" do
663
706
  it_behaves_like "a diff matcher", expected, same, different,
664
707
  <<-EOF
665
708
  [
@@ -668,9 +711,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
668
711
  ~ "(3)",
669
712
  : 4,
670
713
  . 5,
671
- { 6
714
+ { 6,
715
+ R 7 be == 7
672
716
  ]
673
- Where, - 1 missing, + 1 additional, ~ 1 match_regexp, : 1 match_class, . 1 match_range, { 1 match_proc
717
+ Where, - 1 missing, + 1 additional, ~ 1 match_regexp, : 1 match_class, . 1 match_range, { 1 match_proc, R 1 match_rspec
674
718
  EOF
675
719
  end
676
720
 
@@ -693,9 +737,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
693
737
  ~ "(3)",
694
738
  : 4,
695
739
  . 5,
696
- { 6
740
+ { 6,
741
+ R 7 be == 7
697
742
  ]
698
- Where, - 1 missing, + 1 additional, ~ 1 match_regexp, : 1 match_class, . 1 match_range, { 1 match_proc
743
+ Where, - 1 missing, + 1 additional, ~ 1 match_regexp, : 1 match_class, . 1 match_range, { 1 match_proc, R 1 match_rspec
699
744
  EOF
700
745
  end
701
746
 
@@ -708,9 +753,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
708
753
  \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
709
754
  \e[0m \e[34m: \e[1m4\e[0m,
710
755
  \e[0m \e[36m. \e[1m5\e[0m,
711
- \e[0m \e[36m{ \e[1m6\e[0m
756
+ \e[0m \e[36m{ \e[1m6\e[0m,
757
+ \e[0m \e[36mR \e[1m7 be == 7\e[0m
712
758
  \e[0m]
713
- Where, \e[31m- \e[1m1 missing\e[0m, \e[33m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
759
+ Where, \e[31m- \e[1m1 missing\e[0m, \e[33m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m, \e[36mR \e[1m1 match_rspec\e[0m
714
760
  EOF
715
761
 
716
762
  context "on a white background" do
@@ -722,9 +768,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
722
768
  \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
723
769
  \e[0m \e[34m: \e[1m4\e[0m,
724
770
  \e[0m \e[36m. \e[1m5\e[0m,
725
- \e[0m \e[36m{ \e[1m6\e[0m
771
+ \e[0m \e[36m{ \e[1m6\e[0m,
772
+ \e[0m \e[36mR \e[1m7 be == 7\e[0m
726
773
  \e[0m]
727
- Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
774
+ Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m, \e[36mR \e[1m1 match_rspec\e[0m
728
775
  EOF
729
776
  end
730
777
 
@@ -739,9 +786,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
739
786
  \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
740
787
  \e[0m \e[34m: \e[1m4\e[0m,
741
788
  \e[0m \e[36m. \e[1m5\e[0m,
742
- \e[0m \e[36m{ \e[1m6\e[0m
789
+ \e[0m \e[36m{ \e[1m6\e[0m,
790
+ \e[0m \e[36mR \e[1m7 be == 7\e[0m
743
791
  \e[0m]
744
- Where, \e[31m- \e[1m1 missing\e[0m, \e[33m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
792
+ Where, \e[31m- \e[1m1 missing\e[0m, \e[33m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m, \e[36mR \e[1m1 match_rspec\e[0m
745
793
  EOF
746
794
  end
747
795
 
@@ -756,9 +804,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
756
804
  \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
757
805
  \e[0m \e[34m: \e[1m4\e[0m,
758
806
  \e[0m \e[36m. \e[1m5\e[0m,
759
- \e[0m \e[36m{ \e[1m6\e[0m
807
+ \e[0m \e[36m{ \e[1m6\e[0m,
808
+ \e[0m \e[36mR \e[1m7 be == 7\e[0m
760
809
  \e[0m]
761
- Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
810
+ Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m, \e[36mR \e[1m1 match_rspec\e[0m
762
811
  EOF
763
812
  end
764
813
  end
@@ -778,9 +827,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
778
827
  \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
779
828
  \e[0m \e[34m: \e[1m4\e[0m,
780
829
  \e[0m \e[36m. \e[1m5\e[0m,
781
- \e[0m \e[36m{ \e[1m6\e[0m
830
+ \e[0m \e[36m{ \e[1m6\e[0m,
831
+ \e[0m \e[36mR \e[1m7 be == 7\e[0m
782
832
  \e[0m]
783
- Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
833
+ Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m, \e[36mR \e[1m1 match_rspec\e[0m
784
834
  EOF
785
835
  end
786
836
 
@@ -794,9 +844,10 @@ describe "DiffMatcher::difference(expected, actual, opts)" do
794
844
  <span style=\"color:green\">~ </b></span>\"<span style=\"color:green\">(<b>3</b></span><span style=\"color:green\">)</b></span>\"</b></span>,
795
845
  <span style=\"color:blue\">: <b>4</b></span>,
796
846
  <span style=\"color:cyan\">. <b>5</b></span>,
797
- <span style=\"color:cyan\">{ <b>6</b></span>
847
+ <span style=\"color:cyan\">{ <b>6</b></span>,
848
+ <span style=\"color:cyan\">R <b>7 be == 7</b></span>
798
849
  ]
799
- Where, <span style=\"color:red\">- <b>1 missing</b></span>, <span style=\"color:magenta\">+ <b>1 additional</b></span>, <span style=\"color:green\">~ <b>1 match_regexp</b></span>, <span style=\"color:blue\">: <b>1 match_class</b></span>, <span style=\"color:cyan\">. <b>1 match_range</b></span>, <span style=\"color:cyan\">{ <b>1 match_proc</b></span>
850
+ Where, <span style=\"color:red\">- <b>1 missing</b></span>, <span style=\"color:magenta\">+ <b>1 additional</b></span>, <span style=\"color:green\">~ <b>1 match_regexp</b></span>, <span style=\"color:blue\">: <b>1 match_class</b></span>, <span style=\"color:cyan\">. <b>1 match_range</b></span>, <span style=\"color:cyan\">{ <b>1 match_proc</b></span>, <span style=\"color:cyan\">R <b>1 match_rspec</b></span>
800
851
  </pre>
801
852
  EOF
802
853
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diff_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - locochris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-30 00:00:00.000000000 Z
11
+ date: 2016-03-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  DiffMatcher matches input data (eg. from a JSON API) against values,
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  requirements: []
68
68
  rubyforge_project:
69
- rubygems_version: 2.4.2
69
+ rubygems_version: 2.2.5
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: Generates a diff by matching against user-defined matchers written in ruby.
@@ -74,3 +74,4 @@ test_files:
74
74
  - spec/diff_matcher/difference_spec.rb
75
75
  - spec/diff_matcher/rspec/matchers/diff_matcher_spec.rb
76
76
  - spec/spec_helper.rb
77
+ has_rdoc: