Class: Spectus::Requirement::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spectus/requirement/base.rb

Overview

Base class for implementing RFC 2119 requirement levels.

This class provides the core functionality for running tests against different requirement levels (MUST, SHOULD, MAY). It uses TestTube for test execution and Expresenter for result presentation.

Direct Known Subclasses

Optional, Recommended, Required

Instance Method Summary collapse

Constructor Details

#initialize(matcher:, negate:) ⇒ Base

Initialize the requirement level class.

Parameters:

  • matcher (#match?)

    The matcher used to evaluate the test

  • negate (Boolean)

    When true, inverts the matcher’s result

Raises:

  • (ArgumentError)

    If matcher doesn’t respond to match?



24
25
26
27
28
29
# File 'lib/spectus/requirement/base.rb', line 24

def initialize(matcher:, negate:)
  raise ::ArgumentError, "matcher must respond to match?" unless matcher.respond_to?(:match?)

  @matcher = matcher
  @negate  = negate
end

Instance Method Details

#call { ... } ⇒ ::Expresenter::Pass

Execute the test and return its result.

Runs the provided block through the matcher and evaluates the result according to the requirement level’s rules. The result is presented through an Expresenter instance containing all test details.

Examples:

test = Base.new(matcher: SomeMatcher.new, negate: false)
test.call { some_value }
# => #<Expresenter::Pass actual: some_value, ...>

Yields:

  • The block containing the code to test

Returns:

  • (::Expresenter::Pass)

    When the test passes

Raises:

  • (::Expresenter::Fail)

    When the test fails



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/spectus/requirement/base.rb', line 47

def call(&)
  test = ::TestTube.invoke(matcher: @matcher, negate: @negate, &)

  ::Expresenter.call(passed?(test)).with(
    actual:     test.actual,
    definition: @matcher.to_s,
    error:      test.error,
    got:        test.got,
    level:      self.class.level,
    negate:     @negate
  )
end