mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Move common example runner code into new RunnableExample class
This commit is contained in:
parent
e4d46ca603
commit
56229cdc93
4 changed files with 59 additions and 35 deletions
|
@ -139,7 +139,7 @@ module Spectator
|
|||
end
|
||||
end
|
||||
|
||||
class {{class_name.id}} < ::Spectator::Example
|
||||
class {{class_name.id}} < ::Spectator::RunnableExample
|
||||
{% for given_var, i in given_vars %}
|
||||
@%var{i} : ValueWrapper
|
||||
|
||||
|
@ -155,19 +155,8 @@ module Spectator
|
|||
{% end %}
|
||||
end
|
||||
|
||||
def run
|
||||
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 %})
|
||||
end
|
||||
wrapper.call
|
||||
ensure
|
||||
@finished = true
|
||||
context.run_after_each_hooks
|
||||
context.run_after_all_hooks
|
||||
end
|
||||
protected def run_instance
|
||||
Example%example.new.%run({% for v, i in var_names %}%var{i}{% if i < var_names.size - 1 %}, {% end %}{% end %})
|
||||
end
|
||||
|
||||
def description
|
||||
|
|
|
@ -8,7 +8,7 @@ module Spectator
|
|||
def initialize(@context)
|
||||
end
|
||||
|
||||
abstract def run : Nil
|
||||
abstract def run : ExampleResult
|
||||
abstract def description : String
|
||||
end
|
||||
end
|
||||
|
|
54
src/spectator/runnable_example.cr
Normal file
54
src/spectator/runnable_example.cr
Normal 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
|
|
@ -13,7 +13,7 @@ module Spectator
|
|||
@reporter.start_suite
|
||||
results = @context.all_examples.map do |example|
|
||||
@reporter.start_example(example)
|
||||
run_example(example).tap do |result|
|
||||
example.run.tap do |result|
|
||||
@reporter.end_example(result)
|
||||
end
|
||||
end
|
||||
|
@ -21,24 +21,5 @@ module Spectator
|
|||
report = Report.new(results, elapsed)
|
||||
@reporter.end_suite(report)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue