Populate previous_def/super with captured block args

The previous_def and super keywords do not propagate blocks.
See: https://github.com/crystal-lang/crystal/issues/10399
This works around the issue by populating arguments if the method uses a block.
This commit is contained in:
Michael Miller 2022-12-17 16:41:22 -07:00
parent b52593dbde
commit 65a4b8e756
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
2 changed files with 88 additions and 3 deletions

View file

@ -29,6 +29,15 @@ Spectator.describe "GitHub Issue #48" do
def union : Int32 | String
42.as(Int32 | String)
end
def capture(&block : -> T) forall T
block
end
def capture(thing : T, &block : T -> T) forall T
block.call(thing)
block
end
end
mock Test, make_nilable: nil
@ -95,4 +104,22 @@ Spectator.describe "GitHub Issue #48" do
allow(fake).to receive(:union).and_return(:test)
expect { fake.union }.to raise_error(TypeCastError, /Symbol/)
end
it "handles captured blocks" do
proc = ->{}
allow(fake).to receive(:capture).and_return(proc)
expect(fake.capture { nil }).to be(proc)
end
it "raises on type cast error with captured blocks" do
proc = ->{ 42 }
allow(fake).to receive(:capture).and_return(proc)
expect { fake.capture { "other" } }.to raise_error(TypeCastError, /Proc\(String\)/)
end
it "handles captured blocks with arguments" do
proc = ->(x : Int32) { x * 2 }
allow(fake).to receive(:capture).and_return(proc)
expect(fake.capture(5) { 5 }).to be(proc)
end
end