Module: RSpec::Clone::ExpectationHelper::Shared Private

Included in:
It, Its
Defined in:
lib/r_spec/clone/expectation_helper/shared.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Predicate matcher, or default method missing behavior.

Examples:

Empty predicate matcher

matcher = be_empty
matcher.matches? { [] } # => true
matcher.matches? { [4] } # => false

224
225
226
227
228
# 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

Instance Method Details

#be(expected) ⇒ #matches? Also known as: equal

Identity matcher

Examples:

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

Parameters:

  • expected (#equal?)

    The expected identical object.

Returns:

  • (#matches?)

    An identity matcher.


48
49
50
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 48

def be(expected)
  ::Matchi::Be.new(expected)
end

#be_an_instance_of(expected) ⇒ #matches?

Type/class matcher

Examples:

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

Parameters:

  • expected (Class, #to_s)

    The expected class name.

Returns:

  • (#matches?)

    A type/class matcher.


162
163
164
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 162

def be_an_instance_of(expected)
  ::Matchi::BeAnInstanceOf.new(expected)
end

#be_false#matches?

False matcher

Examples:

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

Returns:

  • (#matches?)

    A `false` matcher.


130
131
132
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 130

def be_false
  be(false)
end

#be_nil#matches?

Nil matcher

Examples:

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

Returns:

  • (#matches?)

    A `nil` matcher.


146
147
148
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 146

def be_nil
  be(nil)
end

#be_true#matches?

True matcher

Examples:

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

Returns:

  • (#matches?)

    A `true` matcher.


114
115
116
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 114

def be_true
  be(true)
end

#be_within(delta) ⇒ #matches?

Comparisons matcher

Examples:

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

Parameters:

  • delta (Numeric)

    A numeric value.

Returns:

  • (#matches?)

    A comparison matcher.


66
67
68
# 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) ⇒ #matches?

Change matcher

Examples:

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

Parameters:

  • object (#object_id)

    An object.

  • method (Symbol)

    The name of a method.

  • args (Array)

    A list of arguments.

  • kwargs (Hash)

    A list of keyword arguments.

Returns:

  • (#matches?)

    A change matcher.


197
198
199
# 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) ⇒ #matches? Also known as: eql

Equivalence matcher

Examples:

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

Parameters:

  • expected (#eql?)

    An expected equivalent object.

Returns:

  • (#matches?)

    An equivalence matcher.


29
30
31
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 29

def eq(expected)
  ::Matchi::Eq.new(expected)
end

#match(expected) ⇒ #matches?

Regular expressions matcher

Examples:

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

Parameters:

  • expected (#match)

    A regular expression.

Returns:

  • (#matches?)

    A regular expression matcher.


82
83
84
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 82

def match(expected)
  ::Matchi::Match.new(expected)
end

#raise_exception(expected) ⇒ #matches?

Expecting errors matcher

Examples:

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

Parameters:

  • expected (Exception, #to_s)

    The expected exception name.

Returns:

  • (#matches?)

    An error matcher.


98
99
100
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 98

def raise_exception(expected)
  ::Matchi::RaiseException.new(expected)
end

#satisfy(&expected) ⇒ #matches?

Satisfy matcher

Examples:

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

Parameters:

  • expected (Proc)

    A block of code.

Returns:

  • (#matches?)

    A satisfy matcher.


212
213
214
# File 'lib/r_spec/clone/expectation_helper/shared.rb', line 212

def satisfy(&expected)
  ::Matchi::Satisfy.new(&expected)
end