| 1 | module RSpec::Matchers::BuiltIn |
| 2 | RSpec.describe BaseMatcher do |
| 3 | describe "#match_unless_raises" do |
| 4 | let(:matcher) do |
| 5 | Class.new(BaseMatcher).new |
| 6 | end |
| 7 | |
| 8 | it "returns true if there are no errors" do |
| 9 | expect(matcher.match_unless_raises {}).to be_truthy |
| 10 | end |
| 11 | |
| 12 | it "returns false if there is an error" do |
| 13 | expect(matcher.match_unless_raises { raise }).to be_falsey |
| 14 | end |
| 15 | |
| 16 | it "returns false if the only submitted error is raised" do |
| 17 | expect(matcher.match_unless_raises(RuntimeError) { raise "foo" }).to be_falsey |
| 18 | end |
| 19 | |
| 20 | it "returns false if any of several errors submitted is raised" do |
| 21 | expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise "foo" }).to be_falsey |
| 22 | expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise ArgumentError.new('') }).to be_falsey |
| 23 | expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise NameError.new('') }).to be_falsey |
| 24 | end |
| 25 | |
| 26 | it "re-raises any error other than one of those specified" do |
| 27 | expect do |
| 28 | matcher.match_unless_raises(ArgumentError) { raise "foo" } |
| 29 | end.to raise_error "foo" |
| 30 | end |
| 31 | |
| 32 | it "stores the rescued exception for use in messages" do |
| 33 | matcher.match_unless_raises(RuntimeError) { raise "foo" } |
| 34 | expect(matcher.rescued_exception).to be_a(RuntimeError) |
| 35 | expect(matcher.rescued_exception.message).to eq("foo") |
| 36 | end |
| 37 | |
| 38 | end |
| 39 | |
| 40 | describe "#failure_message" do |
| 41 | context "when the parameter to .new is omitted" do |
| 42 | it "describes what was expected" do |
| 43 | matcher_class = Class.new(BaseMatcher) do |
| 44 | def match(_expected, _actual) |
| 45 | false |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | stub_const("Foo::Bar::BeSomething", matcher_class) |
| 50 | |
| 51 | matcher = matcher_class.new |
| 52 | matcher.matches?("foo") |
| 53 | expect(matcher.failure_message).to eq('expected "foo" to be something') |
| 54 | end |
| 55 | end |
| 56 | end |
| 57 | |
| 58 | describe "#===" do |
| 59 | it "responds the same way as matches?" do |
| 60 | matcher = Class.new(BaseMatcher) do |
| 61 | def initialize(expected) |
| 62 | @expected = expected |
| 63 | end |
| 64 | |
| 65 | def matches?(actual) |
| 66 | (@actual = actual) == @expected |
| 67 | end |
| 68 | end |
| 69 | |
| 70 | expect(matcher.new(3).matches?(3)).to be_truthy |
| 71 | expect(matcher.new(3)).to be === 3 |
| 72 | |
| 73 | expect(matcher.new(3).matches?(4)).to be_falsey |
| 74 | expect(matcher.new(3)).not_to be === 4 |
| 75 | end |
| 76 | end |
| 77 | |
| 78 | describe "default failure message detection" do |
| 79 | def has_default_failure_messages?(matcher) |
| 80 | BaseMatcher::DefaultFailureMessages.has_default_failure_messages?(matcher) |
| 81 | end |
| 82 | |
| 83 | shared_examples_for "detecting default failure message" do |
| 84 | context "that has no failure message overrides" do |
| 85 | it "indicates that it has default failure messages" do |
| 86 | matcher = build_matcher |
| 87 | expect(has_default_failure_messages?(matcher)).to be true |
| 88 | end |
| 89 | end |
| 90 | |
| 91 | context "that overrides `failure_message`" do |
| 92 | it "indicates that it lacks default failure messages" do |
| 93 | matcher = build_matcher { def failure_message; end } |
| 94 | expect(has_default_failure_messages?(matcher)).to be false |
| 95 | end |
| 96 | end |
| 97 | |
| 98 | context "that overrides `failure_message_when_negated`" do |
| 99 | it "indicates that it lacks default failure messages" do |
| 100 | matcher = build_matcher { def failure_message_when_negated; end } |
| 101 | expect(has_default_failure_messages?(matcher)).to be false |
| 102 | end |
| 103 | end |
| 104 | end |
| 105 | |
| 106 | context "for a DSL-defined custom matcher" do |
| 107 | include_examples "detecting default failure message" do |
| 108 | def build_matcher(&block) |
| 109 | definition = Proc.new do |
| 110 | match {} |
| 111 | module_exec(&block) if block |
| 112 | end |
| 113 | |
| 114 | RSpec::Matchers::DSL::Matcher.new(:matcher_name, definition, self) |
| 115 | end |
| 116 | end |
| 117 | end |
| 118 | |
| 119 | context "for a matcher that subclasses `BaseMatcher`" do |
| 120 | include_examples "detecting default failure message" do |
| 121 | def build_matcher(&block) |
| 122 | Class.new(RSpec::Matchers::BuiltIn::BaseMatcher, &block).new |
| 123 | end |
| 124 | end |
| 125 | end |
| 126 | |
| 127 | context "for a custom matcher that lacks `failure_message_when_negated` (documented as an optional part of the matcher protocol" do |
| 128 | it "indicates that it lacks default failure messages" do |
| 129 | matcher = Class.new(RSpec::Matchers::BuiltIn::BaseMatcher) { undef failure_message_when_negated }.new |
| 130 | |
| 131 | expect(RSpec::Support.is_a_matcher?(matcher)).to be true |
| 132 | expect(has_default_failure_messages?(matcher)).to be false |
| 133 | end |
| 134 | end |
| 135 | end |
| 136 | end |
| 137 | end |