Class: RSpec::Loop::Formatter

Inherits:
Core::Formatters::BaseTextFormatter
  • Object
show all
Defined in:
lib/rspec/loop/formatter.rb

Constant Summary collapse

NOTIFICATIONS =
i[
  example_group_started
  example_started
  example_iteration_finished
  example_finished
  example_group_finished
  dump_pending
  dump_failures
  dump_summary
].freeze
RESULT_CHAR_MAP =
{
  passed: ".",
  pending: "*",
  failed: "F",
}.freeze
RESULT_COLOR_CODE_MAP =
{
  passed: :success,
  pending: :pending,
  failed: :failure,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Formatter

Returns a new instance of Formatter.



34
35
36
37
38
39
40
41
42
43
# File 'lib/rspec/loop/formatter.rb', line 34

def initialize(output)
  super
  @group_level = 0

  @example_running = false
  @messages = []
  @totals = Hash.new do |h, key|
    h[key] = { failed: 0, passed: 0, pending: 0 }
  end
end

Instance Method Details

#dump_failures(notification) ⇒ Object



106
107
108
109
110
# File 'lib/rspec/loop/formatter.rb', line 106

def dump_failures(notification)
  return if notification.failure_notifications.empty?

  output.puts notification.fully_formatted_failed_examples
end

#dump_pending(notification) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rspec/loop/formatter.rb', line 93

def dump_pending(notification)
  return if RSpec.configuration.respond_to?(:pending_failure_output) &&
            RSpec.configuration.pending_failure_output == :skip
  return if notification.pending_notifications.empty?

  formatted = "\nPending: (Failures listed here are expected and do not affect your suite's status)\n".dup
  pending_examples = notification.pending_notifications.uniq(&:example)
  pending_examples.each_with_index do |pending, index|
    formatted << pending.fully_formatted(index.next)
  end
  output.puts formatted
end

#dump_summary(summary) ⇒ Object



112
113
114
# File 'lib/rspec/loop/formatter.rb', line 112

def dump_summary(summary)
  output.puts summary.fully_formatted
end

#example_finished(notification) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rspec/loop/formatter.rb', line 70

def example_finished(notification)
  @example_running = false

  if notification.example.skipped?
    notification.example.loop_count.times do
      print_char_for_status(:pending)
    end
  end

  code = RESULT_COLOR_CODE_MAP[notification.example.execution_result.status]
  output.puts "] #{RSpec::Core::Formatters::ConsoleCodes.wrap(notification.example.description.strip, code)}"

  flush_messages
end

#example_group_finished(_notification) ⇒ Object



52
53
54
# File 'lib/rspec/loop/formatter.rb', line 52

def example_group_finished(_notification)
  @group_level -= 1 if @group_level.positive?
end

#example_group_started(notification) ⇒ Object



45
46
47
48
49
50
# File 'lib/rspec/loop/formatter.rb', line 45

def example_group_started(notification)
  output.puts if @group_level.zero?
  output.puts "#{current_indentation}#{notification.group.description.strip}"

  @group_level += 1
end

#example_iteration_finished(notification) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/rspec/loop/formatter.rb', line 61

def example_iteration_finished(notification)
  status = notification.example.[:loop_last_result].status
  return if notification.example.skipped?

  @totals[notification.example.id][status] += 1

  print_char_for_status(status)
end

#example_started(_notification) ⇒ Object



56
57
58
59
# File 'lib/rspec/loop/formatter.rb', line 56

def example_started(_notification)
  @example_running = true
  output.print "#{current_indentation}["
end

#message(notification) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/rspec/loop/formatter.rb', line 85

def message(notification)
  if @example_running
    @messages << notification.message
  else
    output.puts "#{current_indentation}#{notification.message}"
  end
end