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

#expectations, #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)


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

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.



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

def expected_value
  @expected_value
end

Instance Method Details

#actual_changes_messageObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 81

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



61
62
63
64
65
66
67
68
69
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 61

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

#failure_message_when_negatedObject



71
72
73
74
75
76
77
78
79
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 71

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

#match(metric, block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 32

def match(metric, block)
  block.call

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

  return false if observations.empty?

  observations.values.all? do |expected_observation, actual_observation|
    next !actual_observation.nil? if expected_observation.nil?

    values_match?(expected_observation, actual_observation)
  end
end

#match_when_negated(metric, block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yabeda/rspec/observe_yabeda_summary.rb', line 46

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? { |_tags, (_expected, actual)| !actual.nil? }
end

#with(value) ⇒ Object



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

def with(value)
  return super if value.is_a?(Hash)

  @expected_value = value
  self
end