Class: Yabeda::RSpec::ObserveYabedaSummary

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/yabeda/rspec/observe_yabeda_summary.rb

Overview

Custom matcher class with implementation for observe_yabeda_summary

Instance Attribute Summary collapse

Attributes inherited from BaseMatcher

#metric, #tags

Instance Method Summary collapse

Methods inherited from BaseMatcher

#does_not_match?, #expected_formatted, #supports_block_expectations?, #with_tags

Constructor Details

#initializeObserveYabedaSummary

Returns a new instance of ObserveYabedaSummary.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 23

def initialize(*)
  super
  return if metric.is_a? Yabeda::Summary

  raise ArgumentError, "Pass summary instance/name to `observe_yabeda_summary`. Got #{metric.inspect} instead"
end

Instance Attribute Details

#expected_valueObject (readonly)

Returns the value of attribute expected_value.



21
22
23
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 21

def expected_value
  @expected_value
end

Instance Method Details

#actual_changes_messageObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 67

def actual_changes_message
  observations = Yabeda::TestAdapter.instance.summaries.fetch(metric)
  if observations.empty?
    "no observations of this summary have been made"
  elsif tags && observations.key?(tags)
    formatted_tags = ::RSpec::Support::ObjectFormatter.format(tags)
    "has been observed with #{observations.fetch(tags)} with tags #{formatted_tags}"
  else
    "following observations have been made: #{::RSpec::Support::ObjectFormatter.format(observations)}"
  end
end

#failure_messageObject



53
54
55
56
57
58
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 53

def failure_message
  "expected #{expected_formatted} " \
    "to be observed #{"with #{expected} " unless expected_value.nil?}" \
    "#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
    "but #{actual_changes_message}"
end

#failure_message_when_negatedObject



60
61
62
63
64
65
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 60

def failure_message_when_negated
  "expected #{expected_formatted} " \
    "not to be observed " \
    "#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
    "but #{actual_changes_message}"
end

#match(metric, block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 30

def match(metric, block)
  block.call

  observations = filter_matching_changes(Yabeda::TestAdapter.instance.summaries.fetch(metric))

  observations.values.any? { |observation| expected_value.nil? || values_match?(expected_value, observation) }
end

#match_when_negated(metric, block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 38

def match_when_negated(metric, block)
  unless expected_value.nil?
    raise NotImplementedError, <<~MSG
      `expect {}.not_to observe_yabeda_summary` doesn't support specifying values with `.with`
      as it can lead to false positives.
    MSG
  end

  block.call

  observations = filter_matching_changes(Yabeda::TestAdapter.instance.summaries.fetch(metric))

  observations.none?
end

#with(value) ⇒ Object



16
17
18
19
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 16

def with(value)
  @expected_value = value
  self
end