Class: Yabeda::RSpec::IncrementYabedaCounter

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

Overview

Custom matcher class with implementation for increment_yabeda_counter

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, #with_tags

Constructor Details

#initializeIncrementYabedaCounter

Returns a new instance of IncrementYabedaCounter.

Raises:

  • (ArgumentError)


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

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

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

Instance Attribute Details

#expected_incrementObject (readonly)

Returns the value of attribute expected_increment.



22
23
24
# File 'lib/yabeda/rspec/increment_yabeda_counter.rb', line 22

def expected_increment
  @expected_increment
end

Instance Method Details

#actual_increments_messageObject



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

def actual_increments_message
  counter_increments = Yabeda::TestAdapter.instance.counters.fetch(metric)
  if counter_increments.empty?
    "no increments of this counter have been made"
  elsif tags && counter_increments.key?(tags)
    "has been incremented by #{counter_increments.fetch(tags)}"
  else
    "following increments have been made: #{::RSpec::Support::ObjectFormatter.format(counter_increments)}"
  end
end

#by(increment) ⇒ Object



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

def by(increment)
  @expected_increment = increment
  @expectations = { tags || {} => increment }
  self
end

#failure_messageObject



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

def failure_message
  "expected #{expected_formatted} " \
    "to be incremented #{"by #{description_of(expected_increment)} " unless expected_increment.nil?}" \
    "#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
    "#{if !tags && expectations
         "with following expectations: #{::RSpec::Support::ObjectFormatter.format(expectations)} "
       end}" \
    "but #{actual_increments_message}"
end

#failure_message_when_negatedObject



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

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_increments_message}"
end

#match(metric, block) ⇒ Object



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

def match(metric, block)
  block.call

  increments = filter_matching_changes(Yabeda::TestAdapter.instance.counters.fetch(metric))

  return false if increments.empty?

  increments.values.all? do |expected_increment, actual_increment|
    next !actual_increment.nil? if expected_increment.nil?

    values_match?(expected_increment, actual_increment)
  end
end

#match_when_negated(metric, block) ⇒ Object



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

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

  block.call

  increments = filter_matching_changes(Yabeda::TestAdapter.instance.counters.fetch(metric))

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