From c73af72c7f2dc85a20703e819811d5fab8e40ff3 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 13 Dec 2018 14:16:49 -0700 Subject: [PATCH] Move formatter to config --- src/spectator/config.cr | 6 ++++++ src/spectator/config_builder.cr | 28 +++++++++++++++++++++++++--- src/spectator/runner.cr | 11 +++++------ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/spectator/config.cr b/src/spectator/config.cr index 60908ff..86e74fd 100644 --- a/src/spectator/config.cr +++ b/src/spectator/config.cr @@ -1,5 +1,11 @@ module Spectator # Provides customization and describes specifics for how Spectator will run and report tests. class Config + # Used to report test progress and results. + getter formatter : Formatters::Formatter + + # Creates a new configuration. + def initialize(@formatter) + end end end diff --git a/src/spectator/config_builder.cr b/src/spectator/config_builder.cr index f526934..6bced3b 100644 --- a/src/spectator/config_builder.cr +++ b/src/spectator/config_builder.cr @@ -1,9 +1,31 @@ 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 - property formatter : Formatters::Formatter = Formatters::DefaultFormatter.new + @formatter : Formatters::Formatter? = nil - def build - Config.new + # Sets the formatter to use for reporting test progress and results. + 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 diff --git a/src/spectator/runner.cr b/src/spectator/runner.cr index 2eec367..73d5533 100644 --- a/src/spectator/runner.cr +++ b/src/spectator/runner.cr @@ -1,25 +1,24 @@ module Spectator # Main driver for executing tests and feeding results to formatters. class Runner - def initialize(@suite : TestSuite, config : Config) - @formatter = Formatters::DefaultFormatter.new + def initialize(@suite : TestSuite, @config : Config) end def run : Nil results = [] of Result - @formatter.start_suite + @config.formatter.start_suite elapsed = Time.measure do results = @suite.map do |example| run_example(example) end end - @formatter.end_suite(TestSuiteResults.new(results, elapsed)) + @config.formatter.end_suite(TestSuiteResults.new(results, elapsed)) end private def run_example(example) - @formatter.start_example(example) + @config.formatter.start_example(example) result = Internals::Harness.run(example) - @formatter.end_example(result) + @config.formatter.end_example(result) result end end