Clear stubs and recorded calls after example completes

This commit is contained in:
Michael Miller 2022-07-12 20:40:27 -06:00
parent 24eec64d64
commit 0e556c3d55
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
4 changed files with 58 additions and 12 deletions

View file

@ -94,11 +94,15 @@ module Spectator::DSL
end
found_tuple = found_tuples.last %}
{% if found_tuple %}
{{found_tuple[2].id}}.new({{**value_methods}})
{% else %}
::Spectator::LazyDouble.new({{name}}, {{**value_methods}})
{% end %}
begin
%double = {% if found_tuple %}
{{found_tuple[2].id}}.new({{**value_methods}})
{% else %}
::Spectator::LazyDouble.new({{name}}, {{**value_methods}})
{% end %}
::Spectator::Harness.current?.try(&.cleanup { %double._spectator_reset })
%double
end
end
# Instantiates a class double.
@ -148,11 +152,15 @@ module Spectator::DSL
end
found_tuple = found_tuples.last %}
{% if found_tuple %}
{{found_tuple[2].id}}
{% else %}
::Spectator::LazyDouble
{% end %}
begin
%double = {% if found_tuple %}
{{found_tuple[2].id}}
{% else %}
::Spectator::LazyDouble
{% end %}
::Spectator::Harness.current?.try(&.cleanup { %double._spectator_reset })
%double
end
end
# Defines or instantiates a double.
@ -286,11 +294,14 @@ module Spectator::DSL
found_tuple = found_tuples.last %}
{% if found_tuple %}
{{found_tuple[2].id}}.new.tap do |%mock|
begin
%mock = {{found_tuple[2].id}}.new
{% for key, value in value_methods %}
%stub{key} = ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}})
%mock._spectator_define_stub(%stub{key})
{% end %}
::Spectator::Harness.current?.try(&.cleanup { %mock._spectator_reset })
%mock
end
{% else %}
{% raise "Type `#{type.id}` must be previously mocked before attempting to instantiate." %}
@ -362,11 +373,14 @@ module Spectator::DSL
found_tuple = found_tuples.last %}
{% if found_tuple %}
{{found_tuple[2].id}}.tap do |%mock|
begin
%mock = {{found_tuple[2].id}}
{% for key, value in value_methods %}
%stub{key} = ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}})
%mock._spectator_define_stub(%stub{key})
{% end %}
::Spectator::Harness.current?.try(&.cleanup { %mock._spectator_reset })
%mock
end
{% else %}
{% raise "Type `#{type.id}` must be previously mocked before attempting to instantiate." %}

View file

@ -64,6 +64,7 @@ module Spectator
end
@deferred = Deque(->).new
@cleanup = Deque(->).new
@expectations = [] of Expectation
@aggregate : Array(Expectation)? = nil
@ -72,6 +73,7 @@ module Spectator
def run : Result
elapsed, error = capture { yield }
elapsed2, error2 = capture { run_deferred }
run_cleanup
translate(elapsed + elapsed2, error || error2)
end
@ -97,6 +99,13 @@ module Spectator
@deferred << block
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)
previous = @aggregate
@aggregate = aggregate = [] of Expectation
@ -168,5 +177,16 @@ module Spectator
Log.debug { "Running deferred operations" }
@deferred.each(&.call)
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

View file

@ -79,6 +79,12 @@ module Spectator
# Utility method returning the stubbed type's name formatted for user output.
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.
#
# The *method* must be a `Def`.

View file

@ -32,6 +32,12 @@ module Spectator
_spectator_calls.clear
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, &)
Log.trace { "Fallback for #{call} - call original" }
yield