mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add ExceptionStub
This commit is contained in:
parent
f17cc73487
commit
4d5004ab4f
5 changed files with 354 additions and 0 deletions
166
spec/spectator/mocks/exception_stub_spec.cr
Normal file
166
spec/spectator/mocks/exception_stub_spec.cr
Normal file
|
@ -0,0 +1,166 @@
|
||||||
|
require "../../spec_helper"
|
||||||
|
|
||||||
|
Spectator.describe Spectator::ExceptionStub do
|
||||||
|
let(method_call) { Spectator::MethodCall.capture(:foo) }
|
||||||
|
let(location) { Spectator::Location.new(__FILE__, __LINE__) }
|
||||||
|
let(exception) { RuntimeError.new("Test exception") }
|
||||||
|
subject(stub) { described_class.new(:foo, exception, location: location) }
|
||||||
|
|
||||||
|
it "stores the method name" do
|
||||||
|
expect(stub.method).to eq(:foo)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "stores the location" do
|
||||||
|
expect(stub.location).to eq(location)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises the specified exception" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(RuntimeError, "Test exception")
|
||||||
|
end
|
||||||
|
|
||||||
|
context Spectator::StubModifiers do
|
||||||
|
describe "#and_return(value)" do
|
||||||
|
let(arguments) { Spectator::Arguments.capture(/foo/) }
|
||||||
|
let(location) { Spectator::Location.new(__FILE__, __LINE__) }
|
||||||
|
let(original) { Spectator::ExceptionStub.new(:foo, exception, arguments, location) }
|
||||||
|
subject(stub) { original.and_return(123) }
|
||||||
|
|
||||||
|
it "produces a stub that returns a value" do
|
||||||
|
expect(stub.call(method_call)).to eq(123)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the method name" do
|
||||||
|
expect(stub.method).to eq(:foo)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the arguments constraint" do
|
||||||
|
expect(stub.constraint).to eq(arguments)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the location" do
|
||||||
|
expect(stub.location).to eq(location)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#and_return(*values)" do
|
||||||
|
let(arguments) { Spectator::Arguments.capture(/foo/) }
|
||||||
|
let(location) { Spectator::Location.new(__FILE__, __LINE__) }
|
||||||
|
let(original) { Spectator::ExceptionStub.new(:foo, exception, arguments, location) }
|
||||||
|
subject(stub) { original.and_return(3, 2, 1, 0) }
|
||||||
|
|
||||||
|
it "produces a stub that returns values" do
|
||||||
|
values = Array.new(5) { stub.call(method_call) }
|
||||||
|
expect(values).to eq([3, 2, 1, 0, 0])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the method name" do
|
||||||
|
expect(stub.method).to eq(:foo)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the arguments constraint" do
|
||||||
|
expect(stub.constraint).to eq(arguments)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the location" do
|
||||||
|
expect(stub.location).to eq(location)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#and_raise" do
|
||||||
|
let(arguments) { Spectator::Arguments.capture(/foo/) }
|
||||||
|
let(location) { Spectator::Location.new(__FILE__, __LINE__) }
|
||||||
|
let(original) { Spectator::ExceptionStub.new(:foo, exception, arguments, location) }
|
||||||
|
let(new_exception) { ArgumentError.new("Test argument error") }
|
||||||
|
subject(stub) { original.and_raise(new_exception) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class and message" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError, "Test argument error") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a message" do
|
||||||
|
subject(stub) { original.and_raise("Test exception") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(Exception, "Test exception")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the method name" do
|
||||||
|
expect(stub.method).to eq(:foo)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the arguments constraint" do
|
||||||
|
expect(stub.constraint).to eq(arguments)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the location" do
|
||||||
|
expect(stub.location).to eq(location)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#===" do
|
||||||
|
subject { stub === call }
|
||||||
|
|
||||||
|
context "with a matching method name" do
|
||||||
|
let(call) { Spectator::MethodCall.capture(:foo, "foobar") }
|
||||||
|
|
||||||
|
it "returns true" do
|
||||||
|
is_expected.to be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a different method name" do
|
||||||
|
let(call) { Spectator::MethodCall.capture(:bar, "foobar") }
|
||||||
|
|
||||||
|
it "returns false" do
|
||||||
|
is_expected.to be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a constraint" do
|
||||||
|
let(constraint) { Spectator::Arguments.capture(/foo/) }
|
||||||
|
let(stub) { Spectator::ValueStub.new(:foo, 42, constraint) }
|
||||||
|
|
||||||
|
context "with a matching method name" do
|
||||||
|
let(call) { Spectator::MethodCall.capture(:foo, "foobar") }
|
||||||
|
|
||||||
|
it "returns true" do
|
||||||
|
is_expected.to be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a non-matching arguments" do
|
||||||
|
let(call) { Spectator::MethodCall.capture(:foo, "baz") }
|
||||||
|
|
||||||
|
it "returns false" do
|
||||||
|
is_expected.to be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a different method name" do
|
||||||
|
let(call) { Spectator::MethodCall.capture(:bar, "foobar") }
|
||||||
|
|
||||||
|
it "returns false" do
|
||||||
|
is_expected.to be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -72,6 +72,54 @@ Spectator.describe Spectator::MultiValueStub do
|
||||||
expect(stub.location).to eq(location)
|
expect(stub.location).to eq(location)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#and_raise" do
|
||||||
|
let(arguments) { Spectator::Arguments.capture(/foo/) }
|
||||||
|
let(location) { Spectator::Location.new(__FILE__, __LINE__) }
|
||||||
|
let(original) { Spectator::MultiValueStub.new(:foo, [3, 5, 7], arguments, location) }
|
||||||
|
let(new_exception) { ArgumentError.new("Test argument error") }
|
||||||
|
subject(stub) { original.and_raise(new_exception) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class and message" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError, "Test argument error") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a message" do
|
||||||
|
subject(stub) { original.and_raise("Test exception") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(Exception, "Test exception")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the method name" do
|
||||||
|
expect(stub.method).to eq(:foo)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the arguments constraint" do
|
||||||
|
expect(stub.constraint).to eq(arguments)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the location" do
|
||||||
|
expect(stub.location).to eq(location)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#===" do
|
describe "#===" do
|
||||||
|
|
|
@ -64,6 +64,54 @@ Spectator.describe Spectator::NullStub do
|
||||||
expect(stub.location).to eq(location)
|
expect(stub.location).to eq(location)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#and_raise" do
|
||||||
|
let(arguments) { Spectator::Arguments.capture(/foo/) }
|
||||||
|
let(location) { Spectator::Location.new(__FILE__, __LINE__) }
|
||||||
|
let(original) { Spectator::NullStub.new(:foo, arguments, location) }
|
||||||
|
let(new_exception) { ArgumentError.new("Test argument error") }
|
||||||
|
subject(stub) { original.and_raise(new_exception) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class and message" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError, "Test argument error") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a message" do
|
||||||
|
subject(stub) { original.and_raise("Test exception") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(Exception, "Test exception")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the method name" do
|
||||||
|
expect(stub.method).to eq(:foo)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the arguments constraint" do
|
||||||
|
expect(stub.constraint).to eq(arguments)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the location" do
|
||||||
|
expect(stub.location).to eq(location)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#===" do
|
describe "#===" do
|
||||||
|
|
|
@ -64,6 +64,54 @@ Spectator.describe Spectator::ValueStub do
|
||||||
expect(stub.location).to eq(location)
|
expect(stub.location).to eq(location)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#and_raise" do
|
||||||
|
let(arguments) { Spectator::Arguments.capture(/foo/) }
|
||||||
|
let(location) { Spectator::Location.new(__FILE__, __LINE__) }
|
||||||
|
let(original) { Spectator::ValueStub.new(:foo, 42, arguments, location) }
|
||||||
|
let(new_exception) { ArgumentError.new("Test argument error") }
|
||||||
|
subject(stub) { original.and_raise(new_exception) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class and message" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError, "Test argument error") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError, "Test argument error")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a message" do
|
||||||
|
subject(stub) { original.and_raise("Test exception") }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(Exception, "Test exception")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a class" do
|
||||||
|
subject(stub) { original.and_raise(ArgumentError) }
|
||||||
|
|
||||||
|
it "produces a stub that raises" do
|
||||||
|
expect { stub.call(method_call) }.to raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the method name" do
|
||||||
|
expect(stub.method).to eq(:foo)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the arguments constraint" do
|
||||||
|
expect(stub.constraint).to eq(arguments)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "retains the location" do
|
||||||
|
expect(stub.location).to eq(location)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#===" do
|
describe "#===" do
|
||||||
|
|
44
src/spectator/mocks/exception_stub.cr
Normal file
44
src/spectator/mocks/exception_stub.cr
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
require "../location"
|
||||||
|
require "./arguments"
|
||||||
|
require "./stub"
|
||||||
|
require "./stub_modifiers"
|
||||||
|
|
||||||
|
module Spectator
|
||||||
|
# Stub that raises an exception.
|
||||||
|
class ExceptionStub < Stub
|
||||||
|
# Invokes the stubbed implementation.
|
||||||
|
def call(call : MethodCall) : Nil
|
||||||
|
raise @exception
|
||||||
|
end
|
||||||
|
|
||||||
|
# Creates the stub.
|
||||||
|
def initialize(method : Symbol, @exception : Exception, constraint : AbstractArguments? = nil, location : Location? = nil)
|
||||||
|
super(method, constraint, location)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module StubModifiers
|
||||||
|
# Returns a new stub that raises an exception.
|
||||||
|
def and_raise(exception : Exception)
|
||||||
|
ExceptionStub.new(method, exception, constraint, location)
|
||||||
|
end
|
||||||
|
|
||||||
|
# :ditto:
|
||||||
|
def and_raise(exception_class : Exception.class, message)
|
||||||
|
exception = exception_class.new(message)
|
||||||
|
and_raise(exception)
|
||||||
|
end
|
||||||
|
|
||||||
|
# :ditto:
|
||||||
|
def and_raise(message : String? = nil)
|
||||||
|
exception = Exception.new(message)
|
||||||
|
and_raise(exception)
|
||||||
|
end
|
||||||
|
|
||||||
|
# :ditto:
|
||||||
|
def and_raise(exception_class : Exception.class)
|
||||||
|
exception = exception_class.new
|
||||||
|
and_raise(exception)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue