| 1 | module RSpec::Matchers::BuiltIn |
| 2 | RSpec.describe ThrowSymbol do |
| 3 | it_behaves_like "an RSpec block-only matcher" do |
| 4 | def valid_block |
| 5 | throw :foo |
| 6 | end |
| 7 | def invalid_block |
| 8 | end |
| 9 | let(:matcher) { throw_symbol(:foo) } |
| 10 | end |
| 11 | |
| 12 | describe "with no args" do |
| 13 | before(:example) { @matcher = throw_symbol } |
| 14 | |
| 15 | it "matches if any Symbol is thrown" do |
| 16 | expect(@matcher.matches?(lambda { throw :sym })).to be_truthy |
| 17 | end |
| 18 | it "matches if any Symbol is thrown with an arg" do |
| 19 | expect(@matcher.matches?(lambda { throw :sym, "argument" })).to be_truthy |
| 20 | end |
| 21 | it "does not match if no Symbol is thrown" do |
| 22 | expect(@matcher.matches?(lambda {})).to be_falsey |
| 23 | end |
| 24 | it "provides a failure message" do |
| 25 | @matcher.matches?(lambda {}) |
| 26 | expect(@matcher.failure_message).to eq "expected a Symbol to be thrown, got nothing" |
| 27 | end |
| 28 | it "provides a negative failure message" do |
| 29 | @matcher.matches?(lambda { throw :sym }) |
| 30 | expect(@matcher.failure_message_when_negated).to eq "expected no Symbol to be thrown, got :sym" |
| 31 | end |
| 32 | end |
| 33 | |
| 34 | describe "with a symbol" do |
| 35 | before(:example) { @matcher = throw_symbol(:sym) } |
| 36 | |
| 37 | it "matches if correct Symbol is thrown" do |
| 38 | expect(@matcher.matches?(lambda { throw :sym })).to be_truthy |
| 39 | end |
| 40 | it "matches if correct Symbol is thrown with an arg" do |
| 41 | expect(@matcher.matches?(lambda { throw :sym, "argument" })).to be_truthy |
| 42 | end |
| 43 | it "does not match if no Symbol is thrown" do |
| 44 | expect(@matcher.matches?(lambda {})).to be_falsey |
| 45 | end |
| 46 | it "does not match if correct Symbol is thrown" do |
| 47 | expect(@matcher.matches?(lambda { throw :other_sym })).to be_falsey |
| 48 | end |
| 49 | it "provides a failure message when no Symbol is thrown" do |
| 50 | @matcher.matches?(lambda {}) |
| 51 | expect(@matcher.failure_message).to eq "expected :sym to be thrown, got nothing" |
| 52 | end |
| 53 | it "provides a failure message when wrong Symbol is thrown" do |
| 54 | @matcher.matches?(lambda { throw :other_sym }) |
| 55 | expect(@matcher.failure_message).to eq "expected :sym to be thrown, got :other_sym" |
| 56 | end |
| 57 | it "provides a negative failure message" do |
| 58 | @matcher.matches?(lambda { throw :sym }) |
| 59 | expect(@matcher.failure_message_when_negated).to eq "expected :sym not to be thrown, got :sym" |
| 60 | end |
| 61 | it "only matches NameErrors raised by uncaught throws" do |
| 62 | expect { |
| 63 | expect(@matcher.matches?(lambda { sym })).to be_falsey |
| 64 | }.to raise_error(NameError) |
| 65 | end |
| 66 | end |
| 67 | |
| 68 | describe "with a symbol and an arg" do |
| 69 | before(:example) { @matcher = throw_symbol(:sym, "a") } |
| 70 | |
| 71 | it "matches if correct Symbol and args are thrown" do |
| 72 | expect(@matcher.matches?(lambda { throw :sym, "a" })).to be_truthy |
| 73 | end |
| 74 | it "does not match if nothing is thrown" do |
| 75 | expect(@matcher.matches?(lambda {})).to be_falsey |
| 76 | end |
| 77 | it "does not match if other Symbol is thrown" do |
| 78 | expect(@matcher.matches?(lambda { throw :other_sym, "a" })).to be_falsey |
| 79 | end |
| 80 | it "does not match if no arg is thrown" do |
| 81 | expect(@matcher.matches?(lambda { throw :sym })).to be_falsey |
| 82 | end |
| 83 | it "does not match if wrong arg is thrown" do |
| 84 | expect(@matcher.matches?(lambda { throw :sym, "b" })).to be_falsey |
| 85 | end |
| 86 | it "provides a failure message when no Symbol is thrown" do |
| 87 | @matcher.matches?(lambda {}) |
| 88 | expect(@matcher.failure_message).to eq 'expected :sym with "a" to be thrown, got nothing' |
| 89 | end |
| 90 | it "provides a failure message when wrong Symbol is thrown" do |
| 91 | @matcher.matches?(lambda { throw :other_sym }) |
| 92 | expect(@matcher.failure_message).to eq 'expected :sym with "a" to be thrown, got :other_sym' |
| 93 | end |
| 94 | it "provides a failure message when wrong arg is thrown" do |
| 95 | @matcher.matches?(lambda { throw :sym, "b" }) |
| 96 | expect(@matcher.failure_message).to eq 'expected :sym with "a" to be thrown, got :sym with "b"' |
| 97 | end |
| 98 | it "provides a failure message when no arg is thrown" do |
| 99 | @matcher.matches?(lambda { throw :sym }) |
| 100 | expect(@matcher.failure_message).to eq 'expected :sym with "a" to be thrown, got :sym with no argument' |
| 101 | end |
| 102 | it "provides a negative failure message" do |
| 103 | @matcher.matches?(lambda { throw :sym }) |
| 104 | expect(@matcher.failure_message_when_negated).to eq 'expected :sym with "a" not to be thrown, got :sym with no argument' |
| 105 | end |
| 106 | it "only matches NameErrors raised by uncaught throws" do |
| 107 | expect { |
| 108 | expect(@matcher.matches?(lambda { sym })).to be_falsey |
| 109 | }.to raise_error(NameError) |
| 110 | end |
| 111 | it "raises other errors" do |
| 112 | expect { |
| 113 | @matcher.matches?(lambda { raise "Boom" }) |
| 114 | }.to raise_error(/Boom/) |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | describe "composing with other matchers" do |
| 119 | it 'passes when the matcher matches the thrown arg' do |
| 120 | expect { |
| 121 | throw :foo, "bar" |
| 122 | }.to throw_symbol(:foo, a_string_matching(/bar/)) |
| 123 | end |
| 124 | |
| 125 | it 'fails when the matcher does not match the thrown arg' do |
| 126 | expect { |
| 127 | expect { throw :foo, "bar" }.to throw_symbol(:foo, a_string_matching(/foo/)) |
| 128 | }.to fail_with('expected :foo with a string matching /foo/ to be thrown, got :foo with "bar"') |
| 129 | end |
| 130 | |
| 131 | it 'provides a description' do |
| 132 | description = throw_symbol(:foo, a_string_matching(/bar/)).description |
| 133 | expect(description).to eq("throw :foo with a string matching /bar/") |
| 134 | end |
| 135 | end |
| 136 | end |
| 137 | end |