Support multiple formatters at once

This commit is contained in:
Michael Miller 2019-03-23 17:00:21 -06:00
parent f1873238cd
commit ef8b773787
3 changed files with 25 additions and 13 deletions

View file

@ -1,8 +1,7 @@
module Spectator module Spectator
# Provides customization and describes specifics for how Spectator will run and report tests. # Provides customization and describes specifics for how Spectator will run and report tests.
class Config class Config
# Used to report test progress and results. @formatters : Array(Formatting::Formatter)
getter formatter : Formatting::Formatter
# Indicates whether the test should abort on first failure. # Indicates whether the test should abort on first failure.
getter? fail_fast : Bool getter? fail_fast : Bool
@ -16,10 +15,17 @@ module Spectator
# Creates a new configuration. # Creates a new configuration.
def initialize(builder) def initialize(builder)
@formatter = builder.formatter @formatters = builder.formatters
@fail_fast = builder.fail_fast? @fail_fast = builder.fail_fast?
@fail_blank = builder.fail_blank? @fail_blank = builder.fail_blank?
@dry_run = builder.dry_run? @dry_run = builder.dry_run?
end end
# Yields each formatter that should be reported to.
def each_formatter
@formatters.each do |formatter|
yield formatter
end
end
end end
end end

View file

@ -8,21 +8,27 @@ module Spectator
new.build new.build
end end
@formatter : Formatting::Formatter? = nil @primary_formatter : Formatting::Formatter?
@additional_formatters = [] of Formatting::Formatter
@fail_fast = false @fail_fast = false
@fail_blank = false @fail_blank = false
@dry_run = false @dry_run = false
# Sets the formatter to use for reporting test progress and results. # Sets the primary formatter to use for reporting test progress and results.
def formatter=(formatter : Formatting::Formatter) def formatter=(formatter : Formatting::Formatter)
@formatter = formatter @primary_formatter = formatter
end end
# Retrieves the formatter to use. # Adds an extra formater to use for reporting test progress and results.
def add_formatter(formatter : Formatting::Formatter)
@additional_formatters << formatter
end
# Retrieves the formatters to use.
# If one wasn't specified by the user, # If one wasn't specified by the user,
# then `#default_formatter` is returned. # then `#default_formatter` is returned.
def formatter def formatters
@formatter || default_formatter @additional_formatters + [(@primary_formatter || default_formatter)]
end end
# The formatter that should be used, # The formatter that should be used,

View file

@ -13,7 +13,7 @@ module Spectator
# or false if there was at least one failure. # or false if there was at least one failure.
def run : Bool def run : Bool
# Indicate the suite is starting. # Indicate the suite is starting.
@config.formatter.start_suite(@suite) @config.each_formatter(&.start_suite(@suite))
# Run all examples and capture the results. # Run all examples and capture the results.
results = Array(Result).new(@suite.size) results = Array(Result).new(@suite.size)
@ -24,7 +24,7 @@ module Spectator
# Generate a report and pass it along to the formatter. # Generate a report and pass it along to the formatter.
remaining = @suite.size - results.size remaining = @suite.size - results.size
report = Report.new(results, elapsed, remaining, @config.fail_blank?) report = Report.new(results, elapsed, remaining, @config.fail_blank?)
@config.formatter.end_suite(report) @config.each_formatter(&.end_suite(report))
!report.failed? !report.failed?
end end
@ -41,13 +41,13 @@ module Spectator
# Runs a single example and returns the result. # Runs a single example and returns the result.
# The formatter is given the example and result information. # The formatter is given the example and result information.
private def run_example(example) private def run_example(example)
@config.formatter.start_example(example) @config.each_formatter(&.start_example(example))
result = if @config.dry_run? && example.is_a?(RunnableExample) result = if @config.dry_run? && example.is_a?(RunnableExample)
dry_run_result(example) dry_run_result(example)
else else
Internals::Harness.run(example) Internals::Harness.run(example)
end end
@config.formatter.end_example(result) @config.each_formatter(&.end_example(result))
result result
end end