Module: Fix::Matcher Private

Included in:
Dsl
Defined in:
lib/fix/matcher.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.

Collection of expectation matchers.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ 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


215
216
217
218
219
# File 'lib/fix/matcher.rb', line 215

def method_missing(name, ...)
  return super unless predicate_matcher_name?(name)

  ::Matchi::Predicate.new(name, ...)
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.



41
42
43
# File 'lib/fix/matcher.rb', line 41

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.



155
156
157
# File 'lib/fix/matcher.rb', line 155

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.



123
124
125
# File 'lib/fix/matcher.rb', line 123

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.



139
140
141
# File 'lib/fix/matcher.rb', line 139

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.



107
108
109
# File 'lib/fix/matcher.rb', line 107

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.



59
60
61
# File 'lib/fix/matcher.rb', line 59

def be_within(delta)
  ::Matchi::BeWithin.new(delta)
end

#change(object, method) ⇒ #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.

Returns:

  • (#matches?)

    A change matcher.



188
189
190
# File 'lib/fix/matcher.rb', line 188

def change(object, method, ...)
  ::Matchi::Change.new(object, method, ...)
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.



22
23
24
# File 'lib/fix/matcher.rb', line 22

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.



75
76
77
# File 'lib/fix/matcher.rb', line 75

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

#raise_exception(expected) ⇒ #matches?

Expecting errors matcher

Examples:

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

Parameters:

  • expected (Exception, #to_s)

    The expected exception name.

Returns:

  • (#matches?)

    An error matcher.



91
92
93
# File 'lib/fix/matcher.rb', line 91

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.



203
204
205
# File 'lib/fix/matcher.rb', line 203

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