2022-12-14 01:22:22 +00:00
|
|
|
require "../spec_helper"
|
|
|
|
|
|
|
|
Spectator.describe "GitHub Issue #48" do
|
|
|
|
class Test
|
|
|
|
def return_this(thing : T) : T forall T
|
|
|
|
thing
|
|
|
|
end
|
|
|
|
|
|
|
|
def map(thing : T, & : T -> U) : U forall T, U
|
|
|
|
yield thing
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_nilable(thing : T) : T? forall T
|
|
|
|
thing.as(T?)
|
|
|
|
end
|
2022-12-14 05:48:21 +00:00
|
|
|
|
|
|
|
def itself : self
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def itself? : self?
|
|
|
|
self.as(self?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generic(thing : T) : Array(T) forall T
|
|
|
|
Array.new(100) { thing }
|
|
|
|
end
|
|
|
|
|
|
|
|
def union : Int32 | String
|
|
|
|
42.as(Int32 | String)
|
|
|
|
end
|
2022-12-14 01:22:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
mock Test, make_nilable: nil
|
|
|
|
|
|
|
|
let(fake) { mock(Test) }
|
|
|
|
|
|
|
|
it "handles free variables" do
|
|
|
|
allow(fake).to receive(:return_this).and_return("different")
|
|
|
|
expect(fake.return_this("test")).to eq("different")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises on type cast error with free variables" do
|
|
|
|
allow(fake).to receive(:return_this).and_return(42)
|
|
|
|
expect { fake.return_this("test") }.to raise_error(TypeCastError, /String/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles free variables with a block" do
|
|
|
|
allow(fake).to receive(:map).and_return("stub")
|
|
|
|
expect(fake.map(:mapped, &.to_s)).to eq("stub")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises on type cast error with a block and free variables" do
|
|
|
|
allow(fake).to receive(:map).and_return(42)
|
|
|
|
expect { fake.map(:mapped, &.to_s) }.to raise_error(TypeCastError, /String/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles nilable free variables" do
|
|
|
|
expect(fake.make_nilable("foo")).to be_nil
|
|
|
|
end
|
2022-12-14 05:48:21 +00:00
|
|
|
|
|
|
|
it "handles 'self' return type" do
|
|
|
|
not_self = mock(Test)
|
|
|
|
allow(fake).to receive(:itself).and_return(not_self)
|
|
|
|
expect(fake.itself).to be(not_self)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises on type cast error with 'self' return type" do
|
|
|
|
allow(fake).to receive(:itself).and_return(42)
|
|
|
|
expect { fake.itself }.to raise_error(TypeCastError, /#{class_mock(Test)}/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles nilable 'self' return type" do
|
|
|
|
not_self = mock(Test)
|
|
|
|
allow(fake).to receive(:itself?).and_return(not_self)
|
|
|
|
expect(fake.itself?).to be(not_self)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles generic return type" do
|
|
|
|
allow(fake).to receive(:generic).and_return([42])
|
|
|
|
expect(fake.generic(42)).to eq([42])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises on type cast error with generic return type" do
|
|
|
|
allow(fake).to receive(:generic).and_return("test")
|
|
|
|
expect { fake.generic(42) }.to raise_error(TypeCastError, /Array\(Int32\)/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles union return types" do
|
|
|
|
allow(fake).to receive(:union).and_return("test")
|
|
|
|
expect(fake.union).to eq("test")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises on type cast error with union return type" do
|
|
|
|
allow(fake).to receive(:union).and_return(:test)
|
|
|
|
expect { fake.union }.to raise_error(TypeCastError, /Symbol/)
|
|
|
|
end
|
2022-12-14 01:22:22 +00:00
|
|
|
end
|