Pass test values to test instance

This commit is contained in:
Michael Miller 2019-09-26 13:00:23 -06:00
parent 3c9846ae9b
commit cd0ba81417
8 changed files with 22 additions and 9 deletions

View file

@ -44,7 +44,7 @@ module Spectator
# The example will be instantiated later.
def add_example(description : String, source : Source,
example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil
builder = ->{ example_type.new.as(::SpectatorTest) }
builder = ->(values : TestValues) { example_type.new(values).as(::SpectatorTest) }
factory = ExampleBuilder.new(description, source, builder, runner)
@@stack.current.add_child(factory)
end

View file

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

View file

@ -7,7 +7,7 @@ module Spectator::SpecBuilder
end
def build(parent_group)
context = TestContext.new(parent_group.context, build_hooks)
context = TestContext.new(parent_group.context, build_hooks, parent_group.context.values)
NestedExampleGroup.new(@what, parent_group, context).tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)

View file

@ -1,9 +1,10 @@
require "../test_values"
require "./example_group_builder"
module Spectator::SpecBuilder
class RootExampleGroupBuilder < ExampleGroupBuilder
def build
context = TestContext.new(nil, build_hooks)
context = TestContext.new(nil, build_hooks, TestValues.empty)
RootExampleGroup.new(context).tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)

View file

@ -2,11 +2,12 @@ require "./nested_example_group_builder"
module Spectator::SpecBuilder
class SampleExampleGroupBuilder < NestedExampleGroupBuilder
def initialize(@what : String)
def initialize(@what : String | Symbol)
@id = :TODO
end
def build(parent_group)
context = TestContext.new(parent_group.context, build_hooks)
context = TestContext.new(parent_group.context, build_hooks, parent_group.context.values)
NestedExampleGroup.new(@what, parent_group, context).tap do |group|
group.children = [:TODO].map do |element|
build_sub_group(group, element).as(ExampleComponent)
@ -15,7 +16,8 @@ module Spectator::SpecBuilder
end
private def build_sub_group(parent_group, element)
context = TestContext.new(parent_group.context, ExampleHooks.empty)
values = parent_group.context.values.add(@id, @what.to_s, element)
context = TestContext.new(parent_group.context, ExampleHooks.empty, values)
NestedExampleGroup.new("TODO", parent_group, context).tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)

View file

@ -1,6 +1,11 @@
require "./example_hooks"
require "./test_values"
module Spectator
class TestContext
def initialize(@parent : TestContext?, @hooks : ExampleHooks)
getter values
def initialize(@parent : TestContext?, @hooks : ExampleHooks, @values : TestValues)
@before_all_hooks_run = false
@after_all_hooks_run = false
end

View file

@ -1,3 +1,4 @@
require "./typed_value_wrapper"
require "./value_wrapper"
module Spectator

View file

@ -5,4 +5,7 @@ require "./spectator/dsl"
# so that the namespace isn't leaked into tests unexpectedly.
class SpectatorTest
include ::Spectator::DSL
def initialize(@spectator_test_values : ::Spectator::TestValues)
end
end