Code Monkey home page Code Monkey logo

matchi's Introduction

Matchi

Build Status Code Climate Gem Version Inline docs Documentation

Collection of expectation matchers for Ruby.

Contact

Rubies

Installation

Matchi is cryptographically signed.

To be sure the gem you install hasn't been tampered with, add my public key (if you haven't already) as a trusted certificate:

$ gem cert --add <(curl -Ls https://raw.github.com/fixrb/matchi/master/certs/gem-fixrb-public_cert.pem)
$ gem install matchi -P HighSecurity

The HighSecurity trust profile will verify all gems. All of Matchi's dependencies are signed.

Or add this line to your application's Gemfile:

gem 'matchi'

And then execute:

$ bundle

Usage

Built-in matchers

Equivalence matcher:

eql = Matchi::Matchers::Eql::Matcher.new('foo')
eql.matches? { 'foo' } # => true

Identity matcher:

equal = Matchi::Matchers::Equal::Matcher.new(:foo)
equal.matches? { :foo } # => true

Regular expressions matcher:

match = Matchi::Matchers::Match::Matcher.new(/^foo$/)
match.matches? { 'foo' } # => true

Expecting errors matcher:

raise_exception = Matchi::Matchers::RaiseException::Matcher.new(NameError)
raise_exception.matches? { Boom } # => true

Truth matcher:

be_true = Matchi::Matchers::BeTrue::Matcher.new
be_true.matches? { true } # => true

Untruth matcher:

be_false = Matchi::Matchers::BeFalse::Matcher.new
be_false.matches? { false } # => true

Nil matcher:

be_nil = Matchi::Matchers::BeNil::Matcher.new
be_nil.matches? { nil } # => true

Custom matchers

Custom matchers can easily be defined for expressing expectations. They can be any Ruby class that responds to matches?, to_s and to_h instance methods.

A Be the answer matcher:

module Matchi
  module Matchers
    module BeTheAnswer
      class Matcher
        def matches?
          42.equal? yield
        end

        def to_s
          'be_the_answer'
        end

        def to_h
          { BeTheAnswer: [] }
        end
      end
    end
  end
end

be_the_answer = Matchi::Matchers::BeTheAnswer::Matcher.new
be_the_answer.matches? { 42 } # => true

A Be prime matcher:

require 'prime'

module Matchi
  module Matchers
    module BePrime
      class Matcher
        def matches?
          Prime.prime? yield
        end

        def to_s
          'be_prime'
        end

        def to_h
          { BePrime: [] }
        end
      end
    end
  end
end

be_prime = Matchi::Matchers::BePrime::Matcher.new
be_prime.matches? { 42 } # => false

A Start with matcher:

module Matchi
  module Matchers
    module StartWith
      class Matcher
        def initialize(expected)
          @expected = expected
        end

        def matches?
          !Regexp.new("^#{@expected}").match(yield).nil?
        end

        def to_s
          'start_with'
        end

        def to_h
          { StartWith: [@expected] }
        end
      end
    end
  end
end

start_with = Matchi::Matchers::StartWith::Matcher.new('foo')
start_with.matches? { 'foobar' } # => true

Security

As a basic form of security Matchi provides a set of SHA512 checksums for every Gem release. These checksums can be found in the checksum/ directory. Although these checksums do not prevent malicious users from tampering with a built Gem they can be used for basic integrity verification purposes.

The checksum of a file can be checked using the sha512sum command. For example:

$ sha512sum pkg/matchi-0.0.1.gem
548d9f669ded4e622182791a5390aaceae0bf2e557b0864f05a842b0be2c65e10e1fb8499f49a3b9efd0e8eaeb691351b1c670d6316ce49965a99683b1071389  pkg/matchi-0.0.1.gem

Versioning

Matchi follows Semantic Versioning 2.0.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

See LICENSE.md file.


This project is sponsored by:

Sashite

matchi's People

Contributors

cyril avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.