mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Use responds_to? to ensure stub is cast correctly
This commit is contained in:
parent
9c1357da3f
commit
003be48591
3 changed files with 16 additions and 3 deletions
|
@ -57,9 +57,13 @@ module Spectator
|
|||
_spectator_cast_stub_value(%stub, %call, \{{Messages[{{call.name.symbolize}}.id]}}, \{{Messages[{{call.name.symbolize}}.id].resolve >= Nil}})
|
||||
\{% else %}
|
||||
# A method that was not defined during initialization was stubbed.
|
||||
# Even though all stubs will have a #value method, the compiler doesn't seem to agree.
|
||||
# Assert that it will (this should never fail).
|
||||
raise TypeCastError.new("Stub has no value") unless %stub.responds_to?(:value)
|
||||
|
||||
# Return the value of the stub as-is.
|
||||
# Might want to give a warning here, as this may produce a "bloated" union of all known stub types.
|
||||
%stub.as(::Spectator::ValueStub).value
|
||||
%stub.value
|
||||
\{% end %}
|
||||
else
|
||||
# A stub wasn't found, invoke the fallback logic.
|
||||
|
|
|
@ -60,9 +60,13 @@ module Spectator
|
|||
# Attempt to find a stub that satisfies the method call and arguments.
|
||||
if %stub = _spectator_find_stub(%call)
|
||||
# A method that was not defined during initialization was stubbed.
|
||||
# Even though all stubs will have a #value method, the compiler doesn't seem to agree.
|
||||
# Assert that it will (this should never fail).
|
||||
raise TypeCastError.new("Stub has no value") unless %stub.responds_to?(:value)
|
||||
|
||||
# Return the value of the stub as-is.
|
||||
# Might want to give a warning here, as this may produce a "bloated" union of all known stub types.
|
||||
%stub.as(::Spectator::ValueStub).value
|
||||
%stub.value
|
||||
else
|
||||
# A stub wasn't found, invoke the fallback logic.
|
||||
# Message received for a methods that isn't stubbed nor defined when initialized.
|
||||
|
|
|
@ -276,9 +276,14 @@ module Spectator
|
|||
%typed.value
|
||||
else
|
||||
# The stub couldn't be easily cast to match the return type.
|
||||
|
||||
# Even though all stubs will have a #value method, the compiler doesn't seem to agree.
|
||||
# Assert that it will (this should never fail).
|
||||
raise TypeCastError.new("Stub has no value") unless {{stub}}.responds_to?(:value)
|
||||
|
||||
# Get the value as-is from the stub.
|
||||
# This will be compiled as a union of all known stubbed value types.
|
||||
%value = {{stub}}.as(::Spectator::ValueStub).value
|
||||
%value = {{stub}}.value
|
||||
|
||||
# Attempt to cast the value to the method's return type.
|
||||
# If successful, which it will be in most cases, return it.
|
||||
|
|
Loading…
Reference in a new issue