mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add spec for ExceptionMatcher
This commit is contained in:
parent
244947a81d
commit
f2558fd09e
3 changed files with 210 additions and 1 deletions
|
@ -116,7 +116,7 @@ In no particular order, features that have been implemented and are planned:
|
|||
- [X] Type matchers - `be_a`
|
||||
- [ ] Collection matchers - `contain`, `have`, `contain_exactly[.in_order|.in_any_order]`, `match_array[.in_order|.in_any_order]`, `start_with`, `end_with`, `be_empty`, `have_key`, `have_value`, `all`, `all_satisfy`
|
||||
- [X] Truthy matchers - `be`, `be_true`, `be_truthy`, `be_false`, `be_falsey`, `be_nil`
|
||||
- [ ] Error matchers - `raise_error`
|
||||
- [X] Error matchers - `raise_error`
|
||||
- [ ] Yield matchers - `yield_control[.times]`, `yield_with_args[.times]`, `yield_with_no_args[.times]`, `yield_successive_args`
|
||||
- [ ] Output matchers - `output[.to_stdout|.to_stderr]`
|
||||
- [ ] Misc. matchers - `exist`, `match`, `satisfy`, `change[.by|.from[.to]|.to|.by_at_least|.by_at_most]`, `have_attributes`
|
||||
|
|
|
@ -8,6 +8,10 @@ def new_partial(actual : T = 123) forall T
|
|||
Spectator::Expectations::ValueExpectationPartial.new(actual, __FILE__, __LINE__)
|
||||
end
|
||||
|
||||
def new_block_partial(label = "BLOCK", &block)
|
||||
Spectator::Expectations::BlockExpectationPartial.new(block, label, __FILE__, __LINE__)
|
||||
end
|
||||
|
||||
def new_matcher(expected : T, label : String) forall T
|
||||
Spectator::Matchers::EqualityMatcher.new(expected, label)
|
||||
end
|
||||
|
|
205
spec/matchers/exception_matcher_spec.cr
Normal file
205
spec/matchers/exception_matcher_spec.cr
Normal file
|
@ -0,0 +1,205 @@
|
|||
require "../spec_helper"
|
||||
|
||||
describe Spectator::Matchers::ExceptionMatcher do
|
||||
describe "#match" do
|
||||
it "compares the message using #===" do
|
||||
spy = SpySUT.new
|
||||
partial = new_block_partial { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, SpySUT).new(spy, "foo")
|
||||
matcher.match(partial)
|
||||
spy.case_eq_call_count.should be > 0
|
||||
end
|
||||
|
||||
context "returned MatchData" do
|
||||
describe "#matched?" do
|
||||
context "with no exception" do
|
||||
it "is false" do
|
||||
partial = new_block_partial { 42 }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
context "with an exception" do
|
||||
context "of the same type" do
|
||||
it "is true" do
|
||||
partial = new_block_partial { raise ArgumentError.new }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(ArgumentError, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
context "of a different type" do
|
||||
it "is false" do
|
||||
partial = new_block_partial { raise ArgumentError.new }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(KeyError, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
context "of a sub-type" do
|
||||
it "is true" do
|
||||
partial = new_block_partial { raise ArgumentError.new }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
context "and an equal message" do
|
||||
it "is true" do
|
||||
message = "foobar"
|
||||
partial = new_block_partial { raise ArgumentError.new(message) }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(ArgumentError, String).new(message, "label")
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
context "and a different message" do
|
||||
it "is false" do
|
||||
partial = new_block_partial { raise ArgumentError.new("foobar") }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(ArgumentError, String).new("different", "label")
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
context "and a matching regex" do
|
||||
it "is true" do
|
||||
partial = new_block_partial { raise ArgumentError.new("foobar") }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(ArgumentError, Regex).new(/foo/, "label")
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
context "and a non-matching regex" do
|
||||
it "is false" do
|
||||
partial = new_block_partial { raise ArgumentError.new("foobar") }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(ArgumentError, Regex).new(/baz/, "label")
|
||||
match_data = matcher.match(partial)
|
||||
match_data.matched?.should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#values" do
|
||||
describe "expected type" do
|
||||
it "is the exception type" do
|
||||
partial = new_block_partial { raise ArgumentError.new }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(KeyError, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data_prefix(match_data, :"expected type")[:value].should eq(KeyError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "actual type" do
|
||||
it "is the raised type" do
|
||||
partial = new_block_partial { raise ArgumentError.new }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(KeyError, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data_prefix(match_data, :"actual type")[:value].should eq(ArgumentError)
|
||||
end
|
||||
|
||||
context "when nothing is raised" do
|
||||
it "is Nil" do
|
||||
partial = new_block_partial { 42 }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(KeyError, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data_prefix(match_data, :"actual type")[:value].should eq(Nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "expected message" do
|
||||
it "is the expected value" do
|
||||
regex = /baz/
|
||||
partial = new_block_partial { raise ArgumentError.new("foobar") }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(KeyError, Regex).new(regex, "label")
|
||||
match_data = matcher.match(partial)
|
||||
match_data_prefix(match_data, :"expected message")[:value].should eq(regex)
|
||||
end
|
||||
end
|
||||
|
||||
describe "actual message" do
|
||||
it "is the raised exception's message" do
|
||||
message = "foobar"
|
||||
partial = new_block_partial { raise ArgumentError.new(message) }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(KeyError, Regex).new(/baz/, "label")
|
||||
match_data = matcher.match(partial)
|
||||
match_data_prefix(match_data, :"actual message")[:value].should eq(message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#message" do
|
||||
it "mentions raise" do
|
||||
partial = new_block_partial { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.message.should contain("raise")
|
||||
end
|
||||
|
||||
it "contains the actual label" do
|
||||
label = "everything"
|
||||
partial = new_block_partial(label) { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.message.should contain(label)
|
||||
end
|
||||
|
||||
it "contains the expected label" do
|
||||
label = "everything"
|
||||
partial = new_block_partial { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Regex).new(/foobar/, label)
|
||||
match_data = matcher.match(partial)
|
||||
match_data.message.should contain(label)
|
||||
end
|
||||
|
||||
it "contains the exception type" do
|
||||
partial = new_block_partial { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(ArgumentError, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.message.should contain("ArgumentError")
|
||||
end
|
||||
end
|
||||
|
||||
describe "#negated_message" do
|
||||
it "mentions raise" do
|
||||
partial = new_block_partial { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.negated_message.should contain("raise")
|
||||
end
|
||||
|
||||
it "contains the actual label" do
|
||||
label = "everything"
|
||||
partial = new_block_partial(label) { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.negated_message.should contain(label)
|
||||
end
|
||||
|
||||
it "contains the expected label" do
|
||||
label = "everything"
|
||||
partial = new_block_partial { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(Exception, Regex).new(/foobar/, label)
|
||||
match_data = matcher.match(partial)
|
||||
match_data.negated_message.should contain(label)
|
||||
end
|
||||
|
||||
it "contains the exception type" do
|
||||
partial = new_block_partial { raise "foobar" }
|
||||
matcher = Spectator::Matchers::ExceptionMatcher(ArgumentError, Nil).new
|
||||
match_data = matcher.match(partial)
|
||||
match_data.negated_message.should contain("ArgumentError")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue