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
# Build the test spec and run it.
spec = ::Spectator::DSL::Builder.build
# Runner.new(suite, config).run
spec.run
true
rescue ex
# Catch all unhandled exceptions here.

View file

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

View file

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

View file

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

View file

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