Store type to reduce a bit of bloat

This commit is contained in:
Michael Miller 2023-01-23 16:02:30 -07:00
parent abbd6ffd71
commit a5e8f11e11
No known key found for this signature in database
GPG Key ID: AC78B32D30CE34A2
1 changed files with 4 additions and 3 deletions

View File

@ -537,6 +537,7 @@ module Spectator
# Get the value as-is from the stub.
# This will be compiled as a union of all known stubbed value types.
%value = {{stub}}.call({{call}})
%type = {{type}}
# Attempt to cast the value to the method's return type.
# If successful, which it will be in most cases, return it.
@ -547,12 +548,12 @@ module Spectator
%cast
{% elsif fail_cast == :raise %}
# Check if nil was returned by the stub and if its okay to return it.
if %value.nil? && {{type}}.nilable?
if %value.nil? && %type.nilable?
# Value was nil and nil is allowed to be returned.
%cast.as({{type}})
%type.cast(%cast)
elsif %cast.nil?
# The stubbed value was something else entirely and cannot be cast to the return type.
raise TypeCastError.new("#{_spectator_stubbed_name} received message #{ {{call}} } and is attempting to return a `#{%value.class}`, but returned type must be `#{ {{type}} }`.")
raise TypeCastError.new("#{_spectator_stubbed_name} received message #{ {{call}} } and is attempting to return a `#{%value.class}`, but returned type must be `#{%type}`.")
else
# Types match and value can be returned as cast type.
%cast