Fix detection of nullable return type from stubbed method

This commit is contained in:
Michael Miller 2022-05-15 00:19:03 -06:00
parent 380d721fad
commit 7923eb3ad0
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2

View file

@ -130,7 +130,7 @@ module Spectator
if %stub = _spectator_find_stub(%call) if %stub = _spectator_find_stub(%call)
# Cast the stub or return value to the expected type. # Cast the stub or return value to the expected type.
# This is necessary to match the expected return type of the original method. # This is necessary to match the expected return type of the original method.
_spectator_cast_stub_value(%stub, %call, typeof({{original}}), {{method.return_type && method.return_type.resolve < Nil || false}}) _spectator_cast_stub_value(%stub, %call, typeof({{original}}), {{method.return_type && method.return_type.resolve <= Nil || method.return_type.is_a?(Union) && method.return_type.types.map(&.resolve).includes?(Nil)}})
else else
# Delegate missing stub behavior to concrete type. # Delegate missing stub behavior to concrete type.
_spectator_stub_fallback(%call, typeof({{original}})) do _spectator_stub_fallback(%call, typeof({{original}})) do
@ -221,7 +221,7 @@ module Spectator
# This is necessary to match the expected return type of the original method. # This is necessary to match the expected return type of the original method.
{% if method.return_type %} {% if method.return_type %}
# Return type restriction takes priority since it can be a superset of the original implementation. # Return type restriction takes priority since it can be a superset of the original implementation.
_spectator_cast_stub_value(%stub, %call, {{method.return_type}}, {{method.return_type.resolve < Nil}}) _spectator_cast_stub_value(%stub, %call, {{method.return_type}}, {{method.return_type.resolve <= Nil || method.return_type.is_a?(Union) && method.return_type.types.map(&.resolve).includes?(Nil)}})
{% elsif !method.abstract? %} {% elsif !method.abstract? %}
# The method isn't abstract, infer the type it returns without calling it. # The method isn't abstract, infer the type it returns without calling it.
_spectator_cast_stub_value(%stub, %call, typeof({{original}})) _spectator_cast_stub_value(%stub, %call, typeof({{original}}))