Class: RSpec::EnrichedJson::Formatters::EnrichedJsonFormatter

Inherits:
Core::Formatters::JsonFormatter
  • Object
show all
Defined in:
lib/rspec/enriched_json/formatters/enriched_json_formatter.rb

Constant Summary collapse

ANSI_COLOR_REGEX =
/\e\[[0-9;]*[mGKHF]/
EXCEPTION_DETECTOR_REGEX =
/(Exception|Error|undefined method|uninitialized constant)/
PATH_AND_LINE_NUMBER_REGEX =
/#?(?<path>.+?):(?<line_number>\d+)/
EXCEPTION_CLASS_AND_MESSAGE_REGEX =
/^(?<exception_class>[A-Z]\w*Error|Exception):$\n(?<exception_message>(?<extra>^\s\s.*\n?)+)/

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ EnrichedJsonFormatter



17
18
19
20
21
22
# File 'lib/rspec/enriched_json/formatters/enriched_json_formatter.rb', line 17

def initialize(output)
  super
  @output_hash = {
    errors: []
  }
end

Instance Method Details

#message(notification) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rspec/enriched_json/formatters/enriched_json_formatter.rb', line 51

def message(notification)
  super

  if notification.message.match?(EXCEPTION_DETECTOR_REGEX)
    clean_message = notification.message.gsub(ANSI_COLOR_REGEX, "")

    error_info = {
      message: clean_message
    }

    if (match = clean_message.match(PATH_AND_LINE_NUMBER_REGEX))
      error_info[:path] = match.named_captures["path"]
      error_info[:line_number] = match.named_captures["line_number"]
    end

    if (match = clean_message.match(EXCEPTION_CLASS_AND_MESSAGE_REGEX))
      error_info[:exception_class] = match.named_captures["exception_class"]
      error_info[:exception_message] = match.named_captures["exception_message"]
    end

    @output_hash[:errors] << error_info
  end
end

#stop(group_notification) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rspec/enriched_json/formatters/enriched_json_formatter.rb', line 24

def stop(group_notification)
  @output_hash[:examples] = group_notification.notifications.map do |notification|
    format_example(notification.example).tap do |hash|
      (hash, notification.example)
      e = notification.example.exception

      if e
        hash[:exception] = {
          class: e.class.name,
          message: e.message,
          backtrace: notification.formatted_backtrace
        }

        if e.is_a?(RSpec::EnrichedJson::EnrichedExpectationNotMetError) && e.details
          hash[:details] = e.details
        end
      else
        key = notification.example.id
        if RSpec::EnrichedJson.all_test_values.key?(key)
          captured_values = RSpec::EnrichedJson.all_test_values[key]
          hash[:details] = captured_values
        end
      end
    end
  end
end