Spectus

Version Yard documentation Ruby RuboCop License

A Ruby library for defining expectations with precision, using RFC 2119 compliance levels. 🚥

A traffic light with three distinct sections

Installation

Add this line to your application's Gemfile:

gem "spectus"

And then execute:

bundle install

Or install it yourself as:

gem install spectus

Usage

The Spectus library is basically a module defining methods that can be used to qualify expectations in specifications.

To make Spectus available:

require "spectus"

For convenience, we will also instantiate some matchers from the Matchi library:

gem install matchi
require "matchi"

All examples here assume that this has been done.

Absolute Requirement

There is exactly one bat:

definition = Spectus.must Matchi::Be.new(1)
definition.call { "🦇".size }
# => Expresenter::Pass(actual: 1, definition: "be 1", error: nil, expected: 1, got: true, negate: false, level: :MUST)

The test is passed.

Absolute Prohibition

Truth and lies:

definition = Spectus.must_not Matchi::Be.new(true)
definition.call { false }
# => Expresenter::Pass(actual: false, definition: "be true", error: nil, expected: true, got: true, negate: true, level: :MUST)

A well-known joke. The addition of 0.1 and 0.2 is deadly precise:

definition = Spectus.should Matchi::Be.new(0.3)
definition.call { 0.1 + 0.2 }
# => Expresenter::Pass(actual: 0.30000000000000004, definition: "be 0.3", error: nil, expected: 0.3, got: false, negate: false, level: :SHOULD)

This should not be wrong:

definition = Spectus.should_not Matchi::Match.new("123456")

definition.call do
  require "securerandom"

  SecureRandom.hex(3)
end
# => Expresenter::Pass(actual: "bb5716", definition: "match \"123456\"", error: nil, expected: "123456", got: true, negate: true, level: :SHOULD)

In any case, as long as there are no exceptions, the test passes.

Optional

An empty array is blank, right?

definition = Spectus.may Matchi::Be.new(true)
definition.call { [].blank? }
# => Expresenter::Pass(actual: nil, definition: "be true", error: #<NoMethodError: undefined method `blank?' for []:Array>, expected: true, got: nil, negate: false, level: :MAY)

My bad! ActiveSupport was not imported. 🤦‍♂️

Anyways, the test passes because the exception produced is NoMethodError, meaning that the functionality is not implemented.

Contact

Versioning

Spectus follows Semantic Versioning 2.0.

License

The gem is available as open source under the terms of the MIT License.


This project is sponsored by:
Sashité