Don't return nil for methods using NoReturn

This commit is contained in:
Michael Miller 2022-07-13 12:24:29 -06:00
parent 14d8c046f0
commit aa9ca7a98e
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
4 changed files with 96 additions and 2 deletions

View file

@ -114,6 +114,27 @@ Spectator.describe Spectator::Double do
end end
end end
context "with a method that uses NoReturn" do
Spectator::Double.define(NoReturnDouble) do
abstract_stub abstract def oops : NoReturn
end
subject(dbl) { NoReturnDouble.new }
it "raises a TypeCastError when using a value-based stub" do
stub = Spectator::ValueStub.new(:oops, nil).as(Spectator::Stub)
dbl._spectator_define_stub(stub)
expect { dbl.oops }.to raise_error(TypeCastError, /NoReturn/)
end
it "raises when using an exception stub" do
exception = ArgumentError.new("bogus")
stub = Spectator::ExceptionStub.new(:oops, exception).as(Spectator::Stub)
dbl._spectator_define_stub(stub)
expect { dbl.oops }.to raise_error(ArgumentError, "bogus")
end
end
context "with common object methods" do context "with common object methods" do
subject(dbl) do subject(dbl) do
EmptyDouble.new([ EmptyDouble.new([

View file

@ -400,6 +400,31 @@ Spectator.describe Spectator::Mock do
end end
end end
end end
context "with a method that uses NoReturn" do
abstract class Thing
abstract def oops : NoReturn
end
Spectator::Mock.define_subtype(:class, Thing, MockThing)
let(mock) { MockThing.new }
after_each { mock._spectator_clear_stubs }
it "raises a TypeCastError when using a value-based stub" do
stub = Spectator::ValueStub.new(:oops, nil).as(Spectator::Stub)
mock._spectator_define_stub(stub)
expect { mock.oops }.to raise_error(TypeCastError, /NoReturn/)
end
it "raises when using an exception stub" do
exception = ArgumentError.new("bogus")
stub = Spectator::ExceptionStub.new(:oops, exception).as(Spectator::Stub)
mock._spectator_define_stub(stub)
expect { mock.oops }.to raise_error(ArgumentError, "bogus")
end
end
end end
describe "#inject" do describe "#inject" do
@ -719,5 +744,32 @@ Spectator.describe Spectator::Mock do
end end
end end
end end
context "with a method that uses NoReturn" do
struct ::NoReturnThing
def oops : NoReturn
raise "oops"
end
end
Spectator::Mock.inject(:struct, ::NoReturnThing)
let(mock) { NoReturnThing.new }
after_each { mock._spectator_clear_stubs }
it "raises a TypeCastError when using a value-based stub" do
stub = Spectator::ValueStub.new(:oops, nil).as(Spectator::Stub)
mock._spectator_define_stub(stub)
expect { mock.oops }.to raise_error(TypeCastError, /NoReturn/)
end
it "raises when using an exception stub" do
exception = ArgumentError.new("bogus")
stub = Spectator::ExceptionStub.new(:oops, exception).as(Spectator::Stub)
mock._spectator_define_stub(stub)
expect { mock.oops }.to raise_error(ArgumentError, "bogus")
end
end
end end
end end

View file

@ -90,6 +90,27 @@ Spectator.describe Spectator::NullDouble do
end end
end end
context "with a method that uses NoReturn" do
Spectator::NullDouble.define(NoReturnDouble) do
abstract_stub abstract def oops : NoReturn
end
subject(dbl) { NoReturnDouble.new }
it "raises a TypeCastError when using a value-based stub" do
stub = Spectator::ValueStub.new(:oops, nil).as(Spectator::Stub)
dbl._spectator_define_stub(stub)
expect { dbl.oops }.to raise_error(TypeCastError, /NoReturn/)
end
it "raises when using an exception stub" do
exception = ArgumentError.new("bogus")
stub = Spectator::ExceptionStub.new(:oops, exception).as(Spectator::Stub)
dbl._spectator_define_stub(stub)
expect { dbl.oops }.to raise_error(ArgumentError, "bogus")
end
end
context "with common object methods" do context "with common object methods" do
subject(dbl) do subject(dbl) do
EmptyDouble.new([ EmptyDouble.new([

View file

@ -149,7 +149,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 || method.return_type.is_a?(Union) && method.return_type.types.map(&.resolve).includes?(Nil)}}) _spectator_cast_stub_value(%stub, %call, typeof({{original}}), {{method.return_type && method.return_type.resolve != NoReturn && 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
@ -241,7 +241,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 || method.return_type.is_a?(Union) && method.return_type.types.map(&.resolve).includes?(Nil)}}) _spectator_cast_stub_value(%stub, %call, {{method.return_type}}, {{method.return_type.resolve != NoReturn && 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}}))