-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from nikitug/multi_formatter
Multi Formatter
- Loading branch information
Showing
5 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ module Formatter | |
end | ||
|
||
require 'simplecov/formatter/simple_formatter' | ||
require 'simplecov/formatter/multi_formatter' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters