Capture source info for hooks

This commit is contained in:
Michael Miller 2021-01-09 11:30:00 -07:00
parent def66acc15
commit df096d91aa
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 50 additions and 24 deletions

View file

@ -36,31 +36,27 @@ module Spectator::DSL
end
# Defines a block of code to execute before any and all examples in the current group.
#
# See `Spec::Builder#before_all` for usage details.
def before_all(&block)
@@builder.before_all(&block)
def before_all(source = nil, label = "before_all", &block)
hook = ExampleGroupHook.new(source: source, label: label, &block)
@@builder.before_all(hook)
end
# 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 -> _)
@@builder.before_each(&block)
def before_each(source = nil, label = "before_each", &block : Example -> _)
hook = ExampleHook.new(source: source, label: label, &block)
@@builder.before_each(hook)
end
# Defines a block of code to execute after any and all examples in the current group.
#
# See `Spec::Builder#after_all` for usage details.
def after_all(&block)
@@builder.after_all(&block)
def after_all(source = nil, label = "after_all", &block)
hook = ExampleGroupHook.new(source: source, label: label, &block)
@@builder.after_all(hook)
end
# 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 ->)
@@builder.after_each(&block)
def after_each(source = nil, label = "after_each", &block : Example ->)
hook = ExampleHook.new(source: source, label: label, &block)
@@builder.after_each(hook)
end
# Sets the configuration of the spec.

View file

@ -11,7 +11,9 @@ module Spectator::DSL
{{block.body}}
end
::Spectator::DSL::Builder.before_all { {{@type.name}}.%hook }
::Spectator::DSL::Builder.before_all(
::Spectator::Source.new({{block.filename}}, {{block.line_number}})
) { {{@type.name}}.%hook }
end
macro before_each(&block)
@ -21,9 +23,9 @@ module Spectator::DSL
{{block.body}}
end
::Spectator::DSL::Builder.before_each do |example|
example.with_context({{@type.name}}) { %hook }
end
::Spectator::DSL::Builder.before_each(
::Spectator::Source.new({{block.filename}}, {{block.line_number}})
) { |example| example.with_context({{@type.name}}) { %hook } }
end
macro after_all(&block)
@ -33,7 +35,9 @@ module Spectator::DSL
{{block.body}}
end
::Spectator::DSL::Builder.after_all { {{@type.name}}.%hook }
::Spectator::DSL::Builder.after_all(
::Spectator::Source.new({{block.filename}}, {{block.line_number}})
) { {{@type.name}}.%hook }
end
macro after_each(&block)
@ -43,9 +47,9 @@ module Spectator::DSL
{{block.body}}
end
::Spectator::DSL::Builder.after_each do |example|
example.with_context({{@type.name}}) { %hook }
end
::Spectator::DSL::Builder.after_each(
::Spectator::Source.new({{block.filename}}, {{block.line_number}})
) { |example| example.with_context({{@type.name}}) { %hook } }
end
end
end

View file

@ -97,12 +97,25 @@ module Spectator
# The example is added to the current group by `Example` initializer.
end
# Attaches a hook to be invoked before any and all examples in the current group.
def before_all(hook)
Log.trace { "Add before_all hook #{hook}" }
current_group.add_before_all_hook(hook)
end
# Defines a block of code to execute before any and all examples in the current group.
def before_all(&block)
Log.trace { "Add before_all hook" }
current_group.before_all(&block)
end
# Attaches a hook to be invoked before every example in the current group.
# The current example is provided as a block argument.
def before_each(hook)
Log.trace { "Add before_each hook #{hook}" }
current_group.add_before_each_hook(hook)
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 -> _)
@ -110,12 +123,25 @@ module Spectator
current_group.before_each(&block)
end
# Attaches a hook to be invoked after any and all examples in the current group.
def after_all(hook)
Log.trace { "Add after_all hook #{hook}" }
current_group.add_after_all_hook(hook)
end
# Defines a block of code to execute after any and all examples in the current group.
def after_all(&block)
Log.trace { "Add after_all hook" }
current_group.after_all(&block)
end
# Attaches a hook to be invoked after every example in the current group.
# The current example is provided as a block argument.
def after_each(hook)
Log.trace { "Add after_each hook #{hook}" }
current_group.add_after_each_hook(hook)
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 -> _)