Matchi
Collection of expectation matchers for Ruby.
Contact
- Home page: https://github.com/fixrb/matchi
- Bugs/issues: https://github.com/fixrb/matchi/issues
- Support: https://stackoverflow.com/questions/tagged/matchi
Rubies
Installation
Add this line to your application's Gemfile:
gem 'matchi'
And then execute:
$ bundle
Or install it yourself as:
$ gem install matchi
Usage
Built-in matchers
Equivalence matcher:
eql = Matchi::Eql.new('foo')
eql.matches? { 'foo' } # => true
Identity matcher:
equal = Matchi::Equal.new(:foo)
equal.matches? { :foo } # => true
Regular expressions matcher:
match = Matchi::Match.new(/^foo$/)
match.matches? { 'foo' } # => true
Expecting errors matcher:
raise_exception = Matchi::RaiseException.new(NameError)
raise_exception.matches? { Boom } # => true
Custom matchers
Custom matchers can easily be defined for expressing expectations.
Be the answer matcher:
module Matchi
class BeTheAnswer
def matches?
42.equal? yield
end
end
end
be_the_answer = Matchi::BeTheAnswer.new
be_the_answer.matches? { 42 } # => true
Be prime matcher:
require 'prime'
module Matchi
class BePrime
def matches?
Prime.prime? yield
end
end
end
be_prime = Matchi::BePrime.new
be_prime.matches? { 42 } # => false
Start with matcher:
module Matchi
class StartWith
def initialize expected
@expected = expected
end
def matches?
!Regexp.new("^#{@expected}").match(yield).nil?
end
end
end
start_with = Matchi::StartWith.new('foo')
start_with.matches? { 'foobar' } # => true
Versioning
Matchi follows Semantic Versioning 2.0.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request