-
-
Notifications
You must be signed in to change notification settings - Fork 753
Allow adding a subclass of an already added formatter #2019
Allow adding a subclass of an already added formatter #2019
Conversation
…matter superclass
it "adds a subclass of that formatter for the same output target" do | ||
expect { | ||
loader.add Custom::AMoreSpecificFormatter, output | ||
}.to change { loader.formatters.length } |
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.
I think I'd prefer this if the classes were declared as local variables / lets inline rather than using ones prepared in another file, especially as for this spec it's completely unnecessary to have an implementation.
e.g.
a_general_formatter = Struct.new(:output)
a_more_specific_formatter = Class.new(a_general_formatter)
before { loader.add a_general_formatter, output }
it "adds a subclass of that formatter for the same output target" do
expect {
loader.add a_more_specific_formatter, output
}.to change { loader.formatters.length }
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.
I've tried that, but it doesn't work. None of the formatters are added.
and if you do instead
generic_formatter = BaseFormatter
specific_formatter = Class.new(generic_formatter)
Only the generic formatter is added (looks like classes don't get added unless they've registered).
The following works though (and doesn't require the custom formatter classes)
specific_formatter = RSpec::Core::Formatters::JsonFormatter
generic_formatter = specific_formatter.superclass
I'll add those changes.
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.
Ah yep, you do need to register otherwise you're considered a legacy formatter, still the new way is better, thanks!
Good catch @argos83, I'd like to refactor the specs a little before this is merged but it's looking good otherwise, thanks. |
Allow adding a subclass of an already added formatter
Allow adding a subclass of an already added formatter
Picked to |
…class Allow adding a subclass of an already added formatter
[skip ci]
…ding_formatter_subclass Allow adding a subclass of an already added formatter --- This commit was imported from rspec/rspec-core@37728c7.
[skip ci] --- This commit was imported from rspec/rspec-core@ba0955f.
duplicate_formatter_exists?
informatters.rb
prevents the user from adding the same formatter for the same output twice by checking the formatter instance's class. However the check is done in a way that it doesn't only avoid the same class to be added but also subclasses.If a user implements a more specific formatter. E.g. a
SpecializedJsonFormatter
which extends fromRSpec::Core::Formatters::JsonFormatter
and adds some extra functionality, and he/she wants to register both then, depending on the registration order, the SpecializedJsonFormatter may not be added.For instance:
Will work for:
But not for:
I believe both cases used to work with RSpec 3.2.0