Fix cast to nil when nil is expected

Resolves `TypeCastError: The return type of stub #... : Nil at ...:##
doesn't match the expected type Nil`
This commit is contained in:
Michael Miller 2019-11-09 21:30:59 -07:00
parent e9f7e65ac9
commit 17695d35cf
6 changed files with 17 additions and 22 deletions

View file

@ -38,7 +38,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
%stub.call(%args, typeof(%method({{args.splat}})))
%stub.call!(%args, typeof(%method({{args.splat}})))
else
%method({{args.splat}})
end
@ -49,7 +49,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
%stub.call(%args, typeof(%method({{args.splat}}) { |*%ya| yield *%ya }))
%stub.call!(%args, typeof(%method({{args.splat}}) { |*%ya| yield *%ya }))
else
%method({{args.splat}}) do |*%yield_args|
yield *%yield_args

View file

@ -12,6 +12,15 @@ module Spectator::Mocks
abstract def call(args : GenericArguments(T, NT), rt : RT.class) forall T, NT, RT
def call!(args : GenericArguments(T, NT), rt : RT.class) : RT forall T, NT, RT
value = call(args, rt)
if value.is_a?(RT)
value.as(RT)
else
raise TypeCastError.new("The return type of stub #{self} doesn't match the expected type #{RT}")
end
end
def to_s(io)
io << '#'
io << @name

View file

@ -4,12 +4,8 @@ require "./value_method_stub"
module Spectator::Mocks
class NilMethodStub < GenericMethodStub(Nil)
def call(_args : GenericArguments(T, NT), rt : RT.class) forall T, NT, RT
if (cast = nil.as?(RT))
cast
else
raise "The return type of stub #{self} doesn't match the expected type #{RT}"
end
def call(_args : GenericArguments(T, NT), _rt : RT.class) forall T, NT, RT
nil
end
def and_return(value : T) forall T

View file

@ -8,12 +8,7 @@ module Spectator::Mocks
end
def call(_args : GenericArguments(T, NT), rt : RT.class) forall T, NT, RT
result = @proc.call
if (cast = result.as?(RT))
cast
else
raise "The return type of stub #{self} doesn't match the expected type #{RT}"
end
@proc.call
end
end
end

View file

@ -38,7 +38,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
%stub.call(%args, typeof({{original}}({{args.splat}})))
%stub.call!(%args, typeof({{original}}({{args.splat}})))
else
{{original}}({{args.splat}})
end
@ -49,7 +49,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
%stub.call(%args, typeof({{original}}({{args.splat}}) { |*%ya| yield *%ya }))
%stub.call!(%args, typeof({{original}}({{args.splat}}) { |*%ya| yield *%ya }))
else
{{original}}({{args.splat}}) do |*%yield_args|
yield *%yield_args

View file

@ -8,12 +8,7 @@ module Spectator::Mocks
end
def call(_args : GenericArguments(T, NT), rt : RT.class) forall T, NT, RT
result = @value
if (cast = result.as?(RT))
cast
else
raise "The return type of stub #{self} doesn't match the expected type #{RT}"
end
@value
end
end
end