Add config to spec builder

This commit is contained in:
Michael Miller 2020-10-17 17:40:38 -06:00
parent 4462f27316
commit 79499c5d2e
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
5 changed files with 49 additions and 5 deletions

View file

@ -91,8 +91,9 @@ module Spectator
# Builds the tests and runs the framework.
private def run
# Build the test spec and run it.
spec = ::Spectator::DSL::Builder.build
# Build the spec and run it.
# DSL::Builder.config = config # TODO: Set config.
spec = DSL::Builder.build
spec.run
true
rescue ex

View file

@ -1,3 +1,7 @@
require "./composite_example_filter"
require "./example_filter"
require "./null_example_filter"
module Spectator
# Mutable configuration used to produce a final configuration.
# Use the setters in this class to incrementally build a configuration.

View file

@ -35,6 +35,13 @@ module Spectator::DSL
@@builder.add_example(*args, &block)
end
# Sets the configuration of the spec.
#
# See `Spec::Builder#config=` for usage details.
def config=(config)
@@builder.config = config
end
# Constructs the test spec.
# Returns the spec instance.
#

View file

@ -1,3 +1,4 @@
require "./config"
require "./example"
require "./example_group"
require "./example_iterator"
@ -5,14 +6,28 @@ require "./example_iterator"
module Spectator
# Contains examples to be tested.
class Spec
def initialize(@root : ExampleGroup)
def initialize(@root : ExampleGroup, @config : Config)
end
def run
examples = ExampleIterator.new(@root).to_a
Runner.new(examples).run
end
# Generates a list of examples to run.
# The order of the examples are also sorted based on the configuration.
private def examples
ExampleIterator.new(@root).to_a.tap do |examples|
if @config.randomize?
random = if (seed = @config.random_seed)
Random.new(seed)
else
Random.new
end
examples.shuffle!(random)
end
end
end
private struct Runner
def initialize(@examples : Array(Example))
end

View file

@ -1,3 +1,5 @@
require "./config"
require "./config_builder"
require "./example"
require "./example_context_method"
require "./example_group"
@ -17,6 +19,9 @@ module Spectator
# New examples should be added to the current group.
@group_stack : Deque(ExampleGroup)
# Configuration for the spec.
@config : Config?
# Creates a new spec builder.
# A root group is pushed onto the group stack.
def initialize
@ -82,6 +87,12 @@ module Spectator
# The example is added to the current group by `Example` initializer.
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.
#
@ -90,7 +101,7 @@ module Spectator
def build : Spec
raise "Mismatched start and end groups" unless root?
Spec.new(root_group)
Spec.new(root_group, config)
end
# Checks if the current group is the root group.
@ -108,5 +119,11 @@ module Spectator
private def current_group
@group_stack.last
end
# Retrieves the configuration.
# If one wasn't previously set, a default configuration is used.
private def config
@config || ConfigBuilder.new.build
end
end
end