Workaround for stubbing NoReturn method with Nil

Fixes spec/issues/github_issue_29_spec.cr:15
This commit is contained in:
Michael Miller 2021-07-10 12:54:09 -06:00
parent 82f26dbb91
commit 06f9f380c9
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD

View file

@ -83,7 +83,14 @@ module Spectator::Mocks
%harness.mocks.record_call(self, %call) %harness.mocks.record_call(self, %call)
if (%stub = %harness.mocks.find_stub(self, %call)) if (%stub = %harness.mocks.find_stub(self, %call))
if typeof({{original}}) == NoReturn if typeof({{original}}) == NoReturn
return %stub.call!(%args) { raise "Cannot call original implementation of {{name}} - it won't return." } # NOTE: This may break at some point if the Crystal compiler gets smarter.
# The `nil` after raise changes the return type of the method from `NoReturn` to `Nil`.
# Technically, the `nil` will never be reached and if the compiler realizes it,
# the return type could become `NoReturn`
return %stub.call!(%args) do
raise "Cannot call original implementation of {{name}} - it won't return."
nil
end
else else
return %stub.call!(%args) { {{original}} } return %stub.call!(%args) { {{original}} }
end end
@ -106,7 +113,14 @@ module Spectator::Mocks
%harness.mocks.record_call(self, %call) %harness.mocks.record_call(self, %call)
if (%stub = %harness.mocks.find_stub(self, %call)) if (%stub = %harness.mocks.find_stub(self, %call))
if typeof({{original}}) == NoReturn if typeof({{original}}) == NoReturn
return %stub.call!(%args) { raise "Cannot call original implementation of {{name}} - it won't return." } # NOTE: This may break at some point if the Crystal compiler gets smarter.
# The `nil` after raise changes the return type of the method from `NoReturn` to `Nil`.
# Technically, the `nil` will never be reached and if the compiler realizes it,
# the return type could become `NoReturn`
return %stub.call!(%args) do
raise "Cannot call original implementation of {{name}} - it won't return."
nil
end
else else
return %stub.call!(%args) { {{original}} { |*%ya| yield *%ya } } return %stub.call!(%args) { {{original}} { |*%ya| yield *%ya } }
end end