module RSpec::Clone::ExpectationHelper::Shared

Abstract expectation helper base module.

This module defines a number of methods to create expectations, which are automatically included into examples.

It also includes a collection of expectation matchers.

@see github.com/fixrb/matchi

Public Instance Methods

be(expected) click to toggle source

Identity matcher

@example

object = "foo"
matcher = be(object)
matcher.matches? { object } # => true
matcher.matches? { "foo" } # => false

@param expected [#equal?] The expected identical object.

@return [#matches?] An identity matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 48
def be(expected)
  ::Matchi::Be.new(expected)
end
Also aliased as: equal
be_an_instance_of(expected) click to toggle source

Type/class matcher

@example

matcher = be_an_instance_of(String)
matcher.matches? { "foo" } # => true
matcher.matches? { 4 } # => false

@param expected [Class, to_s] The expected class name.

@return [#matches?] A type/class matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 162
def be_an_instance_of(expected)
  ::Matchi::BeAnInstanceOf.new(expected)
end
be_false() click to toggle source

False matcher

@example

matcher = be_false
matcher.matches? { false } # => true
matcher.matches? { true } # => false
matcher.matches? { nil } # => false
matcher.matches? { 4 } # => false

@return [#matches?] A `false` matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 130
def be_false
  be(false)
end
be_nil() click to toggle source

Nil matcher

@example

matcher = be_nil
matcher.matches? { nil } # => true
matcher.matches? { false } # => false
matcher.matches? { true } # => false
matcher.matches? { 4 } # => false

@return [#matches?] A `nil` matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 146
def be_nil
  be(nil)
end
be_true() click to toggle source

True matcher

@example

matcher = be_true
matcher.matches? { true } # => true
matcher.matches? { false } # => false
matcher.matches? { nil } # => false
matcher.matches? { 4 } # => false

@return [#matches?] A `true` matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 114
def be_true
  be(true)
end
be_within(delta) click to toggle source

Comparisons matcher

@example

matcher = be_within(1).of(41)
matcher.matches? { 42 } # => true
matcher.matches? { 43 } # => false

@param delta [Numeric] A numeric value.

@return [#matches?] A comparison matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 66
def be_within(delta)
  ::Matchi::BeWithin.new(delta)
end
change(object, method, *args, **kwargs, &block) click to toggle source

Change matcher

@example

object = []
matcher = change(object, :length).by(1)
matcher.matches? { object << 1 } # => true

object = []
matcher = change(object, :length).by_at_least(1)
matcher.matches? { object << 1 } # => true

object = []
matcher = change(object, :length).by_at_most(1)
matcher.matches? { object << 1 } # => true

object = "foo"
matcher = change(object, :to_s).from("foo").to("FOO")
matcher.matches? { object.upcase! } # => true

object = "foo"
matcher = change(object, :to_s).to("FOO")
matcher.matches? { object.upcase! } # => true

@param object [#object_id] An object. @param method [Symbol] The name of a method. @param args [Array] A list of arguments. @param kwargs [Hash] A list of keyword arguments.

@return [#matches?] A change matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 197
def change(object, method, *args, **kwargs, &block)
  ::Matchi::Change.new(object, method, *args, **kwargs, &block)
end
eq(expected) click to toggle source

Equivalence matcher

@example

matcher = eq("foo")
matcher.matches? { "foo" } # => true
matcher.matches? { "bar" } # => false

@param expected [#eql?] An expected equivalent object.

@return [#matches?] An equivalence matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 29
def eq(expected)
  ::Matchi::Eq.new(expected)
end
Also aliased as: eql
eql(expected)
Alias for: eq
equal(expected)
Alias for: be
match(expected) click to toggle source

Regular expressions matcher

@example

matcher = match(/^foo$/)
matcher.matches? { "foo" } # => true
matcher.matches? { "bar" } # => false

@param expected [#match] A regular expression.

@return [#matches?] A regular expression matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 82
def match(expected)
  ::Matchi::Match.new(expected)
end
raise_exception(expected) click to toggle source

Expecting errors matcher

@example

matcher = raise_exception(NameError)
matcher.matches? { RSpec::Clone::Boom! } # => true
matcher.matches? { true } # => false

@param expected [Exception, to_s] The expected exception name.

@return [#matches?] An error matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 98
def raise_exception(expected)
  ::Matchi::RaiseException.new(expected)
end
satisfy(&expected) click to toggle source

Satisfy matcher

@example

matcher = satisfy { |value| value == 42 }
matcher.matches? { 42 } # => true

@param expected [Proc] A block of code.

@return [#matches?] A satisfy matcher.

@api public

# File lib/r_spec/clone/expectation_helper/shared.rb, line 212
def satisfy(&expected)
  ::Matchi::Satisfy.new(&expected)
end

Private Instance Methods

method_missing(name, *args, **kwargs, &block) click to toggle source

Predicate matcher, or default method missing behavior.

@example Empty predicate matcher

matcher = be_empty
matcher.matches? { [] } # => true
matcher.matches? { [4] } # => false
Calls superclass method
# File lib/r_spec/clone/expectation_helper/shared.rb, line 224
def method_missing(name, *args, **kwargs, &block)
  return super unless predicate_matcher_name?(name)

  ::Matchi::Predicate.new(name, *args, **kwargs, &block)
end
predicate_matcher_name?(name) click to toggle source

Predicate matcher name detector.

@param name [Array, Symbol] The name of a potential predicate matcher.

@return [Boolean] Indicates if it is a predicate matcher name or not.

# File lib/r_spec/clone/expectation_helper/shared.rb, line 244
def predicate_matcher_name?(name)
  name.start_with?("be_", "have_") && !name.end_with?("!", "?")
end
respond_to_missing?(name, include_private = false) click to toggle source

Hook method to return whether the obj can respond to id method or not.

Calls superclass method
# File lib/r_spec/clone/expectation_helper/shared.rb, line 233
def respond_to_missing?(name, include_private = false)
  predicate_matcher_name?(name) || super
end