Add with stub modifier

This commit is contained in:
Michael Miller 2022-07-10 17:31:13 -06:00
parent cd177dd2ae
commit 4aaed186c3
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
7 changed files with 85 additions and 0 deletions

View file

@ -48,4 +48,49 @@ Spectator.describe "Stub DSL", :smoke do
expect(captured).to eq(args)
end
end
describe "#with" do
context Spectator::MultiValueStub do
it "applies the stub with matching arguments" do
allow(dbl).to receive(:foo).and_return(1, 2, 3).with(Int32, bar: /baz/)
aggregate_failures do
expect(dbl.foo(3, bar: "foobarbaz")).to eq(1)
expect(dbl.foo).to eq(42)
expect(dbl.foo(5, bar: "barbaz")).to eq(2)
expect(dbl.foo(7, bar: "foobaz")).to eq(3)
expect(dbl.foo(11, bar: "baz")).to eq(3)
end
end
end
context Spectator::NullStub do
it "applies the stub with matching arguments" do
allow(dbl).to receive(:foo).with(Int32, bar: /baz/).and_return(1)
aggregate_failures do
expect(dbl.foo(3, bar: "foobarbaz")).to eq(1)
expect(dbl.foo).to eq(42)
end
end
it "changes to a proc stub" do
called = 0
allow(dbl).to receive(:foo).with(Int32, bar: /baz/) { called += 1 }
aggregate_failures do
expect { dbl.foo(3, bar: "foobarbaz") }.to change { called }.from(0).to(1)
expect(dbl.foo(5, bar: "baz")).to eq(2)
expect(dbl.foo).to eq(42)
end
end
end
context Spectator::ValueStub do
it "applies the stub with matching arguments" do
allow(dbl).to receive(:foo).and_return(1).with(Int32, bar: /baz/)
aggregate_failures do
expect(dbl.foo(3, bar: "foobarbaz")).to eq(1)
expect(dbl.foo).to eq(42)
end
end
end
end
end

View file

@ -11,6 +11,12 @@ module Spectator
raise @exception
end
# Returns a new stub with constrained arguments.
def with(*args, **kwargs)
constraint = Arguments.new(args, kwargs)
self.class.new(method, @exception, constraint, location)
end
# Creates the stub.
def initialize(method : Symbol, @exception : Exception, constraint : AbstractArguments? = nil, location : Location? = nil)
super(method, constraint, location)

View file

@ -15,6 +15,12 @@ module Spectator
end
end
# Returns a new stub with constrained arguments.
def with(*args, **kwargs)
constraint = Arguments.new(args, kwargs)
self.class.new(method, @values, constraint, location)
end
# Creates the stub.
def initialize(method : Symbol, @values : Array(T), constraint : AbstractArguments? = nil, location : Location? = nil)
super(method, constraint, location)

View file

@ -7,5 +7,11 @@ module Spectator
# Invokes the stubbed implementation.
def call(call : MethodCall) : Nil
end
# Returns a new stub with constrained arguments.
def with(*args, **kwargs)
constraint = Arguments.new(args, kwargs)
self.class.new(method, constraint, location)
end
end
end

View file

@ -10,6 +10,12 @@ module Spectator
@proc.call(call.arguments)
end
# Returns a new stub with constrained arguments.
def with(*args, **kwargs)
constraint = Arguments.new(args, kwargs)
self.class.new(method, @proc, constraint, location)
end
# Creates the stub.
def initialize(method : Symbol, @proc : Proc(AbstractArguments, T), constraint : AbstractArguments? = nil, location : Location? = nil)
super(method, constraint, location)
@ -20,4 +26,12 @@ module Spectator
initialize(method, block, constraint, location)
end
end
module StubModifiers
# Returns a new stub with an argument constraint.
def with(*args, **kwargs, &block : AbstractArguments -> T) forall T
constraint = Arguments.new(args, kwargs)
ProcStub(T).new(method, block, constraint, location)
end
end
end

View file

@ -1,5 +1,7 @@
module Spectator
# Mixin intended for `Stub` to return new, modified stubs.
module StubModifiers
# Returns a new stub of the same type with constrained arguments.
abstract def with(*args, **kwargs)
end
end

View file

@ -11,6 +11,12 @@ module Spectator
@value
end
# Returns a new stub with constrained arguments.
def with(*args, **kwargs)
constraint = Arguments.new(args, kwargs)
self.class.new(method, @value, constraint, location)
end
# Creates the stub.
def initialize(method : Symbol, @value : T, constraint : AbstractArguments? = nil, location : Location? = nil)
super(method, constraint, location)