Module: Spectus
- Defined in:
- lib/spectus.rb,
lib/spectus/requirement.rb,
lib/spectus/requirement/base.rb,
lib/spectus/requirement/optional.rb,
lib/spectus/requirement/required.rb,
lib/spectus/requirement/recommended.rb
Overview
A Ruby library for defining expectations with precision using RFC 2119 compliance levels.
This module provides methods to define expectations according to different requirement levels (MUST, SHOULD, MAY). Each method accepts a matcher object that responds to ‘match?` and follows the block-passing protocol.
While the Matchi gem provides a collection of ready-to-use matchers, you can create your own custom matchers:
Defined Under Namespace
Modules: Requirement
Class Method Summary collapse
-
.may(matcher) ⇒ Requirement::Optional
Defines an optional feature or behavior.
-
.must(matcher) ⇒ Requirement::Required
Defines an absolute requirement that must be satisfied by the implementation.
-
.must_not(matcher) ⇒ Requirement::Required
Defines an absolute prohibition in the specification.
-
.should(matcher) ⇒ Requirement::Recommended
Defines a recommended requirement that should be satisfied unless there are valid reasons not to.
-
.should_not(matcher) ⇒ Requirement::Recommended
Defines a behavior that is not recommended but may be acceptable in specific circumstances.
Class Method Details
.may(matcher) ⇒ Requirement::Optional
Defines an optional feature or behavior. This represents the RFC 2119 “MAY” level - where an item is truly optional. Implementations can freely choose whether to include the item based on their specific needs, while maintaining interoperability with other implementations.
For MAY requirements, a test passes in two cases:
-
When a NoMethodError is raised, indicating the feature is not implemented
-
When the feature is implemented and the test succeeds
203 204 205 206 207 |
# File 'lib/spectus.rb', line 203 def self.may(matcher) raise ::ArgumentError, "matcher must respond to match?" unless matcher.respond_to?(:match?) Requirement::Optional.new(negate: false, matcher:) end |
.must(matcher) ⇒ Requirement::Required
Defines an absolute requirement that must be satisfied by the implementation. This represents the RFC 2119 “MUST” level - an absolute requirement of the specification.
64 65 66 67 68 |
# File 'lib/spectus.rb', line 64 def self.must(matcher) raise ::ArgumentError, "matcher must respond to match?" unless matcher.respond_to?(:match?) Requirement::Required.new(negate: false, matcher:) end |
.must_not(matcher) ⇒ Requirement::Required
Defines an absolute prohibition in the specification. This represents the RFC 2119 “MUST NOT” level - an absolute prohibition.
95 96 97 98 99 |
# File 'lib/spectus.rb', line 95 def self.must_not(matcher) raise ::ArgumentError, "matcher must respond to match?" unless matcher.respond_to?(:match?) Requirement::Required.new(negate: true, matcher:) end |
.should(matcher) ⇒ Requirement::Recommended
Defines a recommended requirement that should be satisfied unless there are valid reasons not to. This represents the RFC 2119 “SHOULD” level - where valid reasons may exist to ignore a particular item, but the implications must be understood and weighed.
127 128 129 130 131 |
# File 'lib/spectus.rb', line 127 def self.should(matcher) raise ::ArgumentError, "matcher must respond to match?" unless matcher.respond_to?(:match?) Requirement::Recommended.new(negate: false, matcher:) end |
.should_not(matcher) ⇒ Requirement::Recommended
Defines a behavior that is not recommended but may be acceptable in specific circumstances. This represents the RFC 2119 “SHOULD NOT” level - where particular behavior may be acceptable but the implications should be understood and the case carefully weighed.
162 163 164 165 166 |
# File 'lib/spectus.rb', line 162 def self.should_not(matcher) raise ::ArgumentError, "matcher must respond to match?" unless matcher.respond_to?(:match?) Requirement::Recommended.new(negate: true, matcher:) end |