Class: Matchi::Fix

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

Overview

Fix specing 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
42
# File 'lib/matchi/fix.rb', line 30

def initialize(name = nil, &block)
  @name = name

  @expected = if unnamed?
                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

                ::Fix[name]
              end
end

Instance Attribute Details

#expected#against (readonly)

Returns A set of specifications.

Returns:

  • (#against)

    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.



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

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)

    Determines whether the test has passed or failed.



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

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

#to_sObject

Returns a string representing the matcher.



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

def to_s
  "fix #{parameter}"
end