From b2bf98068539f305b22bf7461757b889e94eb03b Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Oct 2020 21:39:41 -0600 Subject: [PATCH] Some config cleanup --- src/spectator/config.cr | 12 ++++------- src/spectator/config_builder.cr | 20 ++++++------------- src/spectator/spec_builder.cr | 35 +++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/spectator/config.cr b/src/spectator/config.cr index cde982d..bc71d41 100644 --- a/src/spectator/config.cr +++ b/src/spectator/config.cr @@ -16,19 +16,16 @@ module Spectator # Examples won't run, but the output will show that they did. getter? dry_run : Bool - # Random number generator to use for everything. - getter random : Random - - # Indicates whether tests are run in a random order. + # Indicates whether examples run in a random order. getter? randomize : Bool - # Random seed used for number generation. + # Seed used for random number generation. getter! random_seed : UInt64? - # Indicates whether profiling information should be displayed. + # Indicates whether timing information should be displayed. getter? profile : Bool - # Filter that determines which examples to run. + # Filter determining examples to run. getter example_filter : ExampleFilter # Creates a new configuration. @@ -37,7 +34,6 @@ module Spectator @fail_fast = builder.fail_fast? @fail_blank = builder.fail_blank? @dry_run = builder.dry_run? - @random = builder.random @randomize = builder.randomize? @random_seed = builder.seed? @profile = builder.profile? diff --git a/src/spectator/config_builder.cr b/src/spectator/config_builder.cr index 582edaf..1db01dc 100644 --- a/src/spectator/config_builder.cr +++ b/src/spectator/config_builder.cr @@ -1,4 +1,5 @@ require "./composite_example_filter" +require "./config" require "./example_filter" require "./null_example_filter" @@ -12,14 +13,6 @@ module Spectator new.build end - # Random number generator to use. - protected getter random = Random::DEFAULT - - def initialize - @seed = seed = @random.rand(UInt16).to_u64 - @random.new_seed(seed) - end - @primary_formatter : Formatting::Formatter? @additional_formatters = [] of Formatting::Formatter @fail_fast = false @@ -29,6 +22,11 @@ module Spectator @profile = false @filters = [] of ExampleFilter + # Creates a configuration. + def build : Config + Config.new(self) + end + # Sets the primary formatter to use for reporting test progress and results. def formatter=(formatter : Formatting::Formatter) @primary_formatter = formatter @@ -105,7 +103,6 @@ module Spectator # Sets the seed for the random number generator. def seed=(seed) @seed = seed - @random = Random.new(seed) end # Randomizes test execution order. @@ -153,10 +150,5 @@ module Spectator CompositeExampleFilter.new(@filters) end end - - # Creates a configuration. - def build : Config - Config.new(self) - end end end diff --git a/src/spectator/spec_builder.cr b/src/spectator/spec_builder.cr index 54f8ed6..4c54b17 100644 --- a/src/spectator/spec_builder.cr +++ b/src/spectator/spec_builder.cr @@ -30,6 +30,17 @@ module Spectator @group_stack.push(root_group) end + # Constructs the test spec. + # Returns the spec instance. + # + # Raises an error if there were not symmetrical calls to `#start_group` and `#end_group`. + # This would indicate a logical error somewhere in Spectator or an extension of it. + def build : Spec + raise "Mismatched start and end groups" unless root? + + Spec.new(root_group, config) + end + # Defines a new example group and pushes it onto the group stack. # Examples and groups defined after calling this method will be nested under the new group. # The group will be finished and popped off the stack when `#end_example` is called. @@ -87,23 +98,21 @@ module Spectator # The example is added to the current group by `Example` initializer. end + # Builds the configuration to use for the spec. + # A `ConfigBuilder` is yielded to the block provided to this method. + # That builder will be used to create the configuration. + def config + builder = ConfigBuilder.new + yield builder + @config = builder.build + end + # Sets the configuration of the spec. # This configuration controls how examples run. def config=(config) @config = config end - # Constructs the test spec. - # Returns the spec instance. - # - # Raises an error if there were not symmetrical calls to `#start_group` and `#end_group`. - # This would indicate a logical error somewhere in Spectator or an extension of it. - def build : Spec - raise "Mismatched start and end groups" unless root? - - Spec.new(root_group, config) - end - # Checks if the current group is the root group. private def root? @group_stack.size == 1 @@ -122,8 +131,8 @@ module Spectator # Retrieves the configuration. # If one wasn't previously set, a default configuration is used. - private def config - @config || ConfigBuilder.new.build + private def config : Config + @config || ConfigBuilder.default end end end