Skip to content

Commit

Permalink
support custom formatter as --format option
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Jun 10, 2010
1 parent e46bbd5 commit 22553eb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
18 changes: 11 additions & 7 deletions lib/rspec/core/configuration.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec/core/option_parser.rb
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion spec/rspec/core/configuration_options_spec.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -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)
Expand Down

4 comments on commit 22553eb

@thibaudgg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@dchelimsky
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought I added that already :) Coming soon.

@thibaudgg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx!

@dchelimsky
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.