diff --git a/src/spectator.cr b/src/spectator.cr index 81a13cf..7fb2aa6 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -76,7 +76,8 @@ module Spectator private def self.run # Build the root-level example group and run it. group = ::Spectator::DSL::Builder.build - Runner.new(group).run + suite = ::Spectator::TestSuite.new(group) + Runner.new(suite).run rescue ex # Catch all unhandled exceptions here. # Examples are already wrapped, so any exceptions they throw are caught. diff --git a/src/spectator/runner.cr b/src/spectator/runner.cr index 9908012..569a880 100644 --- a/src/spectator/runner.cr +++ b/src/spectator/runner.cr @@ -1,23 +1,26 @@ module Spectator # Main driver for executing tests and feeding results to formatters. class Runner - def initialize(@group : ExampleGroup, + def initialize(@suite : TestSuite, @formatter : Formatters::Formatter = Formatters::DefaultFormatter.new) end def run : Nil - iterator = ExampleIterator.new(@group) results = [] of Result + @formatter.start_suite elapsed = Time.measure do - @formatter.start_suite - results = iterator.map do |example| - @formatter.start_example(example) - Internals::Harness.run(example).tap do |result| - @formatter.end_example(result) - end.as(Result) - end.to_a + results = @suite.map do |example| + run_example(example) + end end @formatter.end_suite(TestSuiteResults.new(results, elapsed)) end + + private def run_example(example) + @formatter.start_example(example) + result = Internals::Harness.run(example) + @formatter.end_example(result) + result + end end end diff --git a/src/spectator/test_suite.cr b/src/spectator/test_suite.cr index 7c1f4b0..8d973b9 100644 --- a/src/spectator/test_suite.cr +++ b/src/spectator/test_suite.cr @@ -1,5 +1,19 @@ module Spectator # Encapsulates the tests to run and additional properties about them. class TestSuite + include Enumerable(Example) + + def initialize(@group : ExampleGroup) + end + + def each + iterator.each do |example| + yield example + end + end + + private def iterator + ExampleIterator.new(@group) + end end end