Quickly get things somewhat working again

This commit is contained in:
Michael Miller 2019-09-12 20:16:41 -06:00
parent 64166d1c36
commit 08ee03b1de
11 changed files with 71 additions and 36 deletions

View file

@ -0,0 +1,10 @@
module Spectator::Builders
class ExampleBuilder
def initialize(@wrapper : TestWrapper)
end
def build(group)
RunnableExample.new(group, @wrapper).as(ExampleComponent)
end
end
end

View file

@ -0,0 +1,13 @@
require "./example_builder"
module Spectator::Builders
abstract class ExampleGroupBuilder
alias Child = NestedExampleGroupBuilder | ExampleBuilder
private getter children = [] of Child
def add_child(child : Child)
@children << child
end
end
end

View file

@ -1,8 +1,14 @@
require "./root_example_group_builder"
require "./nested_example_group_builder"
module Spectator::Builders module Spectator::Builders
struct ExampleGroupStack struct ExampleGroupStack
getter root = RootExampleGroupBuilder.new getter root
def initialize
@root = RootExampleGroupBuilder.new
@stack = Deque(ExampleGroupBuilder).new(1, @root) @stack = Deque(ExampleGroupBuilder).new(1, @root)
end
def current def current
@stack.last @stack.last

View file

@ -0,0 +1,16 @@
require "./example_group_builder"
module Spectator::Builders
class NestedExampleGroupBuilder < ExampleGroupBuilder
def initialize(@what : String | Symbol)
end
def build(group)
NestedExampleGroup.new(@what, group).tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)
end
end
end
end
end

View file

@ -0,0 +1,13 @@
require "./example_group_builder"
module Spectator::Builders
class RootExampleGroupBuilder < ExampleGroupBuilder
def build
RootExampleGroup.new.tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)
end
end
end
end
end

View file

@ -1,3 +1,5 @@
require "../builders/example_group_stack"
module Spectator::DSL module Spectator::DSL
# Global builder used to create the runtime instance of the spec. # Global builder used to create the runtime instance of the spec.
# The DSL methods call into this module to generate parts of the spec. # The DSL methods call into this module to generate parts of the spec.
@ -14,7 +16,7 @@ module Spectator::DSL
# See `NestedExampleGroupBuilder#initialize` for the arguments # See `NestedExampleGroupBuilder#initialize` for the arguments
# as arguments to this method are passed directly to it. # as arguments to this method are passed directly to it.
def start_group(*args) : Nil def start_group(*args) : Nil
group = NestedExampleGroupBuilder.new(*args) group = Builders::NestedExampleGroupBuilder.new(*args)
@@stack.push(group) @@stack.push(group)
end end
@ -88,7 +90,7 @@ module Spectator::DSL
# Builds the entire spec and returns it as a test suite. # Builds the entire spec and returns it as a test suite.
# This should be called only once after the entire spec has been defined. # This should be called only once after the entire spec has been defined.
protected def build(filter : ExampleFilter) : TestSuite protected def build(filter : ExampleFilter) : TestSuite
group = @@stack.root.build(Internals::SampleValues.empty) group = @@stack.root.build
TestSuite.new(group, filter) TestSuite.new(group, filter)
end end
end end

View file

@ -25,9 +25,11 @@ module Spectator
def symbolic? def symbolic?
description = @test_wrapper.description description = @test_wrapper.description
description.start_with?('#') || description.start_with?('.') description.starts_with?('#') || description.starts_with?('.')
end end
abstract def run_impl
# Runs the example code. # Runs the example code.
# A result is returned, which represents the outcome of the test. # A result is returned, which represents the outcome of the test.
# An example can be run only once. # An example can be run only once.
@ -39,9 +41,6 @@ module Spectator
@finished = true @finished = true
end end
# Implementation-specific for running the example code.
private abstract def run_impl : Result
# Creates the base of the example. # Creates the base of the example.
# The group should be the example group the example belongs to. # The group should be the example group the example belongs to.
def initialize(@group, @test_wrapper) def initialize(@group, @test_wrapper)

View file

@ -15,10 +15,6 @@ module Spectator
include Enumerable(ExampleComponent) include Enumerable(ExampleComponent)
include Iterable(ExampleComponent) include Iterable(ExampleComponent)
# Creates the example group.
def initialize
end
# Retrieves the children in the group. # Retrieves the children in the group.
# This only returns the direct descends (non-recursive). # This only returns the direct descends (non-recursive).
# The children must be set (with `#children=`) prior to calling this method. # The children must be set (with `#children=`) prior to calling this method.

View file

@ -16,28 +16,6 @@ module Spectator
# A captured result is returned. # A captured result is returned.
private def capture_result private def capture_result
ResultCapture.new.tap do |result| ResultCapture.new.tap do |result|
# Get the proc that will call around-each hooks and the example.
wrapper = wrap_run_example(result)
run_wrapper(wrapper)
end
end
private def run_wrapper(wrapper)
wrapper.call
rescue ex
# If an error occurs calling the wrapper,
# it means it came from the "around-each" hooks.
# This is because the test code is completely wrapped with a begin/rescue block.
raise Exception.new("Error encountered while running around hooks", ex)
end
# Creates a proc that runs the test code
# and captures the result.
private def wrap_run_example(result)
# Wrap the method that runs and captures
# the test code with the around-each hooks.
group.wrap_around_each_hooks do
run_example(result) run_example(result)
end end
end end

View file

@ -35,7 +35,7 @@ module Spectator
result = run_example(example).as(Result) result = run_example(example).as(Result)
results << result results << result
if @config.fail_fast? && result.is_a?(FailedResult) if @config.fail_fast? && result.is_a?(FailedResult)
example.group.run_after_all_hooks(ignore_unfinished: true) # TODO: example.group.run_after_all_hooks(ignore_unfinished: true)
break break
end end
end end

View file

@ -2,6 +2,8 @@ require "../spectator_test"
require "./source" require "./source"
module Spectator module Spectator
alias TestMethod = ::SpectatorTest ->
# Stores information about a end-user test. # Stores information about a end-user test.
# Used to instantiate tests and run them. # Used to instantiate tests and run them.
struct TestWrapper struct TestWrapper
@ -14,7 +16,7 @@ module Spectator
# Creates a wrapper for the test. # Creates a wrapper for the test.
# The *builder* creates an instance of the test. # The *builder* creates an instance of the test.
# The *runner* takes the test created by *builder* and runs it. # The *runner* takes the test created by *builder* and runs it.
def initialize(@description, @source, @builder : -> SpectatorTest, @runner : SpectatorTest ->) def initialize(@description, @source, @builder : -> ::SpectatorTest, @runner : TestMethod)
end end
# Instantiates and runs the test. # Instantiates and runs the test.