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) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call) ::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(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 else
%method({{args.splat}}) %method({{args.splat}})
end end
@ -49,7 +49,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call) ::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(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 else
%method({{args.splat}}) do |*%yield_args| %method({{args.splat}}) do |*%yield_args|
yield *%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 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) def to_s(io)
io << '#' io << '#'
io << @name io << @name

View file

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

View file

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

View file

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

View file

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