Class: Matchi::Fix

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/fix.rb

Overview

Fix matcher.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, &block) ⇒ Fix

Initialize the matcher with a behavioral definition.

Examples:

With a block of specifications

require "matchi/fix"

Matchi::Fix.new { it MUST be 42 }

With the constant name of the specifications

require "matchi/fix"

Fix :Answer do
  it MUST be 42
end

Matchi::Fix.new(:Answer)

Parameters:

  • name (String, Symbol) (defaults to: nil)

    The constant name of the specifications.

  • block (Proc)

    A block of specifications.


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/matchi/fix.rb', line 30

def initialize(name = nil, &block)
  @expected = if name.nil?
                raise ::ArgumentError, "Pass either an argument or a block" unless block

                Fix(&block)
              else
                raise ::ArgumentError, "Can't pass both an argument and a block" if block

                @name = name
                ::Fix[name]
              end
end

Instance Attribute Details

#expected#test (readonly)

Returns A set of specifications.

Returns:

  • (#test)

    A set of specifications.


10
11
12
# File 'lib/matchi/fix.rb', line 10

def expected
  @expected
end

Instance Method Details

#inspectObject

A string containing a human-readable representation of the matcher.


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

def inspect
  "#{self.class}(#{parameter})"
end

#matches?(&block) ⇒ Boolean

Boolean comparison between an actual value and the expected specs.

Examples:

With a block of specifications

require "matchi/fix"

matcher = Matchi::Fix.new { it MUST be 42 }

matcher.expected        # => #<Fix::Set:0x00007fd96915dc28 ...>
matcher.matches? { 42 } # => true

With the constant name of the specifications

require "matchi/fix"

Fix :Answer do
  it MUST be 42
end

matcher = Matchi::Fix.new(:Answer)

matcher.expected        # => #<Fix::Set:0x00007fd96915dc28 ...>
matcher.matches? { 42 } # => true

Yield Returns:

  • (#object_id)

    The value to be compared to the specifications.

Returns:

  • (Boolean)

    Actual value test on specifications.


68
69
70
71
72
# File 'lib/matchi/fix.rb', line 68

def matches?(&block)
  expected.test(log_level: 0, &block)
rescue ::SystemExit => e
  e.success?
end

#to_sObject

Returns a string representing the matcher.


80
81
82
# File 'lib/matchi/fix.rb', line 80

def to_s
  "fix #{parameter}"
end