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

View file

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

View file

@ -19,7 +19,7 @@ module Spectator::DSL
{{block.body}} {{block.body}}
end 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 end
macro after_all(&block) 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. # 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). # A hook can be added with an `ExampleContextDelegate` or a block that accepts an example (no context).
private macro example_event(name) private macro example_event(name)
@{{name.id}}_hooks = Deque(ExampleContextMethod).new @{{name.id}}_hooks = Deque(Example ->).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
# Defines a hook for the *{{name.id}}* event. # Defines a hook for the *{{name.id}}* event.
# The block of code given to this method is invoked when the event occurs. # The block of code given to this method is invoked when the event occurs.
# The current example is provided as a block argument. # The current example is provided as a block argument.
def {{name.id}}(&block : Example -> _) : Nil def {{name.id}}(&block : Example ->) : Nil
delegate = ExampleContextDelegate.null(&block) @{{name.id}}_hooks << block
@{{name.id}}_hooks << delegate
end end
# Signals that the *{{name.id}}* event has occurred. # 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. # It can be a `Symbol` to describe a type.
# The *source* tracks where the example exists in source code. # The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided. # 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) name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil)
super(name, source, group) super(name, source, group)
end end
@ -37,7 +37,7 @@ module Spectator
# It can be a `Symbol` to describe a type. # It can be a `Symbol` to describe a type.
# The *source* tracks where the example exists in source code. # The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided. # 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 @context = NullContext.new
@entrypoint = block @entrypoint = block
end end
@ -49,7 +49,7 @@ module Spectator
@@current = self @@current = self
Log.debug { "Running example #{self}" } Log.debug { "Running example #{self}" }
Log.warn { "Example #{self} already ran" } if @finished Log.warn { "Example #{self} already ran" } if @finished
@result = Harness.run { @entrypoint.call(self, @context) } @result = Harness.run { @entrypoint.call(self) }
ensure ensure
@@current = nil @@current = nil
@finished = true @finished = true
@ -65,7 +65,7 @@ module Spectator
# #
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc. # TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
def with_context(klass) def with_context(klass)
context = klass.cast(@delegate.context) context = klass.cast(@context)
with context yield with context yield
end end

View file

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