Class: Yabeda::RSpec::BaseMatcher

Inherits:
RSpec::Matchers::BuiltIn::BaseMatcher
  • Object
show all
Defined in:
lib/yabeda/rspec/base_matcher.rb

Overview

Notes:

+expected+ is always a metric instance
+actual+ is always a block of code

Example:

expect { anything }.to do_whatever_with_yabeda_metric(Yabeda.something)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric) ⇒ BaseMatcher

Returns a new instance of BaseMatcher.



27
28
29
30
31
32
33
34
35
36
# File 'lib/yabeda/rspec/base_matcher.rb', line 27

def initialize(metric)
  super
  @expected = @metric = resolve_metric(metric)
  @expectations = {}
rescue KeyError
  raise ArgumentError, <<~MSG
    Pass metric name or metric instance to matcher (e.g. `increment_yabeda_counter(Yabeda.metric_name)` or \
    increment_yabeda_counter('metric_name')). Got #{metric.inspect} instead
  MSG
end

Instance Attribute Details

#expectationsObject (readonly)

Returns the value of attribute expectations.



11
12
13
# File 'lib/yabeda/rspec/base_matcher.rb', line 11

def expectations
  @expectations
end

#metricObject (readonly)

Returns the value of attribute metric.



11
12
13
# File 'lib/yabeda/rspec/base_matcher.rb', line 11

def metric
  @metric
end

#tagsObject (readonly)

Returns the value of attribute tags.



11
12
13
# File 'lib/yabeda/rspec/base_matcher.rb', line 11

def tags
  @tags
end

Instance Method Details

#does_not_match?(actual) ⇒ Boolean

RSpec doesn’t define this method, but it is more convenient to rely on match_when_negated method presence

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/yabeda/rspec/base_matcher.rb', line 39

def does_not_match?(actual)
  @actual = actual
  if respond_to?(:match_when_negated)
    match_when_negated(expected, actual)
  else
    !match(expected, actual)
  end
end

#expected_formattedObject

Pretty print metric name (expected is expected to always be a Yabeda metric instance)



53
54
55
# File 'lib/yabeda/rspec/base_matcher.rb', line 53

def expected_formatted
  "Yabeda.#{[metric.group, metric.name].compact.join('.')}"
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/yabeda/rspec/base_matcher.rb', line 48

def supports_block_expectations?
  true
end

#with(expectations) ⇒ Object



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

def with(expectations)
  @expectations = expectations || {}
  self
end

#with_tags(tags) ⇒ Object

Specify a scope of labels (tags). Subset of tags can be specified.

Raises:

  • (ArgumentError)


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

def with_tags(tags)
  raise ArgumentError, "Can't use `with_tags` with expectations hash provided" if !@tags && @expectations&.any?

  @tags = tags
  @expectations = { tags => nil }
  self
end