Class: Lumberjack::CaptureDevice::IncludeLogEntryMatcher

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(expected_hash) ⇒ IncludeLogEntryMatcher

Initialize the matcher with expected log entry attributes.

Parameters:

  • expected_hash (Hash)

    Expected log entry attributes to match against.



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

#descriptionString

Provide a description of what this matcher checks.

Returns:

  • (String)

    A human-readable description of the matcher.



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.

Parameters:

  • entry (Lumberjack::LogEntry)

    The log entry to compare to the expectation.

  • indent (Integer) (defaults to: 2)

    The number of spaces to indent each line.

Returns:

  • (String)

    A formatted diff of the entry and the expectation.



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_messageString

Generate a failure message when the matcher fails.

Returns:

  • (String)

    A formatted failure message.



34
35
36
37
38
39
40
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 34

def failure_message
  if valid_logger?
    formatted_failure_message(@expected_hash)
  else
    wrong_object_type_message(@logger)
  end
end

#failure_message_when_negatedString

Generate a failure message when the negated matcher fails.

Returns:

  • (String)

    A formatted failure message for negated expectations.



45
46
47
48
49
50
51
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 45

def failure_message_when_negated
  if valid_logger?
    formatted_negated_failure_message(@expected_hash)
  else
    wrong_object_type_message(@logger)
  end
end

#matches?(actual) ⇒ Boolean

Check if the logger contains a log entry matching the expected attributes.

Parameters:

  • actual (Lumberjack::Logger, Lumberjack::ForkedLogger)

    The logger to check. The logger must be using a Lumberjack::Device::Test device.

Returns:

  • (Boolean)

    True if a matching log entry is found.



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