From 4974054de7e429a168eb3c8ebfb6ed7ae9beae4a Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Oct 2020 11:23:51 -0600 Subject: [PATCH] Some code to run a spec --- src/spectator.cr | 2 +- src/spectator/example.cr | 2 +- src/spectator/example_group.cr | 6 ++++++ src/spectator/example_iterator.cr | 15 +++++++++------ src/spectator/spec.cr | 24 ++++++++++++++---------- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/spectator.cr b/src/spectator.cr index a6708af..89c8306 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -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. diff --git a/src/spectator/example.cr b/src/spectator/example.cr index e5607e2..b395748 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -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 diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index 80b6672..d6d5f84 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -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?) diff --git a/src/spectator/example_iterator.cr b/src/spectator/example_iterator.cr index b5dd3f3..217f0a0 100644 --- a/src/spectator/example_iterator.cr +++ b/src/spectator/example_iterator.cr @@ -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 diff --git a/src/spectator/spec.cr b/src/spectator/spec.cr index 90a2b6a..17218f9 100644 --- a/src/spectator/spec.cr +++ b/src/spectator/spec.cr @@ -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 - end + 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