mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
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:
parent
e9f7e65ac9
commit
17695d35cf
6 changed files with 17 additions and 22 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue