Move formatter to config

This commit is contained in:
Michael Miller 2018-12-13 14:16:49 -07:00
parent 23368f6183
commit c73af72c7f
3 changed files with 36 additions and 9 deletions

View file

@ -1,5 +1,11 @@
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.
getter formatter : Formatters::Formatter
# Creates a new configuration.
def initialize(@formatter)
end
end end
end end

View file

@ -1,9 +1,31 @@
module Spectator module Spectator
# Mutable configuration used to produce a final configuration.
# Use the setters in this class to incrementally build a configuration.
# Then call `#build` to create the final configuration.
class ConfigBuilder class ConfigBuilder
property formatter : Formatters::Formatter = Formatters::DefaultFormatter.new @formatter : Formatters::Formatter? = nil
def build # Sets the formatter to use for reporting test progress and results.
Config.new def formatter=(formatter : Formatters::Formatter)
@formatter = formatter
end
# Retrieves the formatter to use.
# If one wasn't specified by the user,
# then `#default_formatter` is returned.
private def formatter
@formatter || default_formatter
end
# The formatter that should be used,
# if one wasn't provided.
private def default_formatter
Formatters::DefaultFormatter.new
end
# Creates a configuration.
def build : Config
Config.new(formatter)
end end
end end
end end

View file

@ -1,25 +1,24 @@
module Spectator module Spectator
# Main driver for executing tests and feeding results to formatters. # Main driver for executing tests and feeding results to formatters.
class Runner class Runner
def initialize(@suite : TestSuite, config : Config) def initialize(@suite : TestSuite, @config : Config)
@formatter = Formatters::DefaultFormatter.new
end end
def run : Nil def run : Nil
results = [] of Result results = [] of Result
@formatter.start_suite @config.formatter.start_suite
elapsed = Time.measure do elapsed = Time.measure do
results = @suite.map do |example| results = @suite.map do |example|
run_example(example) run_example(example)
end end
end end
@formatter.end_suite(TestSuiteResults.new(results, elapsed)) @config.formatter.end_suite(TestSuiteResults.new(results, elapsed))
end end
private def run_example(example) private def run_example(example)
@formatter.start_example(example) @config.formatter.start_example(example)
result = Internals::Harness.run(example) result = Internals::Harness.run(example)
@formatter.end_example(result) @config.formatter.end_example(result)
result result
end end
end end