Class: Yabeda::RSpec::BaseMatcher
- Inherits:
-
RSpec::Matchers::BuiltIn::BaseMatcher
- Object
- RSpec::Matchers::BuiltIn::BaseMatcher
- Yabeda::RSpec::BaseMatcher
- 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)
Direct Known Subclasses
IncrementYabedaCounter, MeasureYabedaHistogram, ObserveYabedaSummary, UpdateYabedaGauge
Instance Attribute Summary collapse
-
#expectations ⇒ Object
readonly
Returns the value of attribute expectations.
-
#metric ⇒ Object
readonly
Returns the value of attribute metric.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
-
#does_not_match?(actual) ⇒ Boolean
RSpec doesn’t define this method, but it is more convenient to rely on
match_when_negated
method presence. -
#expected_formatted ⇒ Object
Pretty print metric name (expected is expected to always be a Yabeda metric instance).
-
#initialize(metric) ⇒ BaseMatcher
constructor
A new instance of BaseMatcher.
- #supports_block_expectations? ⇒ Boolean
- #with(expectations) ⇒ Object
-
#with_tags(tags) ⇒ Object
Specify a scope of labels (tags).
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
#expectations ⇒ Object (readonly)
Returns the value of attribute expectations.
11 12 13 |
# File 'lib/yabeda/rspec/base_matcher.rb', line 11 def expectations @expectations end |
#metric ⇒ Object (readonly)
Returns the value of attribute metric.
11 12 13 |
# File 'lib/yabeda/rspec/base_matcher.rb', line 11 def metric @metric end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
11 12 13 |
# File 'lib/yabeda/rspec/base_matcher.rb', line 11 def @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
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_formatted ⇒ Object
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
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.
14 15 16 17 18 19 20 |
# File 'lib/yabeda/rspec/base_matcher.rb', line 14 def () raise ArgumentError, "Can't use `with_tags` with expectations hash provided" if !@tags && @expectations&.any? @tags = @expectations = { => nil } self end |