README.md in diff_matcher-2.4.0 vs README.md in diff_matcher-2.4.1
- old
+ new
@@ -1,10 +1,12 @@
DiffMatcher
===
-[](http://travis-ci.org/playup/diff_matcher)
-[](http://stillmaintained.com/playupchris/diff_matcher)
+[](https://travis-ci.org/diff-matcher/diff_matcher)
+[](http://badge.fury.io/rb/diff_matcher)
+[](https://gemnasium.com/diff-matcher/diff_matcher)
+[](http://stillmaintained.com/diff-matcher/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,
@@ -299,12 +301,12 @@
match range => cyan
match proc => cyan
Other colour schemes, eg. `:color_scheme=>:white_background` will use different colour mappings.
-
+
Similar gems
---
### String differs
* <http://difflcs.rubyforge.org> (A resonably fast diff algorithm using longest common substrings)
@@ -351,38 +353,44 @@
* because it doesn't bother generating a pretty difference string it might be faster
Use with rspec
---
-To use with rspec create the following custom matcher:
+To use with rspec, create a custom matcher. The following example matcher works with rspec-1.2.4 and up.
``` ruby
require 'diff_matcher'
-module RSpec
- module Matchers
- class BeMatching
- include BaseMatcher
+# Uses the diff_matcher gem to provide advanced matching abilities, along with nice visual representation of the
+# diff between actual and expected. The functionality set is very helpful for comparing hashes.
+#
+# Usage examples:
+# it { should be_matching(my_var) }
+# it { should be_matching(my_var).with_options(ignore_additional: true) }
+#
+# Options: by default, color_enabled is controlled by Rspec, and quiet is set to true.
+RSpec::Matchers.define :be_matching do |expected|
+ match do |actual|
+ options = { :color_enabled => RSpec::configuration.color_enabled?, :quiet => true }.merge(@options || {})
+ @difference = DiffMatcher::Difference.new(expected, actual, options)
+ @difference.matching?
+ end
- def initialize(expected, opts)
- @expected = expected
- @opts = opts.update(:color_enabled=>RSpec::configuration.color_enabled?)
- end
+ chain :with_options do |options|
+ @options = options
+ end
- def matches?(actual)
- @difference = DiffMatcher::Difference.new(expected, actual, @opts)
- @difference.matching?
- end
+ failure_message_for_should do |actual|
+ "diff is:\n" + @difference.to_s
+ end
- def failure_message_for_should
- @difference.to_s
- end
- end
+ failure_message_for_should_not do |actual|
+ "diff is:\n" + @difference.to_s
+ end
- def be_matching(expected, opts={})
- Matchers::BeMatching.new(expected, opts)
- end
+ description do
+ "match via DiffMatcher #{expected}" + (@options.blank? ? '' : " with options: #{@options}")
end
end
```
And use it with:
@@ -390,11 +398,11 @@
``` ruby
describe "hash matcher" do
subject { { :a=>1, :b=>2, :c=>'3', :d=>4, :e=>"additional stuff" } }
let(:expected) { { :a=>1, :b=>Fixnum, :c=>/[0-9]/, :d=>lambda { |x| (3..5).include?(x) } } }
- it { should be_matching(expected, :ignore_additional=>true) }
+ it { should be_matching(expected).with_options(:ignore_additional=>true) }
it { should be_matching(expected) }
end
```
Will result in:
@@ -429,7 +437,7 @@
Status
---
Our company is using this gem to test our JSON API which has got above and beyond a stable v1.0.0 release.
-There's a [pull request](http://github.com/rspec/rspec-expectations/pull/79) to use this gem in a `be_hash_matching`
+There's a [pull request](http://github.com/rspec/rspec-expectations/pull/79) to use this gem in a `be_hash_matching`
[rspec matcher](https://www.relishapp.com/rspec/rspec-expectations).