Class: Matchi::Fix

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

Overview

Specification conformance matcher that verifies objects against Fix test specifications.

This matcher allows testing objects against Fix specifications, enabling verification of implementation conformance across different testing frameworks. It supports both anonymous specifications defined inline and named specifications registered with Fix. The matcher provides a bridge between Matchi’s matcher interface and Fix’s powerful specification system.

Examples:

Basic usage with anonymous specification

matcher = Matchi::Fix.new { it MUST be 42 }
matcher.match? { 42 }              # => true
matcher.match? { 41 }              # => false

Using named specifications

# Define a Fix specification
Fix :Calculator do
  on(:add, 2, 3) do
    it MUST eq 5
  end

  on(:multiply, 2, 3) do
    it MUST eq 6
  end
end

# Test implementations
calculator = Calculator.new
matcher = Matchi::Fix.new(:Calculator)
matcher.match? { calculator }      # => true if calculator meets spec

Complex specifications

Fix :UserValidator do
  on(:validate, name: "Alice", age: 30) do
    it MUST be true
  end

  on(:validate, name: "", age: -1) do
    it MUST be false
  end
end

validator = UserValidator.new
matcher = Matchi::Fix.new(:UserValidator)
matcher.match? { validator }       # => true if validation logic is correct

See Also:

Instance Method Summary collapse

Constructor Details

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

Initialize the matcher with either a name or a specification block.

Examples:

With anonymous specification

Fix.new { it MUST be_positive }

With named specification

Fix.new(:Calculator)

Parameters:

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

    The name of a registered Fix specification

  • block (Proc, nil)

    A block containing the specification

Raises:

  • (ArgumentError)

    if neither name nor block is provided

  • (ArgumentError)

    if both name and block are provided

  • (KeyError)

    if the named specification doesn’t exist



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

def initialize(name = nil, &block)
  raise ::ArgumentError, "a name or a block must be provided" if name.nil? && block.nil?
  raise ::ArgumentError, "a name or a block must be provided" if !name.nil? && !block.nil?

  @expected = name.nil? ? ::Fix.spec(&block) : ::Fix[name]
end

Instance Method Details

#match? { ... } ⇒ Boolean

Checks if the yielded object satisfies the Fix specification.

This method executes the Fix specification against the provided object, verifying that all specified behaviors and conditions are met. It works with both anonymous and named specifications.

Examples:

With anonymous specification

matcher = Fix.new { it MUST be_zero }
matcher.match? { 0 }             # => true
matcher.match? { 1 }             # => false

With named specification

Fix :Sortable do
  on(:sort) do
    it MUST be_kind_of(Array)
    it MUST be_sorted
  end
end

matcher = Fix.new(:Sortable)
matcher.match? { [1, 2, 3] }     # => true if array meets spec

Yields:

  • Block that returns the object to verify

Yield Returns:

  • (Object)

    The object to check against the specification

Returns:

  • (Boolean)

    true if the object satisfies the specification

Raises:

  • (ArgumentError)

    if no block is provided



109
110
111
112
113
# File 'lib/matchi/fix.rb', line 109

def match?
  raise ::ArgumentError, "a block must be provided" unless block_given?

  @expected.match?(yield)
end

#to_sString

Returns a human-readable description of the matcher.

Examples:

Fix.new(:Calculator).to_s # => "fix Calculator"
Fix.new { it MUST be 42 }.to_s # => "fix #<Fix::Spec:...>"

Returns:

  • (String)

    A string describing what this matcher verifies



124
125
126
# File 'lib/matchi/fix.rb', line 124

def to_s
  "fix #{@expected.inspect}"
end