| 1 | RSpec.describe "expect(...).to satisfy { block }" do |
| 2 | it_behaves_like "an RSpec value matcher", :valid_value => true, :invalid_value => false do |
| 3 | let(:matcher) { satisfy { |v| v } } |
| 4 | end |
| 5 | |
| 6 | it "describes itself" do |
| 7 | expect(satisfy.description).to eq("satisfy block") |
| 8 | end |
| 9 | |
| 10 | it "passes if block returns true" do |
| 11 | expect(true).to satisfy { |val| val } |
| 12 | expect(true).to satisfy do |val| |
| 13 | val |
| 14 | end |
| 15 | end |
| 16 | |
| 17 | context "when no custom description is provided" do |
| 18 | context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do |
| 19 | it "fails with block snippet if block returns false" do |
| 20 | expect { |
| 21 | expect(false).to satisfy { |val| val } |
| 22 | }.to fail_with("expected false to satisfy expression `val`") |
| 23 | |
| 24 | expect do |
| 25 | expect(false).to satisfy do |val| |
| 26 | val |
| 27 | end |
| 28 | end.to fail_with("expected false to satisfy expression `val`") |
| 29 | end |
| 30 | |
| 31 | context 'when used with an alias name' do |
| 32 | alias_matcher :fulfill, :satisfy |
| 33 | |
| 34 | it 'can extract the block snippet' do |
| 35 | expect { |
| 36 | expect(false).to fulfill { |val| val } |
| 37 | }.to fail_with("expected false to fulfill expression `val`") |
| 38 | end |
| 39 | end |
| 40 | end |
| 41 | |
| 42 | context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do |
| 43 | it "fails without block snippet if block returns false" do |
| 44 | expect { |
| 45 | expect(false).to satisfy { |val| val } |
| 46 | }.to fail_with("expected false to satisfy block") |
| 47 | |
| 48 | expect do |
| 49 | expect(false).to satisfy do |val| |
| 50 | val |
| 51 | end |
| 52 | end.to fail_with("expected false to satisfy block") |
| 53 | end |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | context "when a custom description is provided" do |
| 58 | it "describes itself" do |
| 59 | expect(satisfy("be awesome").description).to eq("be awesome") |
| 60 | end |
| 61 | |
| 62 | it "passes if block returns true" do |
| 63 | expect(true).to satisfy("be true") { |val| val } |
| 64 | expect(true).to satisfy("be true") do |val| |
| 65 | val |
| 66 | end |
| 67 | end |
| 68 | |
| 69 | it "fails with the custom description if block returns false" do |
| 70 | expect { |
| 71 | expect(false).to satisfy("be true") { |val| val } |
| 72 | }.to fail_with("expected false to be true") |
| 73 | expect do |
| 74 | expect(false).to satisfy("be true") do |val| |
| 75 | val |
| 76 | end |
| 77 | end.to fail_with("expected false to be true") |
| 78 | end |
| 79 | end |
| 80 | end |
| 81 | |
| 82 | RSpec.describe "expect(...).not_to satisfy { block }" do |
| 83 | it "passes if block returns false" do |
| 84 | expect(false).not_to satisfy { |val| val } |
| 85 | expect(false).not_to satisfy do |val| |
| 86 | val |
| 87 | end |
| 88 | end |
| 89 | |
| 90 | context "when no custom description is provided" do |
| 91 | context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do |
| 92 | it "fails with block snippet if block returns true" do |
| 93 | expect { |
| 94 | expect(true).not_to satisfy { |val| val } |
| 95 | }.to fail_with("expected true not to satisfy expression `val`") |
| 96 | end |
| 97 | end |
| 98 | |
| 99 | context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do |
| 100 | it "fails without block snippet if block returns true" do |
| 101 | expect { |
| 102 | expect(true).not_to satisfy { |val| val } |
| 103 | }.to fail_with("expected true not to satisfy block") |
| 104 | end |
| 105 | end |
| 106 | end |
| 107 | |
| 108 | context "when a custom description is provided" do |
| 109 | it "passes if block returns false" do |
| 110 | expect(false).not_to satisfy("be true") { |val| val } |
| 111 | expect(false).not_to satisfy("be true") do |val| |
| 112 | val |
| 113 | end |
| 114 | end |
| 115 | |
| 116 | it "fails with the custom description if block returns true" do |
| 117 | expect { |
| 118 | expect(true).not_to satisfy("be true") { |val| val } |
| 119 | }.to fail_with("expected true not to be true") |
| 120 | end |
| 121 | end |
| 122 | end |