Class: UsefulMatchers::Matchers::Independent::Hashes::IncludeKeyAndValueMatcher
- Inherits:
-
Object
- Object
- UsefulMatchers::Matchers::Independent::Hashes::IncludeKeyAndValueMatcher
- Defined in:
- lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb
Instance Method Summary collapse
- #failure_message ⇒ Object (also: #failure_message_for_should)
- #failure_message_when_negated ⇒ Object (also: #failure_message_for_should_not)
-
#initialize(key) ⇒ IncludeKeyAndValueMatcher
constructor
A new instance of IncludeKeyAndValueMatcher.
- #matches?(subject) ⇒ Boolean
- #with_value(value) ⇒ Object
Constructor Details
#initialize(key) ⇒ IncludeKeyAndValueMatcher
Returns a new instance of IncludeKeyAndValueMatcher.
10 11 12 13 14 15 16 17 18 |
# File 'lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb', line 10 def initialize(key) @key = key @value = nil # Use boolean flag since we can't test whether @value is set by testing whether @value is nil # because we should be able to evaluate nil values. @value_set = false @subject = nil end |
Instance Method Details
#failure_message ⇒ Object Also known as: failure_message_for_should
36 37 38 39 40 41 42 |
# File 'lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb', line 36 def if @value_set "expected that the value for key #{format_key} in the hash is #{format_value}" else "expected that the hash contains #{format_key} as a key" end end |
#failure_message_when_negated ⇒ Object Also known as: failure_message_for_should_not
45 46 47 48 49 50 51 |
# File 'lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb', line 45 def if @value_set "expected that the value for key #{format_key} in the hash isn't #{format_value}" else "expected that the hash doesn't contain #{format_key} as a key" end end |
#matches?(subject) ⇒ Boolean
20 21 22 23 24 25 26 27 28 |
# File 'lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb', line 20 def matches?(subject) @subject = subject if @value_set ensure_subject_has_target_key && ensure_target_key_corresponds_to_target_value else ensure_subject_has_target_key end end |
#with_value(value) ⇒ Object
30 31 32 33 34 |
# File 'lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb', line 30 def with_value(value) @value = value @value_set = true self end |