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

Commit e570a0c

Browse files
authored
Merge pull request #2649 from ioquatix/patch-1
Indent messages correctly in DocumentationFormatter.
2 parents 0fcf18c + 0022831 commit e570a0c

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

lib/rspec/core/formatters/documentation_formatter.rb

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ module Core
66
module Formatters
77
# @private
88
class DocumentationFormatter < BaseTextFormatter
9-
Formatters.register self, :example_group_started, :example_group_finished,
9+
Formatters.register self, :example_started, :example_group_started, :example_group_finished,
1010
:example_passed, :example_pending, :example_failed
1111

1212
def initialize(output)
1313
super
1414
@group_level = 0
15+
16+
@example_running = false
17+
@messages = []
18+
end
19+
20+
def example_started(_notification)
21+
@example_running = true
1522
end
1623

1724
def example_group_started(notification)
@@ -27,19 +34,44 @@ def example_group_finished(_notification)
2734

2835
def example_passed(passed)
2936
output.puts passed_output(passed.example)
37+
38+
flush_messages
39+
@example_running = false
3040
end
3141

3242
def example_pending(pending)
3343
output.puts pending_output(pending.example,
3444
pending.example.execution_result.pending_message)
45+
46+
flush_messages
47+
@example_running = false
3548
end
3649

3750
def example_failed(failure)
3851
output.puts failure_output(failure.example)
52+
53+
flush_messages
54+
@example_running = false
55+
end
56+
57+
def message(notification)
58+
if @example_running
59+
@messages << notification.message
60+
else
61+
output.puts "#{current_indentation}#{notification.message}"
62+
end
3963
end
4064

4165
private
4266

67+
def flush_messages
68+
@messages.each do |message|
69+
output.puts "#{current_indentation(1)}#{message}"
70+
end
71+
72+
@messages.clear
73+
end
74+
4375
def passed_output(example)
4476
ConsoleCodes.wrap("#{current_indentation}#{example.description.strip}", :success)
4577
end
@@ -61,8 +93,8 @@ def next_failure_index
6193
@next_failure_index += 1
6294
end
6395

64-
def current_indentation
65-
' ' * @group_level
96+
def current_indentation(offset=0)
97+
' ' * (@group_level + offset)
6698
end
6799
end
68100
end

spec/rspec/core/formatters/documentation_formatter_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ def execution_result(values)
8181
")
8282
end
8383

84+
it "can output indented messages from within example group" do
85+
root = RSpec.describe("root")
86+
root.example("example") {|example| example.reporter.message("message")}
87+
88+
root.run(reporter)
89+
90+
expect(formatter_output.string).to eql("
91+
root
92+
example
93+
message
94+
")
95+
end
96+
97+
it "can output indented messages" do
98+
root = RSpec.describe("root")
99+
context = root.describe("nested")
100+
context.example("example") {}
101+
102+
root.run(reporter)
103+
104+
reporter.message("message")
105+
106+
expect(formatter_output.string).to eql("
107+
root
108+
nested
109+
example
110+
message
111+
")
112+
end
113+
84114
it "strips whitespace for each row" do
85115
group = RSpec.describe(" root ")
86116
context1 = group.describe(" nested ")

0 commit comments

Comments
 (0)