Permalink
Browse files

support custom formatter as --format option

  • Loading branch information...
dchelimsky committed Jun 10, 2010
1 parent e46bbd5 commit 22553eba77524015f8a17ebffcb074967f8e75c8
@@ -151,13 +151,17 @@ def full_description=(description)
end
def formatter=(formatter_to_use)
- formatter_class = case formatter_to_use.to_s
- when 'd', 'doc', 'documentation', 's', 'n', 'spec', 'nested'
- RSpec::Core::Formatters::DocumentationFormatter
- when 'progress'
- RSpec::Core::Formatters::ProgressFormatter
- else
- raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?."
+ if formatter_to_use.is_a?(Class)
+ formatter_class = formatter_to_use
+ else
+ formatter_class = case formatter_to_use.to_s
+ when 'd', 'doc', 'documentation', 's', 'n', 'spec', 'nested'
+ RSpec::Core::Formatters::DocumentationFormatter
+ when 'progress'
+ RSpec::Core::Formatters::ProgressFormatter
+ else
+ raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?."
+ end
end
self.formatter_class = formatter_class
end
@@ -43,7 +43,8 @@ def parser(options)
parser.on('-f', '--format FORMATTER', 'Choose a formatter',
' [p]rogress (default - dots)',
- ' [d]ocumentation (group and example names)') do |o|
+ ' [d]ocumentation (group and example names)',
+ ' custom formatter class name') do |o|
options[:formatter] = o
end
@@ -35,12 +35,16 @@ def options_from_args(*args)
end
end
- describe 'formatter' do
+ describe 'format' do
example '-f or --format with an argument should parse' do
options_from_args('--format', 'd').should include(:formatter => 'd')
options_from_args('-f', 'd').should include(:formatter => 'd')
options_from_args('-fd').should include(:formatter => 'd')
end
+
+ example "-f/--format can accept a class name" do
+ options_from_args('-fSome::Formatter::Class').should include(:formatter => 'Some::Formatter::Class')
+ end
end
describe 'profile_examples' do
@@ -190,6 +190,12 @@ def that_thing
config.formatter = 'documentation'
config.formatter.should be_an_instance_of(Formatters::DocumentationFormatter)
end
+
+ it "sets a formatter based on its class" do
+ formatter_class = Class.new(Formatters::BaseTextFormatter)
+ config.formatter = formatter_class
+ config.formatter.should be_an_instance_of(formatter_class)
+ end
it "raises ArgumentError if formatter is unknown" do
lambda { config.formatter = :progresss }.should raise_error(ArgumentError)

4 comments on commit 22553eb

Contributor

thibaudgg replied Jun 12, 2010

Great, now we need --require option to use our own formatter_classes right? :-)

Owner

dchelimsky replied Jun 13, 2010

Thought I added that already :) Coming soon.

Contributor

thibaudgg replied Jun 13, 2010

thx!

Please sign in to comment.