Class: Servactory::TestKit::Rspec::Matchers::Base::AttributeMatcher
- Inherits:
-
Object
- Object
- Servactory::TestKit::Rspec::Matchers::Base::AttributeMatcher
- Includes:
- RSpec::Matchers::Composable, SubmatcherRegistry
- Defined in:
- lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb
Overview
Base class for RSpec matchers that validate Servactory attribute definitions.
## Purpose
AttributeMatcher provides the foundation for testing service attribute definitions (inputs, internals, outputs) in Servactory services. It implements the RSpec matcher protocol with fluent API for chaining validation rules.
## Usage
Subclasses define specific attribute type matchers:
“‘ruby class HaveServiceInputMatcher < Base::AttributeMatcher
for_attribute_type :input
register_submatcher :required,
class_name: "Input::RequiredSubmatcher",
mutually_exclusive_with: [:optional]
end “‘
In RSpec tests:
“‘ruby expect(MyService).to have_service_input(:name)
.type(String)
.required
.inclusion(%w[admin user])
“‘
## Architecture
Works with:
-
SubmatcherRegistry - provides DSL for registering submatchers
-
SubmatcherContext - carries context data to submatchers
-
Submatcher - base class for individual validation rules
## Features
-
**Fluent API** - chain methods for readable test assertions
-
**Dynamic Chain Methods** - generated from submatcher registry
-
**Mutual Exclusivity** - conflicting options replace each other
-
Composable - works with RSpec’s compound matchers
-
**Block Expectations** - supports ‘expect { }.to` syntax
Direct Known Subclasses
Class Attribute Summary collapse
-
.attribute_type ⇒ Symbol
readonly
The attribute type this matcher validates (:input, :internal, :output).
Instance Attribute Summary collapse
-
#attribute_name ⇒ Symbol
readonly
The name of the attribute being validated.
-
#described_class ⇒ Class
readonly
The Servactory service class being tested.
-
#option_types ⇒ Array?
readonly
Type classes passed to the types chain method.
Class Method Summary collapse
-
.for_attribute_type(type) ⇒ void
Sets the attribute type for this matcher class.
Instance Method Summary collapse
-
#description ⇒ String
Builds a human-readable description of what this matcher validates.
-
#failure_message ⇒ String
Builds the failure message when the matcher does not pass.
-
#failure_message_when_negated ⇒ String
Builds the failure message for negated expectations.
-
#initialize(described_class, attribute_name) ⇒ AttributeMatcher
constructor
Creates a new attribute matcher instance.
-
#matches?(subject) ⇒ Boolean
Checks if all submatchers pass for the given subject.
-
#supports_block_expectations? ⇒ Boolean
Indicates this matcher supports block expectations.
Methods included from SubmatcherRegistry
Constructor Details
#initialize(described_class, attribute_name) ⇒ AttributeMatcher
Creates a new attribute matcher instance.
83 84 85 86 87 88 89 90 91 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 83 def initialize(described_class, attribute_name) @described_class = described_class @attribute_name = attribute_name @submatchers = [] @option_types = nil @last_submatcher = nil build_chain_methods_from_registry end |
Class Attribute Details
.attribute_type ⇒ Symbol (readonly)
Returns The attribute type this matcher validates (:input, :internal, :output).
59 60 61 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 59 def attribute_type @attribute_type end |
Instance Attribute Details
#attribute_name ⇒ Symbol (readonly)
Returns The name of the attribute being validated.
74 75 76 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 74 def attribute_name @attribute_name end |
#described_class ⇒ Class (readonly)
Returns The Servactory service class being tested.
71 72 73 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 71 def described_class @described_class end |
#option_types ⇒ Array? (readonly)
Returns Type classes passed to the types chain method.
77 78 79 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 77 def option_types @option_types end |
Class Method Details
.for_attribute_type(type) ⇒ void
This method returns an undefined value.
Sets the attribute type for this matcher class.
65 66 67 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 65 def for_attribute_type(type) @attribute_type = type end |
Instance Method Details
#description ⇒ String
Builds a human-readable description of what this matcher validates.
114 115 116 117 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 114 def description submatcher_descriptions = submatchers.map(&:description).join(", ") "#{attribute_name} with #{submatcher_descriptions}" end |
#failure_message ⇒ String
Builds the failure message when the matcher does not pass.
122 123 124 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 122 def "Expected #{expectation}, which #{}" end |
#failure_message_when_negated ⇒ String
Builds the failure message for negated expectations.
129 130 131 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 129 def "Did not expect #{expectation} with specified options" end |
#matches?(subject) ⇒ Boolean
Checks if all submatchers pass for the given subject.
106 107 108 109 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 106 def matches?(subject) @subject = subject failing_submatchers.empty? end |
#supports_block_expectations? ⇒ Boolean
Indicates this matcher supports block expectations.
Required by RSpec for matchers used with ‘expect { }.to` syntax.
98 99 100 |
# File 'lib/servactory/test_kit/rspec/matchers/base/attribute_matcher.rb', line 98 def supports_block_expectations? true end |