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

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


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

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.



23
24
25
# File 'lib/yabeda/rspec/update_yabeda_gauge.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/update_yabeda_gauge.rb', line 81

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



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

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}" \
    "#{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/update_yabeda_gauge.rb', line 71

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

def match(metric, block)
  block.call

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

  return false if updates.empty?

  updates.values.all? do |expected_update, actual_update|
    next !actual_update.nil? if expected_update.nil?

    expected_update.nil? || values_match?(expected_update, actual_update)
  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/update_yabeda_gauge.rb', line 46

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

#with(value) ⇒ Object



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

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

  @expected_value = value
  self
end