Don't pass context, get/cast from example instance

This commit is contained in:
Michael Miller 2020-11-08 16:53:54 -07:00
parent b8dc83286c
commit 7d54884196
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
6 changed files with 14 additions and 37 deletions

View file

@ -31,7 +31,7 @@ module Spectator::DSL
# The example is added to the group currently on the top of the stack.
#
# See `Spec::Builder#add_example` for usage details.
def add_example(*args, &block : Example, Context ->)
def add_example(*args, &block : Example ->)
@@builder.add_example(*args, &block)
end
@ -45,7 +45,7 @@ module Spectator::DSL
# Defines a block of code to execute before every example in the current group
#
# See `Spec::Builder#before_each` for usage details.
def before_each(&block : Example, Context -> _)
def before_each(&block : Example -> _)
@@builder.before_each(&block)
end
@ -59,7 +59,7 @@ module Spectator::DSL
# Defines a block of code to execute after every example in the current group.
#
# See `Spec::Builder#after_each` for usage details.
def after_each(&block : Example, Context ->)
def after_each(&block : Example ->)
@@builder.after_each(&block)
end

View file

@ -19,8 +19,8 @@ module Spectator::DSL
::Spectator::DSL::Builder.add_example(
_spectator_example_name(\{{what}}),
::Spectator::Source.new(\{{block.filename}}, \{{block.line_number}}),
) { |example, context| context.as(\{{@type.name}}).\%test }
\{{@type.name}}.new.as(::Spectator::Context)
) { |example| example.with_context(\{{@type.name}}) { \%test } }
end
end

View file

@ -19,7 +19,7 @@ module Spectator::DSL
{{block.body}}
end
::Spectator::DSL::Builder.before_each { |context| context.as({{@type.name}).%hook }
::Spectator::DSL::Builder.before_each { |example| example.with_context({{@type.name}) { %hook } }
end
macro after_all(&block)

View file

@ -34,21 +34,13 @@ module Spectator
# Three methods are defined - two to add a hook and the other to trigger the event which calls every hook.
# A hook can be added with an `ExampleContextDelegate` or a block that accepts an example (no context).
private macro example_event(name)
@{{name.id}}_hooks = Deque(ExampleContextMethod).new
# Defines a hook for the *{{name.id}}* event.
# The *delegate* will be called when the event occurs.
# The current example is provided to the delegate.
def {{name.id}}(delegate : ExampleContextDelegate) : Nil
@{{name.id}}_hooks << delegate
end
@{{name.id}}_hooks = Deque(Example ->).new
# Defines a hook for the *{{name.id}}* event.
# The block of code given to this method is invoked when the event occurs.
# The current example is provided as a block argument.
def {{name.id}}(&block : Example -> _) : Nil
delegate = ExampleContextDelegate.null(&block)
@{{name.id}}_hooks << delegate
def {{name.id}}(&block : Example ->) : Nil
@{{name.id}}_hooks << block
end
# Signals that the *{{name.id}}* event has occurred.

View file

@ -25,7 +25,7 @@ module Spectator
# It can be a `Symbol` to describe a type.
# The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided.
def initialize(@context : Context, @entrypoint : ExampleContextMethod,
def initialize(@context : Context, @entrypoint : self ->,
name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil)
super(name, source, group)
end
@ -37,7 +37,7 @@ module Spectator
# It can be a `Symbol` to describe a type.
# The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided.
def initialize(name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil, &block : Example -> _)
def initialize(name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil, &block : self ->)
@context = NullContext.new
@entrypoint = block
end
@ -49,7 +49,7 @@ module Spectator
@@current = self
Log.debug { "Running example #{self}" }
Log.warn { "Example #{self} already ran" } if @finished
@result = Harness.run { @entrypoint.call(self, @context) }
@result = Harness.run { @entrypoint.call(self) }
ensure
@@current = nil
@finished = true
@ -65,7 +65,7 @@ module Spectator
#
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
def with_context(klass)
context = klass.cast(@delegate.context)
context = klass.cast(@context)
with context yield
end

View file

@ -91,10 +91,9 @@ module Spectator
# It is expected that the test code runs when the block is called.
#
# The newly created example is returned.
def add_example(name, source, context, &block : Example, Context ->) : Example
def add_example(name, source, context, &block : Example -> _) : Example
Log.trace { "Add example: #{name} @ #{source}" }
delegate = ExampleContextDelegate.new(context, block)
Example.new(delegate, name, source, current_group)
Example.new(context, block, name, source, current_group)
# The example is added to the current group by `Example` initializer.
end
@ -104,13 +103,6 @@ module Spectator
current_group.before_all(&block)
end
# Defines a delegate to call before every example in the current group.
# The current example is provided to the delegate.
def before_each(delegate : ExampleContextDelegate)
Log.trace { "Add before_each hook delegate" }
current_group.before_each(delegate)
end
# Defines a block of code to execute before every example in the current group.
# The current example is provided as a block argument.
def before_each(&block : Example -> _)
@ -124,13 +116,6 @@ module Spectator
current_group.after_all(&block)
end
# Defines a delegate to call after every example in the current group.
# The current example is provided to the delegate.
def after_each(delegate : ExampleContextDelegate)
Log.trace { "Add after_each hook delegate" }
current_group.after_each(delegate)
end
# Defines a block of code to execute after every example in the current group.
# The current example is provided as a block argument.
def after_each(&block : Example -> _)