Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Indent messages correctly in DocumentationFormatter. #2649

Merged
merged 10 commits into from
Sep 10, 2019
38 changes: 35 additions & 3 deletions lib/rspec/core/formatters/documentation_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ module Core
module Formatters
# @private
class DocumentationFormatter < BaseTextFormatter
Formatters.register self, :example_group_started, :example_group_finished,
Formatters.register self, :example_started, :example_group_started, :example_group_finished,
:example_passed, :example_pending, :example_failed

def initialize(output)
super
@group_level = 0

@example_running = false
@messages = []
end

def example_started(_notification)
@example_running = true
end

def example_group_started(notification)
Expand All @@ -27,19 +34,44 @@ def example_group_finished(_notification)

def example_passed(passed)
output.puts passed_output(passed.example)

flush_messages
@example_running = false
end

def example_pending(pending)
output.puts pending_output(pending.example,
pending.example.execution_result.pending_message)

flush_messages
@example_running = false
end

def example_failed(failure)
output.puts failure_output(failure.example)

flush_messages
@example_running = false
end

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

private

def flush_messages
@messages.each do |message|
output.puts "#{current_indentation(1)}#{message}"
end

@messages.clear
end

def passed_output(example)
ConsoleCodes.wrap("#{current_indentation}#{example.description.strip}", :success)
end
Expand All @@ -61,8 +93,8 @@ def next_failure_index
@next_failure_index += 1
end

def current_indentation
' ' * @group_level
def current_indentation(offset=0)
' ' * (@group_level + offset)
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/rspec/core/formatters/documentation_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ def execution_result(values)
")
end

it "can output indented messages from within example group" do
root = RSpec.describe("root")
root.example("example") {|example| example.reporter.message("message")}

root.run(reporter)

expect(formatter_output.string).to eql("
root
example
message
")
end

it "can output indented messages" do
root = RSpec.describe("root")
context = root.describe("nested")
context.example("example") {}

root.run(reporter)

reporter.message("message")

expect(formatter_output.string).to eql("
root
nested
example
message
")
end

it "strips whitespace for each row" do
group = RSpec.describe(" root ")
context1 = group.describe(" nested ")
Expand Down