Class: UsefulMatchers::Matchers::Independent::Hashes::IncludeKeyAndValueMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb

Instance Method Summary collapse

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_messageObject 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 failure_message
  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_negatedObject 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 failure_message_when_negated
  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

Returns:

  • (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