From a5e8f11e1188f2ec8acfbd7d1fba5a83833828ec Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 23 Jan 2023 16:02:30 -0700 Subject: [PATCH] Store type to reduce a bit of bloat --- src/spectator/mocks/stubbable.cr | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/spectator/mocks/stubbable.cr b/src/spectator/mocks/stubbable.cr index 3a14887..3385ad4 100644 --- a/src/spectator/mocks/stubbable.cr +++ b/src/spectator/mocks/stubbable.cr @@ -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