Class: Yabeda::RSpec::MeasureYabedaHistogram

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

Overview

Custom matcher class with implementation for measure_yabeda_histogram

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

#initializeMeasureYabedaHistogram

Returns a new instance of MeasureYabedaHistogram.

Raises:

  • (ArgumentError)


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

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

  raise ArgumentError, "Pass histogram instance/name to `measure_yabeda_histogram`. 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/measure_yabeda_histogram.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
# File 'lib/yabeda/rspec/measure_yabeda_histogram.rb', line 81

def actual_changes_message
  measures = Yabeda::TestAdapter.instance.histograms.fetch(metric)
  if measures.empty?
    "no changes of this gauge have been made"
  elsif tags && measures.key?(tags)
    "has been changed to #{measures.fetch(tags)} with tags #{::RSpec::Support::ObjectFormatter.format(tags)}"
  else
    "following changes have been made: #{::RSpec::Support::ObjectFormatter.format(measures)}"
  end
end

#failure_messageObject



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

def failure_message
  "expected #{expected_formatted} " \
    "to be changed #{"to #{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/measure_yabeda_histogram.rb', line 71

def failure_message_when_negated
  "expected #{expected_formatted} " \
    "not to be incremented " \
    "#{"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/measure_yabeda_histogram.rb', line 32

def match(metric, block)
  block.call

  measures = filter_matching_changes(Yabeda::TestAdapter.instance.histograms.fetch(metric))

  return false if measures.empty?

  measures.values.all? do |expected_measure, actual_measure|
    next !actual_measure.nil? if expected_measure.nil?

    values_match?(expected_measure, actual_measure)
  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/measure_yabeda_histogram.rb', line 46

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

  block.call

  measures = filter_matching_changes(Yabeda::TestAdapter.instance.histograms.fetch(metric))

  measures.none? { |_tags, (_expected, actual)| !actual.nil? }
end

#with(value) ⇒ Object



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

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

  @expected_value = value
  self
end