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

Commit d2b9a06

Browse files
author
Sebastián Tello
committed
Allow adding a formatter class that extends from an already added formatter superclass
1 parent 9f99b6a commit d2b9a06

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

lib/rspec/core/formatters.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def register(formatter, notifications)
182182

183183
def duplicate_formatter_exists?(new_formatter)
184184
@formatters.any? do |formatter|
185-
formatter.class === new_formatter && formatter.output == new_formatter.output
185+
formatter.class == new_formatter.class && formatter.output == new_formatter.output
186186
end
187187
end
188188

spec/rspec/core/formatters_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'pathname'
2+
require 'rspec/core/resources/a_custom_formatter'
23

34
module RSpec::Core::Formatters
45
RSpec.describe Loader do
@@ -137,6 +138,16 @@ module RSpec::Core::Formatters
137138
}.to change { loader.formatters.length }
138139
end
139140
end
141+
142+
context "When a custom formatter exists" do
143+
before { loader.add Custom::AGeneralFormatter, output }
144+
145+
it "adds a subclass of that formatter for the same output target" do
146+
expect {
147+
loader.add Custom::AMoreSpecificFormatter, output
148+
}.to change { loader.formatters.length }
149+
end
150+
end
140151
end
141152

142153
describe "#setup_default" do
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Custom
2+
class AGeneralFormatter < RSpec::Core::Formatters::BaseFormatter
3+
RSpec::Core::Formatters.register self, :message
4+
attr_reader :call_times
5+
6+
def message(_notification)
7+
puts message_builder
8+
end
9+
10+
def message_builder
11+
'Important Message'
12+
end
13+
end
14+
15+
class AMoreSpecificFormatter < AGeneralFormatter
16+
RSpec::Core::Formatters.register self, :message
17+
18+
def message_builder
19+
'Very ' + super
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)