Move common example runner code into new RunnableExample class

This commit is contained in:
Michael Miller 2018-09-14 22:17:16 -06:00
parent e4d46ca603
commit 56229cdc93
4 changed files with 59 additions and 35 deletions

View file

@ -139,7 +139,7 @@ module Spectator
end end
end end
class {{class_name.id}} < ::Spectator::Example class {{class_name.id}} < ::Spectator::RunnableExample
{% for given_var, i in given_vars %} {% for given_var, i in given_vars %}
@%var{i} : ValueWrapper @%var{i} : ValueWrapper
@ -155,20 +155,9 @@ module Spectator
{% end %} {% end %}
end end
def run protected def run_instance
context.run_before_all_hooks
context.run_before_each_hooks
begin
wrapper = context.wrap_around_each_hooks do
Example%example.new.%run({% for v, i in var_names %}%var{i}{% if i < var_names.size - 1 %}, {% end %}{% end %}) Example%example.new.%run({% for v, i in var_names %}%var{i}{% if i < var_names.size - 1 %}, {% end %}{% end %})
end end
wrapper.call
ensure
@finished = true
context.run_after_each_hooks
context.run_after_all_hooks
end
end
def description def description
{% if description.is_a?(StringLiteral) %} {% if description.is_a?(StringLiteral) %}

View file

@ -8,7 +8,7 @@ module Spectator
def initialize(@context) def initialize(@context)
end end
abstract def run : Nil abstract def run : ExampleResult
abstract def description : String abstract def description : String
end end
end end

View file

@ -0,0 +1,54 @@
require "./example"
module Spectator
abstract class RunnableExample < Example
def run
result = ResultCapture.new
context.run_before_all_hooks
context.run_before_each_hooks
begin
wrapped_capture_result(result).call
ensure
@finished = true
context.run_after_each_hooks
context.run_after_all_hooks
end
translate_result(result)
end
private def wrapped_capture_result(result)
context.wrap_around_each_hooks do
capture_result(result)
end
end
private def capture_result(result)
result.elapsed = Time.measure do
begin
run_instance
rescue ex
result.error = ex
end
end
end
private def translate_result(result)
case (error = result.error)
when Nil
SuccessfulExampleResult.new(self, result.elapsed)
when ExpectationFailedError
FailedExampleResult.new(self, result.elapsed, error)
else
ErroredExampleResult.new(self, result.elapsed, error)
end
end
protected abstract def run_instance
private class ResultCapture
property elapsed = Time::Span.new(nanoseconds: 0)
property error : Exception?
end
end
end

View file

@ -13,7 +13,7 @@ module Spectator
@reporter.start_suite @reporter.start_suite
results = @context.all_examples.map do |example| results = @context.all_examples.map do |example|
@reporter.start_example(example) @reporter.start_example(example)
run_example(example).tap do |result| example.run.tap do |result|
@reporter.end_example(result) @reporter.end_example(result)
end end
end end
@ -21,24 +21,5 @@ module Spectator
report = Report.new(results, elapsed) report = Report.new(results, elapsed)
@reporter.end_suite(report) @reporter.end_suite(report)
end end
private def run_example(example)
error = nil
elapsed = Time.measure do
begin
example.run
rescue ex
error = ex
end
end
case error
when Nil
SuccessfulExampleResult.new(example, elapsed)
when ExpectationFailedError
FailedExampleResult.new(example, elapsed, error)
else
ErroredExampleResult.new(example, elapsed, error)
end
end
end end
end end