Class: SemanticLogger::Test::RSpec::EventMatcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers::Composable
Defined in:
lib/semantic_logger/test/rspec.rb

Overview

Matches a single SemanticLogger::Log against a set of expectations.

An expected value may be:

* a Class    - the actual value must be a kind_of that class
* :nil       - the actual value must be nil
* any value  - compared with ==

Instance Method Summary collapse

Constructor Details

#initialize(message_includes: nil, payload_includes: nil, exception_includes: nil, **expected) ⇒ EventMatcher

Returns a new instance of EventMatcher.

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
# File 'lib/semantic_logger/test/rspec.rb', line 46

def initialize(message_includes: nil, payload_includes: nil, exception_includes: nil, **expected)
  unknown = expected.keys - EVENT_ATTRIBUTES
  raise ArgumentError, "Unknown log event attribute(s): #{unknown.join(', ')}" unless unknown.empty?

  @expected           = expected
  @message_includes   = message_includes
  @payload_includes   = payload_includes
  @exception_includes = exception_includes
end

Instance Method Details

#descriptionObject



72
73
74
# File 'lib/semantic_logger/test/rspec.rb', line 72

def description
  "be a semantic logger event matching #{full_expectations.inspect}"
end

#failure_messageObject



76
77
78
79
# File 'lib/semantic_logger/test/rspec.rb', line 76

def failure_message
  "expected log event to #{description}, but #{@failure}.\n" \
    "Log event: #{event_inspect}"
end

#failure_message_when_negatedObject



81
82
83
# File 'lib/semantic_logger/test/rspec.rb', line 81

def failure_message_when_negated
  "expected log event not to #{description}.\nLog event: #{event_inspect}"
end

#matches?(event) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/semantic_logger/test/rspec.rb', line 56

def matches?(event)
  @event = event
  return failed("no log event") unless event

  @expected.each_pair do |name, expected|
    actual = event.public_send(name)
    return failed(mismatch(name, expected, actual)) unless attribute_matches?(expected, actual)
  end

  return failed(includes_failure(:message, @message_includes)) unless message_includes_matches?
  return failed(includes_failure(:payload, @payload_includes)) unless payload_includes_matches?
  return failed(includes_failure(:exception, @exception_includes)) unless exception_includes_matches?

  true
end