Class: Yabeda::RSpec::UpdateYabedaGauge

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

Overview

Custom matcher class with implementation for update_yabeda_gauge

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

#initializeUpdateYabedaGauge

Returns a new instance of UpdateYabedaGauge.

Raises:

  • (ArgumentError)


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

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

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

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

#failure_messageObject



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

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

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

def match(metric, block)
  block.call

  updates = filter_matching_changes(Yabeda::TestAdapter.instance.gauges.fetch(metric))

  updates.values.any? { |update| expected_value.nil? || values_match?(expected_value, update) }
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/update_yabeda_gauge.rb', line 38

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

  block.call

  updates = filter_matching_changes(Yabeda::TestAdapter.instance.gauges.fetch(metric))

  updates.none?
end

#with(value) ⇒ Object



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

def with(value)
  @expected_value = value
  self
end