Remove TestSuite middle-man object

This commit is contained in:
Michael Miller 2021-05-08 23:38:13 -06:00
parent e7138080a6
commit f09a6a87e5
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
3 changed files with 26 additions and 47 deletions

View file

@ -1,19 +1,28 @@
require "./config" require "./config"
require "./example"
require "./example_group" require "./example_group"
require "./test_suite" require "./spec/*"
module Spectator module Spectator
# Contains examples to be tested. # Contains examples to be tested and configuration for running them.
class Spec class Spec
# Creates the spec.
# The *root* is the top-most example group.
# All examples in this group and groups nested under are candidates for execution.
# The *config* provides settings controlling how tests will be executed.
def initialize(@root : ExampleGroup, @config : Config) def initialize(@root : ExampleGroup, @config : Config)
end end
def run(filter : ExampleFilter) # Runs all selected examples and returns the results.
suite = TestSuite.new(@root, filter) def run
Runner.new(suite, @config).run Runner.new(examples, @config).run
end
# Selects and shuffles the examples that should run.
private def examples
iterator = @config.iterator(@root)
iterator.to_a.tap do |examples|
@config.shuffle!(examples)
end
end end
end end
end end
require "./spec/*"

View file

@ -2,14 +2,18 @@ require "../example"
module Spectator module Spectator
class Spec class Spec
# Logic for executing examples and collecting results.
private struct Runner private struct Runner
def initialize(@suite : TestSuite, @config : Config) # Creates the runner.
# The collection of *examples* should be pre-filtered and shuffled.
# This runner will run each example in the order provided.
def initialize(@examples : Enumerable(Example), @config : Config)
end end
# Runs the test suite. # Runs the spec.
# This will run the selected examples # This will run the provided examples
# and invoke the formatter to output results. # and invoke the reporters to communicate results.
# True will be returned if the test suite ran successfully, # True will be returned if the spec ran successfully,
# or false if there was at least one failure. # or false if there was at least one failure.
def run : Bool def run : Bool
# Indicate the suite is starting. # Indicate the suite is starting.
@ -42,15 +46,6 @@ module Spectator
end end
end end
# Retrieves an enumerable for the examples to run.
# The order of examples is randomized
# if specified by the configuration.
private def example_order
@suite.to_a.tap do |examples|
@config.shuffle!(examples)
end
end
# Runs a single example and returns the result. # Runs a single example and returns the result.
# The formatter is given the example and result information. # The formatter is given the example and result information.
private def run_example(example) private def run_example(example)

View file

@ -1,25 +0,0 @@
module Spectator
# Encapsulates the tests to run and additional properties about them.
# Use `#each` to enumerate over all tests in the suite.
class TestSuite
include Enumerable(Example)
# Creates the test suite.
# The example *group* provided will be run.
# The *filter* identifies which examples to run from the *group*.
def initialize(@group : ExampleGroup, @filter : ExampleFilter)
end
# Yields each example in the test suite.
def each : Nil
iterator.each do |example|
yield example if @filter.includes?(example)
end
end
# Creates an iterator for the example group.
private def iterator
ExampleIterator.new(@group)
end
end
end