shard-spectator/spec/spectator/mocks/double_spec.cr

319 lines
11 KiB
Crystal
Raw Normal View History

2022-03-04 05:10:09 +00:00
require "../../spec_helper"
Spectator.describe Spectator::Double do
2022-03-12 20:54:40 +00:00
Spectator::Double.define(EmptyDouble)
Spectator::Double.define(FooBarDouble, "dbl-name", foo: 42, bar: "baz")
2022-03-12 16:39:32 +00:00
2022-03-12 20:54:40 +00:00
# The subject `dbl` must be carefully used in sub-contexts, otherwise it pollutes parent scopes.
# This changes the type of `dbl` to `Double+`, which produces a union of methods and their return types.
2022-03-12 20:54:40 +00:00
context "plain double" do
subject(dbl) { FooBarDouble.new }
2022-03-04 05:10:09 +00:00
2022-03-12 20:54:40 +00:00
it "responds to defined messages" do
aggregate_failures do
expect(dbl.foo).to eq(42)
expect(dbl.bar).to eq("baz")
end
2022-03-04 05:10:09 +00:00
end
2022-03-12 20:54:40 +00:00
it "fails on undefined messages" do
expect { dbl.baz }.to raise_error(Spectator::UnexpectedMessage, /baz/)
end
2022-03-04 05:10:09 +00:00
2022-03-12 20:54:40 +00:00
it "reports the name in errors" do
expect { dbl.baz }.to raise_error(/"dbl-name"/)
end
2022-03-12 20:54:40 +00:00
it "reports arguments" do
expect { dbl.baz(123, "qux", field: :value) }.to raise_error(Spectator::UnexpectedMessage, /\(123, "qux", field: :value\)/)
end
it "has a non-union return type" do
aggregate_failures do
expect(dbl.foo).to compile_as(Int32)
expect(dbl.bar).to compile_as(String)
end
end
2022-03-04 05:31:40 +00:00
2022-03-12 20:54:40 +00:00
it "uses nil for undefined messages" do
expect { dbl.baz }.to compile_as(Nil)
2022-03-12 16:39:32 +00:00
end
2022-03-19 23:04:51 +00:00
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
2022-03-12 16:39:32 +00:00
end
2022-03-04 05:31:40 +00:00
context "without a double name" do
2022-03-12 20:54:40 +00:00
Spectator::Double.define(NamelessDouble, foo: 42)
2022-03-12 16:39:32 +00:00
2022-03-12 20:54:40 +00:00
subject(dbl) { NamelessDouble.new }
2022-03-04 05:31:40 +00:00
it "reports as anonymous" do
expect { dbl.baz }.to raise_error(/anonymous/i)
end
end
context "with abstract stubs and return type annotations" do
Spectator::Double.define(TestDouble) do
abstract_stub abstract def foo(value) : String
end
let(arguments) { Spectator::Arguments.capture(/foo/) }
let(stub) { Spectator::ValueStub.new(:foo, "bar", arguments).as(Spectator::Stub) }
subject(dbl) { TestDouble.new([stub]) }
it "enforces the return type" do
expect(dbl.foo("foobar")).to compile_as(String)
end
it "raises on non-matching arguments" do
expect { dbl.foo("bar") }.to raise_error(Spectator::UnexpectedMessage, /foo/)
end
it "raises on non-matching stub" do
stub = Spectator::ValueStub.new(:foo, 42, arguments).as(Spectator::Stub)
dbl._spectator_define_stub(stub)
expect { dbl.foo("foobar") }.to raise_error(TypeCastError, /String/)
end
end
context "with nillable return type annotations" do
Spectator::Double.define(TestDouble) do
abstract_stub abstract def foo : String?
abstract_stub abstract def bar : Nil
end
let(foo_stub) { Spectator::ValueStub.new(:foo, nil).as(Spectator::Stub) }
let(bar_stub) { Spectator::ValueStub.new(:bar, nil).as(Spectator::Stub) }
subject(dbl) { TestDouble.new([foo_stub, bar_stub]) }
it "doesn't raise on nil" do
aggregate_failures do
expect(dbl.foo).to be_nil
expect(dbl.bar).to be_nil
end
end
end
2022-03-04 05:10:09 +00:00
context "with common object methods" do
subject(dbl) do
2022-03-12 20:54:40 +00:00
EmptyDouble.new([
2022-03-31 02:44:43 +00:00
Spectator::ValueStub.new(:"!=", false),
Spectator::ValueStub.new(:"!~", false),
Spectator::ValueStub.new(:"==", true),
Spectator::ValueStub.new(:"===", true),
Spectator::ValueStub.new(:"=~", nil),
Spectator::ValueStub.new(:class, EmptyDouble),
Spectator::ValueStub.new(:dup, EmptyDouble.new),
2022-03-12 20:54:40 +00:00
Spectator::ValueStub.new(:"in?", true),
Spectator::ValueStub.new(:inspect, "inspect"),
2022-03-31 02:44:43 +00:00
Spectator::ValueStub.new(:itself, EmptyDouble.new),
Spectator::ValueStub.new(:"not_nil!", EmptyDouble.new),
2022-03-12 20:54:40 +00:00
Spectator::ValueStub.new(:pretty_inspect, "pretty_inspect"),
2022-03-31 02:44:43 +00:00
Spectator::ValueStub.new(:tap, EmptyDouble.new),
2022-03-12 20:54:40 +00:00
Spectator::ValueStub.new(:to_json, "to_json"),
Spectator::ValueStub.new(:to_pretty_json, "to_pretty_json"),
Spectator::ValueStub.new(:to_s, "to_s"),
Spectator::ValueStub.new(:to_yaml, "to_yaml"),
2022-03-31 02:44:43 +00:00
Spectator::ValueStub.new(:try, nil),
2022-03-12 20:54:40 +00:00
Spectator::ValueStub.new(:object_id, 42_u64),
Spectator::ValueStub.new(:"same?", true),
2022-03-12 20:54:40 +00:00
] of Spectator::Stub)
2022-03-04 05:10:09 +00:00
end
it "responds with defined messages" do
aggregate_failures do
2022-03-31 02:44:43 +00:00
expect(dbl.!=(42)).to be_false
expect(dbl.!~(42)).to be_false
expect(dbl.==(42)).to be_true
expect(dbl.===(42)).to be_true
expect(dbl.=~(42)).to be_nil
expect(dbl.class).to eq(EmptyDouble)
expect(dbl.dup).to be_a(EmptyDouble)
expect(dbl.in?([42])).to eq(true)
2022-03-04 05:10:09 +00:00
expect(dbl.in?(1, 2, 3)).to eq(true)
expect(dbl.inspect).to eq("inspect")
2022-03-31 02:44:43 +00:00
expect(dbl.itself).to be_a(EmptyDouble)
expect(dbl.not_nil!).to be_a(EmptyDouble)
2022-03-04 05:10:09 +00:00
expect(dbl.pretty_inspect).to eq("pretty_inspect")
2022-03-31 02:44:43 +00:00
expect(dbl.tap { nil }).to be_a(EmptyDouble)
2022-03-04 05:10:09 +00:00
expect(dbl.to_json).to eq("to_json")
expect(dbl.to_pretty_json).to eq("to_pretty_json")
expect(dbl.to_s).to eq("to_s")
expect(dbl.to_yaml).to eq("to_yaml")
2022-03-31 02:44:43 +00:00
expect(dbl.try { nil }).to be_nil
2022-03-04 05:10:09 +00:00
expect(dbl.object_id).to eq(42_u64)
2022-03-31 02:44:43 +00:00
expect(dbl.same?(dbl)).to be_true
expect(dbl.same?(nil)).to be_true
2022-03-04 05:10:09 +00:00
end
end
2022-03-12 16:39:32 +00:00
it "has a non-union return type" do
expect(dbl.inspect).to compile_as(String)
end
2022-03-04 05:10:09 +00:00
end
context "without common object methods" do
2022-03-12 20:54:40 +00:00
subject(dbl) { EmptyDouble.new }
2022-03-04 05:10:09 +00:00
2022-03-31 02:28:36 +00:00
it "returns original implementation with undefined messages" do
2022-03-04 05:10:09 +00:00
io = IO::Memory.new
pp = PrettyPrint.new(io)
hasher = Crystal::Hasher.new
2022-03-04 05:10:09 +00:00
aggregate_failures do
2022-03-26 06:12:37 +00:00
expect(dbl.!=(42)).to be_true
expect(dbl.!~(42)).to be_true
expect(dbl.==(42)).to be_false
expect(dbl.===(42)).to be_false
2022-03-31 02:28:36 +00:00
expect(dbl.=~(42)).to be_nil
expect(dbl.class).to eq(EmptyDouble)
expect(dbl.dup).to be_a(EmptyDouble)
expect(dbl.hash(hasher)).to be_a(Crystal::Hasher)
expect(dbl.hash).to be_a(UInt64)
2022-03-26 06:12:37 +00:00
expect(dbl.in?([42])).to be_false
expect(dbl.in?(1, 2, 3)).to be_false
2022-03-31 02:28:36 +00:00
expect(dbl.inspect).to contain("EmptyDouble")
2022-03-26 06:12:37 +00:00
expect(dbl.itself).to be(dbl)
expect(dbl.not_nil!).to be(dbl)
2022-03-31 02:28:36 +00:00
expect(dbl.pretty_inspect).to contain("EmptyDouble")
2022-03-26 06:12:37 +00:00
expect(dbl.pretty_print(pp)).to be_nil
expect(dbl.tap { nil }).to be(dbl)
2022-03-31 02:28:36 +00:00
expect(dbl.to_s).to contain("EmptyDouble")
2022-03-26 06:12:37 +00:00
expect(dbl.to_s(io)).to be_nil
expect(dbl.try { nil }).to be_nil
2022-03-31 02:28:36 +00:00
expect(dbl.object_id).to be_a(UInt64)
2022-03-26 06:12:37 +00:00
expect(dbl.same?(dbl)).to be_true
expect(dbl.same?(nil)).to be_false
2022-03-04 05:10:09 +00:00
end
end
2022-03-31 02:28:36 +00:00
it "reports arguments when they don't match" do
expect { dbl.same?(123, :xyz) }.to raise_error(Spectator::UnexpectedMessage, /\(123, :xyz\)/)
end
2022-03-04 05:10:09 +00:00
end
2022-03-05 22:00:11 +00:00
context "with arguments constraints" do
let(arguments) { Spectator::Arguments.capture(/foo/) }
2022-03-12 20:54:40 +00:00
context "without common object methods" do
Spectator::Double.define(TestDouble) do
abstract_stub abstract def foo(value) : String
2022-03-19 23:04:51 +00:00
abstract_stub abstract def foo(value, & : -> _) : String
2022-03-12 20:54:40 +00:00
end
2022-03-05 22:00:11 +00:00
2022-03-12 20:54:40 +00:00
let(stub) { Spectator::ValueStub.new(:foo, "bar", arguments).as(Spectator::Stub) }
subject(dbl) { TestDouble.new([stub]) }
2022-03-05 22:00:11 +00:00
2022-03-12 20:54:40 +00:00
it "returns the response when constraint satisfied" do
expect(dbl.foo("foobar")).to eq("bar")
end
2022-03-05 22:39:35 +00:00
2022-03-12 20:54:40 +00:00
it "raises an error when constraint unsatisfied" do
expect { dbl.foo("baz") }.to raise_error(Spectator::UnexpectedMessage)
end
it "raises an error when argument count doesn't match" do
expect { dbl.foo }.to raise_error(Spectator::UnexpectedMessage)
end
it "has a non-union return type" do
expect(dbl.foo("foobar")).to compile_as(String)
end
2022-03-19 23:04:51 +00:00
it "ignores the block argument if not in the constraint" do
expect(dbl.foo("foobar") { nil }).to eq("bar")
end
2022-03-12 16:39:32 +00:00
end
2022-03-05 22:39:35 +00:00
context "with common object methods" do
2022-03-12 20:54:40 +00:00
Spectator::Double.define(TestDouble) do
stub abstract def same?(other : Reference) : Bool
end
2022-03-06 03:09:56 +00:00
let(stub) { Spectator::ValueStub.new(:"same?", true, arguments).as(Spectator::Stub) }
2022-03-12 20:54:40 +00:00
subject(dbl) { TestDouble.new([stub]) }
2022-03-05 22:39:35 +00:00
it "returns the response when constraint satisfied" do
expect(dbl.same?("foobar")).to eq(true)
end
it "raises an error when constraint unsatisfied" do
2022-03-12 20:54:40 +00:00
expect { dbl.same?("baz") }.to raise_error(Spectator::UnexpectedMessage)
2022-03-05 22:39:35 +00:00
end
it "raises an error when argument count doesn't match" do
expect { dbl.same? }.to raise_error(Spectator::UnexpectedMessage)
end
2022-03-12 16:39:32 +00:00
it "has a non-union return type" do
expect(dbl.same?("foobar")).to compile_as(Bool)
end
2022-03-05 22:39:35 +00:00
end
2022-03-05 22:00:11 +00:00
end
2022-03-12 21:31:19 +00:00
describe "#_spectator_define_stub" do
subject(dbl) { FooBarDouble.new }
let(stub3) { Spectator::ValueStub.new(:foo, 3) }
let(stub5) { Spectator::ValueStub.new(:foo, 5) }
let(stub7) { Spectator::ValueStub.new(:foo, 7, Spectator::Arguments.capture(:lucky)) }
it "overrides an existing method" do
expect { dbl._spectator_define_stub(stub3) }.to change { dbl.foo }.from(42).to(3)
end
it "replaces an existing stub" do
dbl._spectator_define_stub(stub3)
expect { dbl._spectator_define_stub(stub5) }.to change { dbl.foo }.from(3).to(5)
end
it "doesn't affect other methods" do
expect { dbl._spectator_define_stub(stub5) }.to_not change { dbl.bar }
end
it "picks the correct stub based on arguments" do
dbl._spectator_define_stub(stub5)
dbl._spectator_define_stub(stub7)
aggregate_failures do
expect(dbl.foo).to eq(5)
expect(dbl.foo(:lucky)).to eq(7)
end
end
it "only uses a stub if an argument constraint is met" do
dbl._spectator_define_stub(stub7)
aggregate_failures do
expect(dbl.foo).to eq(42)
expect(dbl.foo(:lucky)).to eq(7)
end
end
2022-03-19 23:04:51 +00:00
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
2022-03-12 21:31:19 +00:00
end
2022-03-04 05:10:09 +00:00
end