Use TestWrapper in Example classes

This commit is contained in:
Michael Miller 2019-09-08 10:38:26 -06:00
parent de8f298676
commit a178db05ac
4 changed files with 15 additions and 13 deletions

View file

@ -2,7 +2,7 @@ require "./example_component"
module Spectator module Spectator
# Base class for all types of examples. # Base class for all types of examples.
# Concrete types must implement the `#run_impl, `#what`, `#instance`, and `#source` methods. # Concrete types must implement the `#run_impl` method.
abstract class Example < ExampleComponent abstract class Example < ExampleComponent
# Indicates whether the example has already been run. # Indicates whether the example has already been run.
getter? finished = false getter? finished = false
@ -11,10 +11,16 @@ module Spectator
getter group : ExampleGroup getter group : ExampleGroup
# Retrieves the internal wrapped instance. # Retrieves the internal wrapped instance.
abstract def instance private getter @test_wrapper : TestWrapper
# Source where the example originated from. # Source where the example originated from.
abstract def source : Source def source
@test_wrapper.source
end
def what
@test_wrapper.description
end
# 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.
@ -31,7 +37,7 @@ module Spectator
# 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) def initialize(@group, @test_wrapper)
end end
# Indicates there is only one example to run. # Indicates there is only one example to run.

View file

@ -3,6 +3,7 @@ module Spectator
# This is used as the base node type for the composite design pattern. # This is used as the base node type for the composite design pattern.
abstract class ExampleComponent abstract class ExampleComponent
# Text that describes the context or test. # Text that describes the context or test.
# TODO: Rename to description.
abstract def what : String abstract def what : String
# Indicates whether the example (or group) has been completely run. # Indicates whether the example (or group) has been completely run.

View file

@ -3,7 +3,7 @@ require "./example"
module Spectator module Spectator
# Common class for all examples marked as pending. # Common class for all examples marked as pending.
# This class will not run example code. # This class will not run example code.
abstract class PendingExample < Example class PendingExample < Example
# Returns a pending result. # Returns a pending result.
private def run_impl private def run_impl
PendingResult.new(self) PendingResult.new(self)

View file

@ -1,11 +1,9 @@
require "./example" require "./example"
module Spectator module Spectator
# Common base for all examples that can be run. # Includes all the logic for running example hooks,
# This class includes all the logic for running example hooks,
# the example code, and capturing a result. # the example code, and capturing a result.
# Sub-classes need to implement the `#what` and `#run_instance` methods. class RunnableExample < Example
abstract class RunnableExample < Example
# Runs the example, hooks, and captures the result # Runs the example, hooks, and captures the result
# and translates to a usable result. # and translates to a usable result.
def run_impl def run_impl
@ -14,9 +12,6 @@ module Spectator
translate_result(result, expectations) translate_result(result, expectations)
end end
# Runs the actual test code.
private abstract def run_instance
# Runs all hooks and the example code. # Runs all hooks and the example code.
# A captured result is returned. # A captured result is returned.
private def capture_result private def capture_result
@ -52,7 +47,7 @@ module Spectator
# 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
run_instance # Actually run the example code. test_wrapper.run {} # Actually run the example code.
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