From aecdb514b31777078d71578fcdc784ceb9fb4997 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 12 Oct 2019 22:35:07 -0600 Subject: [PATCH] Implement basic and_return --- src/spectator/double.cr | 8 +++++++- src/spectator/generic_method_stub.cr | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/spectator/double.cr b/src/spectator/double.cr index 27de724..9507b1d 100644 --- a/src/spectator/double.cr +++ b/src/spectator/double.cr @@ -51,7 +51,7 @@ module Spectator call = ::Spectator::MethodCall.new({{name.symbolize}}, args, options) stub = @stubs.find(&.callable?(call)) if stub - stub.as(::Spectator::GenericMethodStub(typeof(%method(*args, **options)))).call(call) + stub.as(::Spectator::GenericMethodStub(typeof(%method(*args, **options) { |*yield_args| yield *yield_args }))).call(call) else %method(*args, **options) do |*yield_args| yield *yield_args @@ -66,6 +66,12 @@ module Spectator {{body.body}} {% else %} raise "Stubbed method called without being allowed" + # This code shouldn't be reached, but makes the compiler happy to have a matching type. + {% if definition.is_a?(TypeDeclaration) %} + %x = uninitialized {{definition.type}} + {% else %} + nil + {% end %} {% end %} end end diff --git a/src/spectator/generic_method_stub.cr b/src/spectator/generic_method_stub.cr index 78042b1..263d843 100644 --- a/src/spectator/generic_method_stub.cr +++ b/src/spectator/generic_method_stub.cr @@ -15,5 +15,9 @@ module Spectator def call(call : MethodCall) : ReturnType @proc.call end + + def and_return(value : T) forall T + GenericMethodStub(T).new(@name, @source, -> { value }) + end end end