Some code to run a spec

This commit is contained in:
Michael Miller 2020-10-17 11:23:51 -06:00
parent f71a8ac6a3
commit 4974054de7
No known key found for this signature in database
GPG Key ID: FB9F12F7C646A4AD
5 changed files with 31 additions and 18 deletions

View File

@ -95,7 +95,7 @@ module Spectator
private def run private def run
# Build the test spec and run it. # Build the test spec and run it.
spec = ::Spectator::DSL::Builder.build spec = ::Spectator::DSL::Builder.build
# Runner.new(suite, config).run spec.run
true true
rescue ex rescue ex
# Catch all unhandled exceptions here. # Catch all unhandled exceptions here.

View File

@ -28,7 +28,7 @@ module Spectator
# Returns the result of the execution. # Returns the result of the execution.
# The result will also be stored in `#result`. # The result will also be stored in `#result`.
def run : Result def run : Result
Spectator.debug_out("Running example #{example}") Spectator.debug_out("Running example #{self}")
@delegate.call(self) @delegate.call(self)
raise NotImplementedError.new("#run") raise NotImplementedError.new("#run")
end end

View File

@ -4,6 +4,7 @@ module Spectator
# Collection of examples and sub-groups. # Collection of examples and sub-groups.
class ExampleGroup < ExampleNode class ExampleGroup < ExampleNode
include Enumerable(ExampleNode) include Enumerable(ExampleNode)
include Iterable(ExampleNode)
@nodes = [] of ExampleNode @nodes = [] of ExampleNode
@ -22,6 +23,11 @@ module Spectator
@nodes.each { |node| yield node } @nodes.each { |node| yield node }
end end
# Returns an iterator for each (example and sub-group).
def each
@nodes.each
end
# Checks if all examples and sub-groups have finished. # Checks if all examples and sub-groups have finished.
def finished? : Bool def finished? : Bool
@nodes.all?(&.finished?) @nodes.all?(&.finished?)

View File

@ -1,3 +1,7 @@
require "./example"
require "./example_group"
require "./example_node"
module Spectator module Spectator
# Iterates through all examples in a group and its nested groups. # Iterates through all examples in a group and its nested groups.
class ExampleIterator class ExampleIterator
@ -5,12 +9,12 @@ module Spectator
# Stack that contains the iterators for each group. # Stack that contains the iterators for each group.
# A stack is used to track where in the tree this iterator is. # A stack is used to track where in the tree this iterator is.
@stack : Array(Iterator(ExampleComponent)) @stack : Array(Iterator(ExampleNode))
# Creates a new iterator. # Creates a new iterator.
# The *group* is the example group to iterate through. # The *group* is the example group to iterate through.
def initialize(@group : Iterable(ExampleComponent)) def initialize(@group : ExampleGroup)
iter = @group.each.as(Iterator(ExampleComponent)) iter = @group.each.as(Iterator(ExampleNode))
@stack = [iter] @stack = [iter]
end end
@ -22,8 +26,7 @@ module Spectator
# b. the stack is empty. # b. the stack is empty.
until @stack.empty? until @stack.empty?
# Retrieve the next "thing". # Retrieve the next "thing".
# This could be an `Example`, # This could be an `Example` or a group.
# or a group.
item = advance item = advance
# Return the item if it's an example. # Return the item if it's an example.
# Otherwise, advance and check the next one. # Otherwise, advance and check the next one.
@ -36,7 +39,7 @@ module Spectator
# Restart the iterator at the beginning. # Restart the iterator at the beginning.
def rewind def rewind
# Same code as `#initialize`, but return self. # Same code as `#initialize`, but return self.
iter = @group.each.as(Iterator(ExampleComponent)) iter = @group.each.as(Iterator(ExampleNode))
@stack = [iter] @stack = [iter]
self self
end end

View File

@ -1,20 +1,24 @@
require "./example" require "./example"
require "./example_group" require "./example_group"
require "./example_iterator"
module Spectator module Spectator
# Contains examples to be tested.
class Spec class Spec
include Enumerable(Example) def initialize(@root : ExampleGroup)
def initialize(@group : ExampleGroup)
end end
def each def run
@group.each do |node| examples = ExampleIterator.new(@root).to_a
if (example = node.as?(Example)) Runner.new(examples).run
yield example end
elsif (group = node.as?(ExampleGroup))
# TODO private struct Runner
end def initialize(@examples : Array(Example))
end
def run
@examples.each(&.run)
end end
end end
end end