diff --git a/src/spectator.cr b/src/spectator.cr index a2df911..5391a78 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -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 diff --git a/src/spectator/config_builder.cr b/src/spectator/config_builder.cr index d65ffc1..582edaf 100644 --- a/src/spectator/config_builder.cr +++ b/src/spectator/config_builder.cr @@ -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. diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index 638b39d..dc4647c 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -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. # diff --git a/src/spectator/spec.cr b/src/spectator/spec.cr index 17218f9..a1dfa5c 100644 --- a/src/spectator/spec.cr +++ b/src/spectator/spec.cr @@ -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 diff --git a/src/spectator/spec_builder.cr b/src/spectator/spec_builder.cr index 09c81a0..54f8ed6 100644 --- a/src/spectator/spec_builder.cr +++ b/src/spectator/spec_builder.cr @@ -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