Some work on hooks in DSL

This commit is contained in:
Michael Miller 2020-11-08 22:21:52 -07:00
parent 7d54884196
commit b4e74444d1
No known key found for this signature in database
GPG Key ID: F9A0C5C65B162436
3 changed files with 8 additions and 79 deletions

View File

@ -2,6 +2,7 @@
require "./dsl/builder"
require "./dsl/examples"
require "./dsl/groups"
require "./dsl/hooks"
require "./dsl/top"
module Spectator

View File

@ -1,3 +1,5 @@
require "./builder"
module Spectator::DSL
# DSL methods for adding custom logic to key times of the spec execution.
module Hooks
@ -9,7 +11,7 @@ module Spectator::DSL
{{block.body}}
end
::Spectator::DSL::Builder.before_all { {{@type.name}.%hook }
::Spectator::DSL::Builder.before_all { {{@type.name}}.%hook }
end
macro before_each(&block)
@ -19,7 +21,9 @@ module Spectator::DSL
{{block.body}}
end
::Spectator::DSL::Builder.before_each { |example| example.with_context({{@type.name}) { %hook } }
::Spectator::DSL::Builder.before_each do |example|
example.with_context({{@type.name}}) { %hook }
end
end
macro after_all(&block)
@ -28,81 +32,4 @@ module Spectator::DSL
macro after_each(&block)
end
end
macro before_each(&block)
def %hook({{block.args.splat}}) : Nil
{{block.body}}
end
::Spectator::SpecBuilder.add_before_each_hook do |test, example|
cast_test = test.as({{@type.id}})
{% if block.args.empty? %}
cast_test.%hook
{% else %}
cast_test.%hook(example)
{% end %}
end
end
macro after_each(&block)
def %hook({{block.args.splat}}) : Nil
{{block.body}}
end
::Spectator::SpecBuilder.add_after_each_hook do |test, example|
cast_test = test.as({{@type.id}})
{% if block.args.empty? %}
cast_test.%hook
{% else %}
cast_test.%hook(example)
{% end %}
end
end
macro before_all(&block)
::Spectator::SpecBuilder.add_before_all_hook {{block}}
end
macro after_all(&block)
::Spectator::SpecBuilder.add_after_all_hook {{block}}
end
macro around_each(&block)
def %hook({{block.args.first || :example.id}}) : Nil
{{block.body}}
end
::Spectator::SpecBuilder.add_around_each_hook { |test, proc| test.as({{@type.id}}).%hook(proc) }
end
macro pre_condition(&block)
def %hook({{block.args.splat}}) : Nil
{{block.body}}
end
::Spectator::SpecBuilder.add_pre_condition do |test, example|
cast_test = test.as({{@type.id}})
{% if block.args.empty? %}
cast_test.%hook
{% else %}
cast_test.%hook(example)
{% end %}
end
end
macro post_condition(&block)
def %hook({{block.args.splat}}) : Nil
{{block.body}}
end
::Spectator::SpecBuilder.add_post_condition do |test, example|
cast_test = test.as({{@type.id}})
{% if block.args.empty? %}
cast_test.%hook
{% else %}
cast_test.%hook(example)
{% end %}
end
end
end
end

View File

@ -4,6 +4,7 @@ require "./spectator/dsl"
class SpectatorTestContext < SpectatorContext
include ::Spectator::DSL::Examples
include ::Spectator::DSL::Groups
include ::Spectator::DSL::Hooks
# Initial implicit subject for tests.
# This method should be overridden by example groups when an object is described.