Class: Phlex::Sorbet::RSpec::Matchers::HaveProps
- Inherits:
-
Object
- Object
- Phlex::Sorbet::RSpec::Matchers::HaveProps
- Defined in:
- lib/phlex/sorbet/rspec/matchers.rb
Overview
Matcher for validating multiple props at once
Instance Method Summary collapse
- #and_prop(field_name, expected_type) ⇒ Object
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(args_hash_or_field = nil, expected_type = nil) ⇒ HaveProps
constructor
A new instance of HaveProps.
- #matches?(component_class) ⇒ Boolean
Constructor Details
#initialize(args_hash_or_field = nil, expected_type = nil) ⇒ HaveProps
Returns a new instance of HaveProps.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 120 def initialize(args_hash_or_field = nil, expected_type = nil) @props_to_check = [] if args_hash_or_field.is_a?(Hash) args_hash_or_field.each do |field_name, type| @props_to_check << [field_name, type] end elsif args_hash_or_field.is_a?(Symbol) @props_to_check << [args_hash_or_field, expected_type] end end |
Instance Method Details
#and_prop(field_name, expected_type) ⇒ Object
132 133 134 135 |
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 132 def and_prop(field_name, expected_type) @props_to_check << [field_name, expected_type] self end |
#description ⇒ Object
159 160 161 162 |
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 159 def description desc = @props_to_check.map { |name, type| "#{name}: #{type}" }.join(", ") "have props: #{desc}" end |
#failure_message ⇒ Object
151 152 153 |
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 151 def @failure_message || "expected #{@actual} to have the specified props" end |
#failure_message_when_negated ⇒ Object
155 156 157 |
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 155 def "expected #{@actual}::Props not to have the specified props" end |
#matches?(component_class) ⇒ Boolean
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 137 def matches?(component_class) @actual = component_class @props_to_check.each do |field_name, expected_type| matcher = HaveProp.new(field_name, expected_type) unless matcher.matches?(component_class) @failure_message = matcher. return false end end true end |