README.md in diff_matcher-2.7.0 vs README.md in diff_matcher-2.7.1

- old
+ new

@@ -3,25 +3,27 @@ [![Build Status](https://secure.travis-ci.org/diff-matcher/diff_matcher.png)](https://travis-ci.org/diff-matcher/diff_matcher) [![Gem Version](https://badge.fury.io/rb/diff_matcher.png)](http://badge.fury.io/rb/diff_matcher) [![Dependency Status](https://gemnasium.com/diff-matcher/diff_matcher.png)](https://gemnasium.com/diff-matcher/diff_matcher) [![still maintained](http://stillmaintained.com/diff-matcher/diff_matcher.png)](http://stillmaintained.com/diff-matcher/diff_matcher) +[![diff_matcher API Documentation](https://www.omniref.com/ruby/gems/diff_matcher.png)](https://www.omniref.com/ruby/gems/diff_matcher) Generates a diff by matching against user-defined matchers written in ruby. DiffMatcher matches input data (eg. from a JSON API) against values, -ranges, classes, regexes, procs, custom matchers and/or easily composed, +ranges, classes, regexes, procs, custom matchers, RSpec matchers and/or easily composed, nested combinations thereof to produce an easy to read diff string. Actual input values are matched against expected matchers in the following way: ``` ruby -actual.is_a? expected # when expected is a class -expected.match actual # when expected is a regexp -expected.call actual # when expected is a proc -actual == expected # when expected is anything else -expected.diff actual # when expected is a built-in DiffMatcher +actual.is_a? expected # when expected is a class +expected.match actual # when expected is a regexp +expected.call actual # when expected is a proc +expected.matches? actual # when expected implements an RSpec Matcher interface +actual == expected # when expected is anything else +expected.diff actual # when expected is a built-in DiffMatcher ``` Using these building blocks, more complicated nested matchers can be composed. eg. @@ -198,10 +200,47 @@ # => - Float+ "3" # => ] # => Where, - 1 missing, + 1 additional, | 2 match_matcher ``` +When `actual` is matched against some custom logic, an object with an RSpec Matcher +interface can be used. (NB. `#mathches?(actual)`, `#description` and `#failure_message` +methods must be implemented.) + +So you can use one of the RSpec matchers or you can implement your own. + +```ruby +class BeAWeekend + def matches?(day) + @day = day + %w{Sat Sun}.include?(day) + end + + def description + 'be a weekend day' + end + + def failure_message + "expected #{@day} to #{description}" + end +end + +be_a_weekend = BeAWeekend.new +puts DiffMatcher::difference(be_a_weekend, 'Mon') +# => - expected Mon to be a weekend day+ "Mon" +# => Where, - 1 missing, + 1 additional + + +all_be_weekends = DiffMatcher::AllMatcher.new(be_a_weekend) +puts DiffMatcher::difference(all_be_weekends, ['Sat', 'Mon']) +# => [ +# => R "Sat" be a weekend day, +# => - expected Mon to be a weekend day+ "Mon" +# => ] +# => Where, - 1 missing, + 1 additional, R 1 match_rspec +``` + ### Matcher options Matcher options can be passed to `DiffMatcher::difference` or `DiffMatcher::Matcher#diff` or instances of `DiffMatcher::Matcher` @@ -282,10 +321,11 @@ match regexp => "~ " match class => ": " match matcher => "| " match range => ". " match proc => "{ " + match rspec => "R " #### Colours Colours (defined in colour schemes) can also appear in the difference. @@ -298,9 +338,10 @@ match regexp => green match class => blue match matcher => blue match range => cyan match proc => cyan + match rspec => cyan Other colour schemes, eg. `:color_scheme=>:white_background` will use different colour mappings.