mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Clear stubs and recorded calls after example completes
This commit is contained in:
parent
24eec64d64
commit
0e556c3d55
4 changed files with 58 additions and 12 deletions
|
@ -94,11 +94,15 @@ module Spectator::DSL
|
||||||
end
|
end
|
||||||
found_tuple = found_tuples.last %}
|
found_tuple = found_tuples.last %}
|
||||||
|
|
||||||
{% if found_tuple %}
|
begin
|
||||||
|
%double = {% if found_tuple %}
|
||||||
{{found_tuple[2].id}}.new({{**value_methods}})
|
{{found_tuple[2].id}}.new({{**value_methods}})
|
||||||
{% else %}
|
{% else %}
|
||||||
::Spectator::LazyDouble.new({{name}}, {{**value_methods}})
|
::Spectator::LazyDouble.new({{name}}, {{**value_methods}})
|
||||||
{% end %}
|
{% end %}
|
||||||
|
::Spectator::Harness.current?.try(&.cleanup { %double._spectator_reset })
|
||||||
|
%double
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Instantiates a class double.
|
# Instantiates a class double.
|
||||||
|
@ -148,11 +152,15 @@ module Spectator::DSL
|
||||||
end
|
end
|
||||||
found_tuple = found_tuples.last %}
|
found_tuple = found_tuples.last %}
|
||||||
|
|
||||||
{% if found_tuple %}
|
begin
|
||||||
|
%double = {% if found_tuple %}
|
||||||
{{found_tuple[2].id}}
|
{{found_tuple[2].id}}
|
||||||
{% else %}
|
{% else %}
|
||||||
::Spectator::LazyDouble
|
::Spectator::LazyDouble
|
||||||
{% end %}
|
{% end %}
|
||||||
|
::Spectator::Harness.current?.try(&.cleanup { %double._spectator_reset })
|
||||||
|
%double
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Defines or instantiates a double.
|
# Defines or instantiates a double.
|
||||||
|
@ -286,11 +294,14 @@ module Spectator::DSL
|
||||||
found_tuple = found_tuples.last %}
|
found_tuple = found_tuples.last %}
|
||||||
|
|
||||||
{% if found_tuple %}
|
{% if found_tuple %}
|
||||||
{{found_tuple[2].id}}.new.tap do |%mock|
|
begin
|
||||||
|
%mock = {{found_tuple[2].id}}.new
|
||||||
{% for key, value in value_methods %}
|
{% for key, value in value_methods %}
|
||||||
%stub{key} = ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}})
|
%stub{key} = ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}})
|
||||||
%mock._spectator_define_stub(%stub{key})
|
%mock._spectator_define_stub(%stub{key})
|
||||||
{% end %}
|
{% end %}
|
||||||
|
::Spectator::Harness.current?.try(&.cleanup { %mock._spectator_reset })
|
||||||
|
%mock
|
||||||
end
|
end
|
||||||
{% else %}
|
{% else %}
|
||||||
{% raise "Type `#{type.id}` must be previously mocked before attempting to instantiate." %}
|
{% raise "Type `#{type.id}` must be previously mocked before attempting to instantiate." %}
|
||||||
|
@ -362,11 +373,14 @@ module Spectator::DSL
|
||||||
found_tuple = found_tuples.last %}
|
found_tuple = found_tuples.last %}
|
||||||
|
|
||||||
{% if found_tuple %}
|
{% if found_tuple %}
|
||||||
{{found_tuple[2].id}}.tap do |%mock|
|
begin
|
||||||
|
%mock = {{found_tuple[2].id}}
|
||||||
{% for key, value in value_methods %}
|
{% for key, value in value_methods %}
|
||||||
%stub{key} = ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}})
|
%stub{key} = ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}})
|
||||||
%mock._spectator_define_stub(%stub{key})
|
%mock._spectator_define_stub(%stub{key})
|
||||||
{% end %}
|
{% end %}
|
||||||
|
::Spectator::Harness.current?.try(&.cleanup { %mock._spectator_reset })
|
||||||
|
%mock
|
||||||
end
|
end
|
||||||
{% else %}
|
{% else %}
|
||||||
{% raise "Type `#{type.id}` must be previously mocked before attempting to instantiate." %}
|
{% raise "Type `#{type.id}` must be previously mocked before attempting to instantiate." %}
|
||||||
|
|
|
@ -64,6 +64,7 @@ module Spectator
|
||||||
end
|
end
|
||||||
|
|
||||||
@deferred = Deque(->).new
|
@deferred = Deque(->).new
|
||||||
|
@cleanup = Deque(->).new
|
||||||
@expectations = [] of Expectation
|
@expectations = [] of Expectation
|
||||||
@aggregate : Array(Expectation)? = nil
|
@aggregate : Array(Expectation)? = nil
|
||||||
|
|
||||||
|
@ -72,6 +73,7 @@ module Spectator
|
||||||
def run : Result
|
def run : Result
|
||||||
elapsed, error = capture { yield }
|
elapsed, error = capture { yield }
|
||||||
elapsed2, error2 = capture { run_deferred }
|
elapsed2, error2 = capture { run_deferred }
|
||||||
|
run_cleanup
|
||||||
translate(elapsed + elapsed2, error || error2)
|
translate(elapsed + elapsed2, error || error2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -97,6 +99,13 @@ module Spectator
|
||||||
@deferred << block
|
@deferred << block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Stores a block of code to be executed at cleanup.
|
||||||
|
# Cleanup is run after everything else, even deferred blocks.
|
||||||
|
# Each cleanup step is wrapped in error handling so that one failure doesn't block the next ones.
|
||||||
|
def cleanup(&block) : Nil
|
||||||
|
@cleanup << block
|
||||||
|
end
|
||||||
|
|
||||||
def aggregate_failures(label = nil)
|
def aggregate_failures(label = nil)
|
||||||
previous = @aggregate
|
previous = @aggregate
|
||||||
@aggregate = aggregate = [] of Expectation
|
@aggregate = aggregate = [] of Expectation
|
||||||
|
@ -168,5 +177,16 @@ module Spectator
|
||||||
Log.debug { "Running deferred operations" }
|
Log.debug { "Running deferred operations" }
|
||||||
@deferred.each(&.call)
|
@deferred.each(&.call)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Invokes all cleanup callbacks.
|
||||||
|
# Each callback is wrapped with error handling.
|
||||||
|
private def run_cleanup
|
||||||
|
Log.debug { "Running cleanup" }
|
||||||
|
@cleanup.each do |callback|
|
||||||
|
callback.call
|
||||||
|
rescue e
|
||||||
|
Log.error(exception: e) { "Encountered error during cleanup" }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -79,6 +79,12 @@ module Spectator
|
||||||
# Utility method returning the stubbed type's name formatted for user output.
|
# Utility method returning the stubbed type's name formatted for user output.
|
||||||
abstract def _spectator_stubbed_name : String
|
abstract def _spectator_stubbed_name : String
|
||||||
|
|
||||||
|
# Clears all previously defined calls and stubs.
|
||||||
|
def _spectator_reset : Nil
|
||||||
|
_spectator_clear_calls
|
||||||
|
_spectator_clear_stubs
|
||||||
|
end
|
||||||
|
|
||||||
# Redefines a method to accept stubs and provides a default response.
|
# Redefines a method to accept stubs and provides a default response.
|
||||||
#
|
#
|
||||||
# The *method* must be a `Def`.
|
# The *method* must be a `Def`.
|
||||||
|
|
|
@ -32,6 +32,12 @@ module Spectator
|
||||||
_spectator_calls.clear
|
_spectator_calls.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Clears all previously defined calls and stubs.
|
||||||
|
def _spectator_reset : Nil
|
||||||
|
_spectator_clear_calls
|
||||||
|
_spectator_clear_stubs
|
||||||
|
end
|
||||||
|
|
||||||
def _spectator_stub_fallback(call : MethodCall, &)
|
def _spectator_stub_fallback(call : MethodCall, &)
|
||||||
Log.trace { "Fallback for #{call} - call original" }
|
Log.trace { "Fallback for #{call} - call original" }
|
||||||
yield
|
yield
|
||||||
|
|
Loading…
Reference in a new issue