From f09a6a87e558d9cda672e30cae9e57fd7d532173 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 8 May 2021 23:38:13 -0600 Subject: [PATCH] Remove TestSuite middle-man object --- src/spectator/spec.cr | 25 +++++++++++++++++-------- src/spectator/spec/runner.cr | 23 +++++++++-------------- src/spectator/test_suite.cr | 25 ------------------------- 3 files changed, 26 insertions(+), 47 deletions(-) delete mode 100644 src/spectator/test_suite.cr diff --git a/src/spectator/spec.cr b/src/spectator/spec.cr index a034332..03f3d51 100644 --- a/src/spectator/spec.cr +++ b/src/spectator/spec.cr @@ -1,19 +1,28 @@ require "./config" -require "./example" require "./example_group" -require "./test_suite" +require "./spec/*" module Spectator - # Contains examples to be tested. + # Contains examples to be tested and configuration for running them. 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) end - def run(filter : ExampleFilter) - suite = TestSuite.new(@root, filter) - Runner.new(suite, @config).run + # Runs all selected examples and returns the results. + def 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 - -require "./spec/*" diff --git a/src/spectator/spec/runner.cr b/src/spectator/spec/runner.cr index f5ccf8f..69d8940 100644 --- a/src/spectator/spec/runner.cr +++ b/src/spectator/spec/runner.cr @@ -2,14 +2,18 @@ require "../example" module Spectator class Spec + # Logic for executing examples and collecting results. 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 - # Runs the test suite. - # This will run the selected examples - # and invoke the formatter to output results. - # True will be returned if the test suite ran successfully, + # Runs the spec. + # This will run the provided examples + # and invoke the reporters to communicate results. + # True will be returned if the spec ran successfully, # or false if there was at least one failure. def run : Bool # Indicate the suite is starting. @@ -42,15 +46,6 @@ module Spectator 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. # The formatter is given the example and result information. private def run_example(example) diff --git a/src/spectator/test_suite.cr b/src/spectator/test_suite.cr deleted file mode 100644 index 2106193..0000000 --- a/src/spectator/test_suite.cr +++ /dev/null @@ -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