Class: Lumberjack::CaptureDevice::IncludeLogEntryMatcher
- Inherits:
-
Object
- Object
- Lumberjack::CaptureDevice::IncludeLogEntryMatcher
- Defined in:
- lib/lumberjack/capture_device/include_log_entry_matcher.rb
Overview
RSpec matcher for checking captured logs for specific entries.
Instance Method Summary collapse
-
#description ⇒ String
Provide a description of what this matcher checks.
-
#entry_diff(entry, indent: 2) ⇒ String
Generate a diff between a log entry and the expected log entry values.
-
#failure_message ⇒ String
Generate a failure message when the matcher fails.
-
#failure_message_when_negated ⇒ String
Generate a failure message when the negated matcher fails.
-
#initialize(expected_hash) ⇒ IncludeLogEntryMatcher
constructor
Initialize the matcher with expected log entry attributes.
-
#matches?(actual) ⇒ Boolean
Check if the logger contains a log entry matching the expected attributes.
Constructor Details
#initialize(expected_hash) ⇒ IncludeLogEntryMatcher
Initialize the matcher with expected log entry attributes.
13 14 15 16 17 |
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 13 def initialize(expected_hash) # The keys are symbolized so the hash can be passed to any Lumberjack::Device::Test device. @expected_hash = expected_hash.transform_keys(&:to_sym) @logger = nil end |
Instance Method Details
#description ⇒ String
Provide a description of what this matcher checks.
56 57 58 |
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 56 def description "have logged entry with #{expectation_description(@expected_hash)}" end |
#entry_diff(entry, indent: 2) ⇒ String
Generate a diff between a log entry and the expected log entry values. The severity, message, progname, and attributes of the entry are listed. Values that don't match the expectation are shown on a pair of lines with the expected value prefixed with "-" and the value from the log entry prefixed with "+". This is intended to make it easy to spot exactly which fields kept an entry from matching.
The comparison is done by Lumberjack::LogEntryMatcher#diff so the diff always agrees with the result of the match.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 72 def entry_diff(entry, indent: 2) indent_str = " " * indent entry_differences(entry).collect do |name, expected, actual, matched| if matched "#{indent_str} #{name}: #{actual}" else "#{indent_str}- #{name}: #{expected}#{Lumberjack::LINE_SEPARATOR}#{indent_str}+ #{name}: #{actual}" end end.join(Lumberjack::LINE_SEPARATOR) end |
#failure_message ⇒ String
Generate a failure message when the matcher fails.
34 35 36 37 38 39 40 |
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 34 def if valid_logger? (@expected_hash) else (@logger) end end |
#failure_message_when_negated ⇒ String
Generate a failure message when the negated matcher fails.
45 46 47 48 49 50 51 |
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 45 def if valid_logger? (@expected_hash) else (@logger) end end |
#matches?(actual) ⇒ Boolean
Check if the logger contains a log entry matching the expected attributes.
24 25 26 27 28 29 |
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 24 def matches?(actual) @logger = actual return false unless valid_logger? device.include?(@expected_hash) end |