Don't create test as part of run method

This commit is contained in:
Michael Miller 2019-09-17 20:37:06 -06:00
parent 9129aa4286
commit da8736f891
4 changed files with 22 additions and 20 deletions

View file

@ -25,7 +25,7 @@ module Spectator
# Capture how long it takes to run the test code. # Capture how long it takes to run the test code.
result.elapsed = Time.measure do result.elapsed = Time.measure do
begin begin
test_wrapper.run {} # Actually run the example code. test_wrapper.run # Actually run the example code.
rescue ex # Catch all errors and handle them later. rescue ex # Catch all errors and handle them later.
result.error = ex result.error = ex
end end

View file

@ -45,8 +45,7 @@ module Spectator
def add_example(description : String, source : Source, def add_example(description : String, source : Source,
example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil
builder = ->{ example_type.new.as(::SpectatorTest) } builder = ->{ example_type.new.as(::SpectatorTest) }
wrapper = TestWrapper.new(description, source, builder, runner) factory = ExampleBuilder.new(description, source, builder, runner)
factory = ExampleBuilder.new(wrapper)
@@stack.current.add_child(factory) @@stack.current.add_child(factory)
end end

View file

@ -1,10 +1,17 @@
require "../../spectator_test"
require "../test_wrapper"
module Spectator::SpecBuilder module Spectator::SpecBuilder
class ExampleBuilder class ExampleBuilder
def initialize(@wrapper : TestWrapper) alias FactoryMethod = -> ::SpectatorTest
def initialize(@description : String, @source : Source, @builder : FactoryMethod, @runner : TestMethod)
end end
def build(group) def build(group)
RunnableExample.new(group, @wrapper).as(ExampleComponent) test = @builder.call
wrapper = TestWrapper.new(@description, @source, test, @runner)
RunnableExample.new(group, wrapper).as(ExampleComponent)
end end
end end
end end

View file

@ -7,26 +7,22 @@ module Spectator
# Stores information about a end-user test. # Stores information about a end-user test.
# Used to instantiate tests and run them. # Used to instantiate tests and run them.
struct TestWrapper struct TestWrapper
# Location of the test in source code.
getter source : Source
# Description the user provided for the test. # Description the user provided for the test.
getter description : String getter description
# Location of the test in source code.
getter source
# Creates a wrapper for the test. # Creates a wrapper for the test.
# The *builder* creates an instance of the test. def initialize(@description : String, @source : Source, @test : ::SpectatorTest, @runner : TestMethod)
# The *runner* takes the test created by *builder* and runs it.
def initialize(@description, @source, @builder : -> ::SpectatorTest, @runner : TestMethod)
end end
# Instantiates and runs the test. def run
# This method yields twice - before and after the test. call(@runner)
# The test instance is yielded. end
def run : Nil
test = @builder.call def call(method : TestMethod) : Nil
yield test method.call(@test)
@runner.call(test)
yield test
end end
end end
end end