-
-
Notifications
You must be signed in to change notification settings - Fork 753
Add fallback formatter for #message #1980
Changes from all commits
b0c9931
a8749e9
fe7cb29
5e9db38
60b2dc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module RSpec | ||
module Core | ||
module Formatters | ||
# @api private | ||
# Formatter for providing message output as a fallback when no other | ||
# profiler implements #message | ||
class FallbackMessageFormatter | ||
Formatters.register self, :message | ||
|
||
def initialize(output) | ||
@output = output | ||
end | ||
|
||
# @private | ||
attr_reader :output | ||
|
||
# @api public | ||
# | ||
# Used by the reporter to send messages to the output stream. | ||
# | ||
# @param notification [MessageNotification] containing message | ||
def message(notification) | ||
output.puts notification.message | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'rspec/core/reporter' | ||
require 'rspec/core/formatters/fallback_message_formatter' | ||
|
||
module RSpec::Core::Formatters | ||
RSpec.describe FallbackMessageFormatter do | ||
include FormatterSupport | ||
|
||
describe "#message" do | ||
it 'writes the message to the output' do | ||
expect { | ||
send_notification :message, message_notification('Custom Message') | ||
}.to change { formatter_output.string }. | ||
from(excluding 'Custom Message'). | ||
to(including 'Custom Message') | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,28 +130,55 @@ module RSpec::Core::Formatters | |
end | ||
end | ||
|
||
describe "#setup_default", "with profiling enabled" do | ||
describe "#setup_default" do | ||
let(:setup_default) { loader.setup_default output, output } | ||
|
||
before do | ||
allow(RSpec.configuration).to receive(:profile_examples?) { true } | ||
context "with a formatter that implements #message" do | ||
it 'doesnt add a fallback formatter' do | ||
allow(reporter).to receive(:registered_listeners).with(:message) { [:json] } | ||
setup_default | ||
expect(loader.formatters).to exclude( | ||
an_instance_of ::RSpec::Core::Formatters::FallbackMessageFormatter | ||
) | ||
end | ||
end | ||
|
||
context "without an existing profile formatter" do | ||
it "will add the profile formatter" do | ||
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [] } | ||
setup_default | ||
expect(loader.formatters.last).to be_a ::RSpec::Core::Formatters::ProfileFormatter | ||
context "without a formatter that implements #message" do | ||
it 'adds a fallback for message output' do | ||
allow(reporter).to receive(:registered_listeners).with(:message) { [] } | ||
expect { | ||
setup_default | ||
}.to change { loader.formatters }. | ||
from( excluding an_instance_of ::RSpec::Core::Formatters::FallbackMessageFormatter ). | ||
to( including an_instance_of ::RSpec::Core::Formatters::FallbackMessageFormatter ) | ||
end | ||
end | ||
|
||
context "when a formatter that implement #dump_profile is added" do | ||
it "wont add the profile formatter" do | ||
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [:json] } | ||
setup_default | ||
expect( | ||
loader.formatters.map(&:class) | ||
).to_not include ::RSpec::Core::Formatters::ProfileFormatter | ||
context "with profiling enabled" do | ||
before do | ||
allow(reporter).to receive(:registered_listeners).with(:message) { [:json] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems brittle to stub Or do you see an advantage to stubbing the reporter here over just registering a listener? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its not a real reporter, but an instance_double. |
||
allow(RSpec.configuration).to receive(:profile_examples?) { true } | ||
end | ||
|
||
context "without an existing profile formatter" do | ||
it "will add the profile formatter" do | ||
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [] } | ||
expect { | ||
setup_default | ||
}.to change { loader.formatters }. | ||
from( excluding an_instance_of ::RSpec::Core::Formatters::ProfileFormatter ). | ||
to( including an_instance_of ::RSpec::Core::Formatters::ProfileFormatter ) | ||
end | ||
end | ||
|
||
context "when a formatter that implement #dump_profile is added" do | ||
it "wont add the profile formatter" do | ||
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [:json] } | ||
setup_default | ||
expect( | ||
loader.formatters.map(&:class) | ||
).to_not include ::RSpec::Core::Formatters::ProfileFormatter | ||
end | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to stub this here? Isn't the reporter instance here a bare reporter instance that doesn't have any listeners registered?