Skip to content

Commit

Permalink
Merge pull request #158 from nikitug/multi_formatter
Browse files Browse the repository at this point in the history
Multi Formatter
  • Loading branch information
colszowka committed Sep 11, 2012
2 parents 4e2fdba + 6decd51 commit 2fd9a4c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,12 @@ being an instance of SimpleCov::Result. Do whatever your wish with that!

## Using multiple formatters

There is currently no built-in support for this, but you could help yourself with a wrapper class:
Configure the formatter to use built-in MultiFormatter:

class SimpleCov::Formatter::MergedFormatter
def format(result)
SimpleCov::Formatter::HTMLFormatter.new.format(result)
SimpleCov::Formatter::CSVFormatter.new.format(result)
end
end

Then configure the formatter to use the new merger:

SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::CSVFormatter,
]

## Available formatters

Expand Down
52 changes: 52 additions & 0 deletions features/config_formatters.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@test_unit @config
Feature:

The formatter for test coverage can be customized
with the SimpleCov.formatter setting. There are two
built-in formatters:
SimpleCov::Formatter::SimpleFormatter is a simple
formatter returning a string of all files with
theirs coverages.
SimpleCov::Formatter::MultiFormatter is a formatter
used to call multiple formatters at once.

Scenario: With SimpleFormatter
Given SimpleCov for Test/Unit is configured with:
"""
require 'simplecov'
SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter
SimpleCov.at_exit do
puts SimpleCov.result.format!
end
SimpleCov.start do
add_group 'Libs', 'lib/faked_project/'
end
"""

When I successfully run `bundle exec rake test`
Then the output should contain "lib/faked_project/meta_magic.rb (coverage: 100.0%)"

Scenario: With MultiFormatter
Given SimpleCov for Test/Unit is configured with:
"""
require 'simplecov'
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::SimpleFormatter,
Class.new do
def format(result)
raise "Unable to format"
end
end
]
SimpleCov.at_exit do
puts SimpleCov.result.format!.join
end
SimpleCov.start do
add_group 'Libs', 'lib/faked_project/'
end
"""

When I successfully run `bundle exec rake test`
Then the output should contain "lib/faked_project/meta_magic.rb (coverage: 100.0%)"
And the output should match /Formatter [^\s]* failed with RuntimeError: Unable to format/
1 change: 1 addition & 0 deletions lib/simplecov/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ module Formatter
end

require 'simplecov/formatter/simple_formatter'
require 'simplecov/formatter/multi_formatter'
25 changes: 25 additions & 0 deletions lib/simplecov/formatter/multi_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class SimpleCov::Formatter::MultiFormatter
def self.[](*args)
Class.new(self) do
define_method :formatters do
@formatters ||= args
end
end
end

def format(result)
formatters.map do |formatter|
begin
formatter.new.format(result)
rescue => e
STDERR.puts("Formatter #{formatter} failed with #{e.class}: #{e.message} (#{e.backtrace.first})")
nil
end
end
end

def formatters
@formatters ||= []
end

end
15 changes: 15 additions & 0 deletions test/test_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ class TestResult < Test::Unit::TestCase
assert_equal String, @result.format!.class
end
end

context "and multi formatter being used" do
setup do
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::SimpleFormatter,
SimpleCov::Formatter::SimpleFormatter,
]
end

should "return an array containing formatted string with result.format!" do
formated = @result.format!
assert_equal 2, formated.count
assert_equal String, formated.first.class
end
end
end

context "with groups set up that do not match all files" do
Expand Down

0 comments on commit 2fd9a4c

Please sign in to comment.