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.
result.elapsed = Time.measure do
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.
result.error = ex
end

View file

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

View file

@ -1,10 +1,17 @@
require "../../spectator_test"
require "../test_wrapper"
module Spectator::SpecBuilder
class ExampleBuilder
def initialize(@wrapper : TestWrapper)
alias FactoryMethod = -> ::SpectatorTest
def initialize(@description : String, @source : Source, @builder : FactoryMethod, @runner : TestMethod)
end
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

View file

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