mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add pre- and post-conditions
This commit is contained in:
parent
1eec9190dd
commit
f47c48b483
9 changed files with 80 additions and 16 deletions
|
@ -45,5 +45,35 @@ module Spectator
|
||||||
|
|
||||||
::Spectator::SpecBuilder.add_around_each_hook { |test, proc| test.as({{@type.id}}).%hook(proc) }
|
::Spectator::SpecBuilder.add_around_each_hook { |test, proc| test.as({{@type.id}}).%hook(proc) }
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,28 +10,32 @@ module Spectator
|
||||||
# This will effectively run nothing extra while running a test.
|
# This will effectively run nothing extra while running a test.
|
||||||
def self.empty
|
def self.empty
|
||||||
new(
|
new(
|
||||||
[] of ->,
|
[] of TestMetaMethod,
|
||||||
[] of ->
|
[] of TestMetaMethod
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a new set of conditions.
|
# Creates a new set of conditions.
|
||||||
def initialize(
|
def initialize(
|
||||||
@pre_conditions : Array(->),
|
@pre_conditions : Array(TestMetaMethod),
|
||||||
@post_conditions : Array(->)
|
@post_conditions : Array(TestMetaMethod)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Runs all pre-condition checks.
|
# Runs all pre-condition checks.
|
||||||
# These should be run before every test.
|
# These should be run before every test.
|
||||||
def run_pre_conditions
|
def run_pre_conditions(wrapper : TestWrapper, example : Example)
|
||||||
@pre_conditions.each &.call
|
@pre_conditions.each do |hook|
|
||||||
|
wrapper.call(hook, example)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Runs all post-condition checks.
|
# Runs all post-condition checks.
|
||||||
# These should be run after every test.
|
# These should be run after every test.
|
||||||
def run_post_conditions
|
def run_post_conditions(wrapper : TestWrapper, example : Example)
|
||||||
@post_conditions.each &.call
|
@post_conditions.each do |hook|
|
||||||
|
wrapper.call(hook, example)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,12 +25,15 @@ module Spectator
|
||||||
|
|
||||||
# Runs the test code and captures the result.
|
# Runs the test code and captures the result.
|
||||||
private def run_example(result)
|
private def run_example(result)
|
||||||
wrapper = test_wrapper.around_hook(group.context)
|
context = group.context
|
||||||
|
wrapper = test_wrapper.around_hook(context)
|
||||||
|
|
||||||
# Capture how long it takes to run the test code.
|
# Capture how long it takes to run the test code.
|
||||||
result.elapsed = Time.measure do
|
result.elapsed = Time.measure do
|
||||||
begin
|
begin
|
||||||
|
context.run_pre_conditions(self)
|
||||||
wrapper.call
|
wrapper.call
|
||||||
|
context.run_post_conditions(self)
|
||||||
rescue ex # Catch all errors and handle them later.
|
rescue ex # Catch all errors and handle them later.
|
||||||
result.error = ex
|
result.error = ex
|
||||||
end
|
end
|
||||||
|
|
|
@ -87,12 +87,12 @@ module Spectator
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds a pre-condition to run at the start of every example in the current group.
|
# Adds a pre-condition to run at the start of every example in the current group.
|
||||||
def add_pre_condition(&block : ->) : Nil
|
def add_pre_condition(&block : TestMetaMethod) : Nil
|
||||||
@@stack.current.add_pre_condition(block)
|
@@stack.current.add_pre_condition(block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds a post-condition to run at the end of every example in the current group.
|
# Adds a post-condition to run at the end of every example in the current group.
|
||||||
def add_post_condition(&block : ->) : Nil
|
def add_post_condition(&block : TestMetaMethod) : Nil
|
||||||
@@stack.current.add_post_condition(block)
|
@@stack.current.add_post_condition(block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ module Spectator::SpecBuilder
|
||||||
@before_all_hooks = Deque(->).new
|
@before_all_hooks = Deque(->).new
|
||||||
@after_all_hooks = Deque(->).new
|
@after_all_hooks = Deque(->).new
|
||||||
@around_each_hooks = Deque(::SpectatorTest, Proc(Nil) ->).new
|
@around_each_hooks = Deque(::SpectatorTest, Proc(Nil) ->).new
|
||||||
|
@pre_conditions = Deque(TestMetaMethod).new
|
||||||
|
@post_conditions = Deque(TestMetaMethod).new
|
||||||
|
|
||||||
def add_child(child : Child)
|
def add_child(child : Child)
|
||||||
@children << child
|
@children << child
|
||||||
|
@ -37,6 +39,14 @@ module Spectator::SpecBuilder
|
||||||
@around_each_hooks << hook
|
@around_each_hooks << hook
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_pre_condition(hook : TestMetaMethod)
|
||||||
|
@pre_conditions << hook
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_post_condition(hook : TestMetaMethod)
|
||||||
|
@post_conditions << hook
|
||||||
|
end
|
||||||
|
|
||||||
private def build_hooks
|
private def build_hooks
|
||||||
ExampleHooks.new(
|
ExampleHooks.new(
|
||||||
@before_all_hooks.to_a,
|
@before_all_hooks.to_a,
|
||||||
|
@ -46,5 +56,12 @@ module Spectator::SpecBuilder
|
||||||
@around_each_hooks.to_a
|
@around_each_hooks.to_a
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def build_conditions
|
||||||
|
ExampleConditions.new(
|
||||||
|
@pre_conditions.to_a,
|
||||||
|
@post_conditions.to_a
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,7 +7,7 @@ module Spectator::SpecBuilder
|
||||||
end
|
end
|
||||||
|
|
||||||
def build(parent_group)
|
def build(parent_group)
|
||||||
context = TestContext.new(parent_group.context, build_hooks, parent_group.context.values)
|
context = TestContext.new(parent_group.context, build_hooks, build_conditions, parent_group.context.values)
|
||||||
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
||||||
group.children = children.map do |child|
|
group.children = children.map do |child|
|
||||||
child.build(group).as(ExampleComponent)
|
child.build(group).as(ExampleComponent)
|
||||||
|
|
|
@ -4,7 +4,7 @@ require "./example_group_builder"
|
||||||
module Spectator::SpecBuilder
|
module Spectator::SpecBuilder
|
||||||
class RootExampleGroupBuilder < ExampleGroupBuilder
|
class RootExampleGroupBuilder < ExampleGroupBuilder
|
||||||
def build
|
def build
|
||||||
context = TestContext.new(nil, build_hooks, TestValues.empty)
|
context = TestContext.new(nil, build_hooks, build_conditions, TestValues.empty)
|
||||||
RootExampleGroup.new(context).tap do |group|
|
RootExampleGroup.new(context).tap do |group|
|
||||||
group.children = children.map do |child|
|
group.children = children.map do |child|
|
||||||
child.build(group).as(ExampleComponent)
|
child.build(group).as(ExampleComponent)
|
||||||
|
|
|
@ -9,7 +9,7 @@ module Spectator::SpecBuilder
|
||||||
def build(parent_group)
|
def build(parent_group)
|
||||||
values = parent_group.context.values
|
values = parent_group.context.values
|
||||||
collection = @collection_builder.call(values)
|
collection = @collection_builder.call(values)
|
||||||
context = TestContext.new(parent_group.context, build_hooks, values)
|
context = TestContext.new(parent_group.context, build_hooks, build_conditions, values)
|
||||||
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
||||||
group.children = collection.map do |element|
|
group.children = collection.map do |element|
|
||||||
build_sub_group(group, element).as(ExampleComponent)
|
build_sub_group(group, element).as(ExampleComponent)
|
||||||
|
@ -19,7 +19,7 @@ module Spectator::SpecBuilder
|
||||||
|
|
||||||
private def build_sub_group(parent_group, element)
|
private def build_sub_group(parent_group, element)
|
||||||
values = parent_group.context.values.add(@id, @description.to_s, element)
|
values = parent_group.context.values.add(@id, @description.to_s, element)
|
||||||
context = TestContext.new(parent_group.context, ExampleHooks.empty, values)
|
context = TestContext.new(parent_group.context, ExampleHooks.empty, ExampleConditions.empty, values)
|
||||||
NestedExampleGroup.new("#{@label} = #{element.inspect}", @source, parent_group, context).tap do |group|
|
NestedExampleGroup.new("#{@label} = #{element.inspect}", @source, parent_group, context).tap do |group|
|
||||||
group.children = children.map do |child|
|
group.children = children.map do |child|
|
||||||
child.build(group).as(ExampleComponent)
|
child.build(group).as(ExampleComponent)
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Spectator
|
||||||
class TestContext
|
class TestContext
|
||||||
getter values
|
getter values
|
||||||
|
|
||||||
def initialize(@parent : TestContext?, @hooks : ExampleHooks, @values : TestValues)
|
def initialize(@parent : TestContext?, @hooks : ExampleHooks, @conditions : ExampleConditions, @values : TestValues)
|
||||||
@before_all_hooks_run = false
|
@before_all_hooks_run = false
|
||||||
@after_all_hooks_run = false
|
@after_all_hooks_run = false
|
||||||
end
|
end
|
||||||
|
@ -56,5 +56,15 @@ module Spectator
|
||||||
wrapper
|
wrapper
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def run_pre_conditions(example)
|
||||||
|
@parent.try &.run_pre_conditions(example)
|
||||||
|
@conditions.run_pre_conditions(example.test_wrapper, example)
|
||||||
|
end
|
||||||
|
|
||||||
|
def run_post_conditions(example)
|
||||||
|
@conditions.run_post_conditions(example.test_wrapper, example)
|
||||||
|
@parent.try &.run_post_conditions(example)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue