Test handling of blocks in doubles

This commit is contained in:
Michael Miller 2022-03-19 17:04:51 -06:00
parent 043ebd818f
commit 0c7f72dc78
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
4 changed files with 154 additions and 2 deletions

View file

@ -32,6 +32,28 @@ Spectator.describe "Double DSL" do
expect { dbl.baz(:xyz, 123, a: "XYZ") }.to raise_error(Spectator::UnexpectedMessage, /\(:xyz, 123, a: "XYZ"\)/)
end
end
context "blocks" do
it "supports blocks" do
aggregate_failures do
expect(dbl.foo { nil }).to eq("foobar")
expect(dbl.bar { nil }).to eq(42)
end
end
it "supports blocks and has non-union return types" do
aggregate_failures do
expect(dbl.foo { nil }).to compile_as(String)
expect(dbl.bar { nil }).to compile_as(Int32)
end
end
it "fails on undefined messages" do
expect do
dbl.baz { nil }
end.to raise_error(Spectator::UnexpectedMessage, /baz/)
end
end
end
context "block with stubs" do
@ -143,6 +165,28 @@ Spectator.describe "Double DSL" do
expect(dbl.baz(42)).to eq("block2")
end
end
context "methods accepting blocks" do
double(:test7) do
stub def foo
yield
end
stub def bar(& : Int32 -> String)
yield 42
end
end
subject(dbl) { double(:test7) }
it "defines the method and yields" do
expect(dbl.foo { :xyz }).to eq(:xyz)
end
it "matches methods with block argument type restrictions" do
expect(dbl.bar &.to_s).to eq("42")
end
end
end
describe "double naming" do

View file

@ -28,6 +28,26 @@ Spectator.describe "Null double DSL" do
it "returns self for unexpected messages" do
expect_null_double(dbl, dbl.baz)
end
context "blocks" do
it "supports blocks" do
aggregate_failures do
expect(dbl.foo { nil }).to eq("foobar")
expect(dbl.bar { nil }).to eq(42)
end
end
it "supports blocks and has non-union return types" do
aggregate_failures do
expect(dbl.foo { nil }).to compile_as(String)
expect(dbl.bar { nil }).to compile_as(Int32)
end
end
it "returns self on undefined messages" do
expect_null_double(dbl, dbl.baz { nil })
end
end
end
context "block with stubs" do
@ -139,5 +159,27 @@ Spectator.describe "Null double DSL" do
expect(dbl.baz(42)).to eq("block2")
end
end
context "methods accepting blocks" do
double(:test7) do
stub def foo
yield
end
stub def bar(& : Int32 -> String)
yield 42
end
end
subject(dbl) { double(:test7).as_null_object }
it "defines the method and yields" do
expect(dbl.foo { :xyz }).to eq(:xyz)
end
it "matches methods with block argument type restrictions" do
expect(dbl.bar &.to_s).to eq("42")
end
end
end
end

View file

@ -39,8 +39,26 @@ Spectator.describe Spectator::Double do
expect { dbl.baz }.to compile_as(Nil)
end
it "supports blocks" do
dbl.bar
context "blocks" do
it "supports blocks" do
aggregate_failures do
expect(dbl.foo { nil }).to eq(42)
expect(dbl.bar { nil }).to eq("baz")
end
end
it "supports blocks and has non-union return types" do
aggregate_failures do
expect(dbl.foo { nil }).to compile_as(Int32)
expect(dbl.bar { nil }).to compile_as(String)
end
end
it "fails on undefined messages" do
expect do
dbl.baz { nil }
end.to raise_error(Spectator::UnexpectedMessage, /baz/)
end
end
end
@ -208,6 +226,7 @@ Spectator.describe Spectator::Double do
context "without common object methods" do
Spectator::Double.define(TestDouble) do
abstract_stub abstract def foo(value) : String
abstract_stub abstract def foo(value, & : -> _) : String
end
let(stub) { Spectator::ValueStub.new(:foo, "bar", arguments).as(Spectator::Stub) }
@ -228,6 +247,10 @@ Spectator.describe Spectator::Double do
it "has a non-union return type" do
expect(dbl.foo("foobar")).to compile_as(String)
end
it "ignores the block argument if not in the constraint" do
expect(dbl.foo("foobar") { nil }).to eq("bar")
end
end
context "with common object methods" do
@ -291,5 +314,14 @@ Spectator.describe Spectator::Double do
expect(dbl.foo(:lucky)).to eq(7)
end
end
it "ignores the block argument if not in the constraint" do
dbl._spectator_define_stub(stub5)
dbl._spectator_define_stub(stub7)
aggregate_failures do
expect(dbl.foo { nil }).to eq(5)
expect(dbl.foo(:lucky) { nil }).to eq(7)
end
end
end
end

View file

@ -32,6 +32,26 @@ Spectator.describe Spectator::NullDouble do
expect(dbl.bar).to compile_as(String)
end
end
context "blocks" do
it "supports blocks" do
aggregate_failures do
expect(dbl.foo { nil }).to eq(42)
expect(dbl.bar { nil }).to eq("baz")
end
end
it "supports blocks and has non-union return types" do
aggregate_failures do
expect(dbl.foo { nil }).to compile_as(Int32)
expect(dbl.bar { nil }).to compile_as(String)
end
end
it "returns self on undefined messages" do
expect_null_double(dbl, dbl.baz { nil })
end
end
end
context "with abstract stubs and return type annotations" do
@ -168,6 +188,7 @@ Spectator.describe Spectator::NullDouble do
context "without common object methods" do
Spectator::NullDouble.define(TestDouble) do
abstract_stub abstract def foo(value)
abstract_stub abstract def foo(value, & : -> _)
end
let(stub) { Spectator::ValueStub.new(:foo, "bar", arguments).as(Spectator::Stub) }
@ -184,6 +205,10 @@ Spectator.describe Spectator::NullDouble do
it "returns self when argument count doesn't match" do
expect_null_double(dbl, dbl.foo)
end
it "ignores the block argument if not in the constraint" do
expect(dbl.foo("foobar") { nil }).to eq("bar")
end
end
context "with common object methods" do
@ -243,5 +268,14 @@ Spectator.describe Spectator::NullDouble do
expect(dbl.foo(:lucky)).to eq(7)
end
end
it "ignores the block argument if not in the constraint" do
dbl._spectator_define_stub(stub5)
dbl._spectator_define_stub(stub7)
aggregate_failures do
expect(dbl.foo { nil }).to eq(5)
expect(dbl.foo(:lucky) { nil }).to eq(7)
end
end
end
end