| 1 | module RSpec::Matchers::BuiltIn |
| 2 | RSpec.describe All do |
| 3 | |
| 4 | it_behaves_like 'an RSpec value matcher', :valid_value => ['A', 'A', 'A'], :invalid_value => ['A', 'A', 'B'], :disallows_negation => true do |
| 5 | let(:matcher) { all( eq('A') ) } |
| 6 | end |
| 7 | |
| 8 | describe 'description' do |
| 9 | it 'provides a description' do |
| 10 | matcher = all( eq('A') ) |
| 11 | expect(matcher.description).to eq 'all eq "A"' |
| 12 | end |
| 13 | end |
| 14 | |
| 15 | it "refuses to invert" do |
| 16 | expect { |
| 17 | expect([5, 3, 3]).not_to all(be_odd) |
| 18 | }.to raise_error(NotImplementedError, /not_to all.*is not supported/) |
| 19 | end |
| 20 | |
| 21 | context 'when single matcher is given' do |
| 22 | |
| 23 | describe 'expect(...).to all(expected)' do |
| 24 | |
| 25 | it 'can pass' do |
| 26 | expect(['A', 'A', 'A']).to all( eq('A') ) |
| 27 | end |
| 28 | |
| 29 | describe 'failure message' do |
| 30 | |
| 31 | context 'when the matcher has single-line failure message' do |
| 32 | it 'returns the index of the failed object' do |
| 33 | expect { |
| 34 | expect(['A', 'A', 'A', 5, 'A']).to all( be_a(String) ) |
| 35 | }.to fail_with(dedent <<-EOS) |
| 36 | |expected ["A", "A", "A", 5, "A"] to all be a kind of String |
| 37 | | |
| 38 | | object at index 3 failed to match: |
| 39 | | expected 5 to be a kind of String |
| 40 | EOS |
| 41 | end |
| 42 | |
| 43 | it 'returns the indexes of all failed objects, not just the first one' do |
| 44 | expect { |
| 45 | expect(['A', 'A', 'A', 5, 6]).to all( be_a(String) ) |
| 46 | }.to fail_with(dedent <<-EOS) |
| 47 | |expected ["A", "A", "A", 5, 6] to all be a kind of String |
| 48 | | |
| 49 | | object at index 3 failed to match: |
| 50 | | expected 5 to be a kind of String |
| 51 | | |
| 52 | | object at index 4 failed to match: |
| 53 | | expected 6 to be a kind of String |
| 54 | EOS |
| 55 | end |
| 56 | end |
| 57 | |
| 58 | context 'when the matcher has multi-line failure message' do |
| 59 | it 'returns the index of the failed object' do |
| 60 | expect { |
| 61 | expect(['A', 'A', 'A', 'C', 'A']).to all( eq('A') ) |
| 62 | }.to fail_with(dedent <<-EOS) |
| 63 | |expected ["A", "A", "A", "C", "A"] to all eq "A" |
| 64 | | |
| 65 | | object at index 3 failed to match: |
| 66 | | expected: "A" |
| 67 | | got: "C" |
| 68 | | |
| 69 | | (compared using ==) |
| 70 | EOS |
| 71 | end |
| 72 | |
| 73 | it 'returns the indexes of all failed objects, not just the first one' do |
| 74 | expect { |
| 75 | expect(['A', 'B', 'A', 'C', 'A']).to all( eq('A') ) |
| 76 | }.to fail_with(dedent <<-EOS) |
| 77 | |expected ["A", "B", "A", "C", "A"] to all eq "A" |
| 78 | | |
| 79 | | object at index 1 failed to match: |
| 80 | | expected: "A" |
| 81 | | got: "B" |
| 82 | | |
| 83 | | (compared using ==) |
| 84 | | |
| 85 | | object at index 3 failed to match: |
| 86 | | expected: "A" |
| 87 | | got: "C" |
| 88 | | |
| 89 | | (compared using ==) |
| 90 | EOS |
| 91 | end |
| 92 | end |
| 93 | |
| 94 | end |
| 95 | end |
| 96 | end |
| 97 | |
| 98 | context 'when composed matcher is given' do |
| 99 | |
| 100 | describe 'expect(...).to all(expected)' do |
| 101 | it 'can pass' do |
| 102 | expect([3, 4, 7, 8]).to all( be_between(2, 5).or be_between(6, 9) ) |
| 103 | end |
| 104 | end |
| 105 | |
| 106 | describe 'failure message' do |
| 107 | |
| 108 | context 'when a single object fails' do |
| 109 | it 'returns the index of the failed object for the composed matcher' do |
| 110 | expect { |
| 111 | expect([3, 4, 7, 28]).to all( be_between(2, 5).or be_between(6, 9) ) |
| 112 | }.to fail_with(dedent <<-EOS) |
| 113 | |expected [3, 4, 7, 28] to all be between 2 and 5 (inclusive) or be between 6 and 9 (inclusive) |
| 114 | | |
| 115 | | object at index 3 failed to match: |
| 116 | | expected 28 to be between 2 and 5 (inclusive) |
| 117 | | |
| 118 | | ...or: |
| 119 | | |
| 120 | | expected 28 to be between 6 and 9 (inclusive) |
| 121 | EOS |
| 122 | end |
| 123 | end |
| 124 | |
| 125 | context 'when a multiple objects fails' do |
| 126 | it 'returns the indexes of the failed objects for the composed matcher, not just the first one' do |
| 127 | expect { |
| 128 | expect([3, 4, 27, 22]).to all( be_between(2, 5).or be_between(6, 9) ) |
| 129 | }.to fail_with(dedent <<-EOS) |
| 130 | |expected [3, 4, 27, 22] to all be between 2 and 5 (inclusive) or be between 6 and 9 (inclusive) |
| 131 | | |
| 132 | | object at index 2 failed to match: |
| 133 | | expected 27 to be between 2 and 5 (inclusive) |
| 134 | | |
| 135 | | ...or: |
| 136 | | |
| 137 | | expected 27 to be between 6 and 9 (inclusive) |
| 138 | | |
| 139 | | object at index 3 failed to match: |
| 140 | | expected 22 to be between 2 and 5 (inclusive) |
| 141 | | |
| 142 | | ...or: |
| 143 | | |
| 144 | | expected 22 to be between 6 and 9 (inclusive) |
| 145 | EOS |
| 146 | end |
| 147 | end |
| 148 | end |
| 149 | end |
| 150 | |
| 151 | context 'when composed in another matcher' do |
| 152 | it 'returns the indexes of the failed objects only' do |
| 153 | expect { |
| 154 | expect([[false], [true]]).to all( all( be(true) ) ) |
| 155 | }.to fail_with(dedent <<-EOS) |
| 156 | |expected [[false], [true]] to all all equal true |
| 157 | | |
| 158 | | object at index 0 failed to match: |
| 159 | | expected [false] to all equal true |
| 160 | | |
| 161 | | object at index 0 failed to match: |
| 162 | | expected true |
| 163 | | got false |
| 164 | EOS |
| 165 | end |
| 166 | end |
| 167 | |
| 168 | shared_examples "making a copy" do |copy_method| |
| 169 | context "when making a copy via `#{copy_method}`" do |
| 170 | |
| 171 | let(:base_matcher) { eq(3) } |
| 172 | let(:all_matcher) { all( base_matcher ) } |
| 173 | let(:copied_matcher) { all_matcher.__send__(copy_method) } |
| 174 | |
| 175 | it "uses a copy of the base matcher" do |
| 176 | expect(copied_matcher).not_to equal(all_matcher) |
| 177 | expect(copied_matcher.matcher).not_to equal(base_matcher) |
| 178 | expect(copied_matcher.matcher).to be_a(base_matcher.class) |
| 179 | expect(copied_matcher.matcher.expected).to eq(3) |
| 180 | end |
| 181 | |
| 182 | context 'when using a custom matcher' do |
| 183 | |
| 184 | let(:base_matcher) { custom_include(3) } |
| 185 | |
| 186 | it 'copies custom matchers properly so they can work even though they have singleton behavior' do |
| 187 | expect(copied_matcher).not_to equal(all_matcher) |
| 188 | expect(copied_matcher.matcher).not_to equal(base_matcher) |
| 189 | expect([[3]]).to copied_matcher |
| 190 | expect { expect([[4]]).to copied_matcher }.to fail_including("expected [4]") |
| 191 | end |
| 192 | |
| 193 | end |
| 194 | |
| 195 | end |
| 196 | end |
| 197 | |
| 198 | include_examples 'making a copy', :clone |
| 199 | include_examples 'making a copy', :dup |
| 200 | |
| 201 | context "when using a matcher instance that memoizes state multiple times in a composed expression" do |
| 202 | it "works properly in spite of the memoization" do |
| 203 | expect { |
| 204 | expect(["foo", "bar", "a"]).to all( have_string_length(3) ) |
| 205 | }.to fail |
| 206 | end |
| 207 | |
| 208 | context "when passing a compound expression" do |
| 209 | it "works properly in spite of the memoization" do |
| 210 | expect { |
| 211 | expect(["foo", "bar", "a"]).to all( have_string_length(2).or have_string_length(3) ) |
| 212 | }.to fail |
| 213 | end |
| 214 | end |
| 215 | end |
| 216 | |
| 217 | context 'when the actual data does not define #each_with_index' do |
| 218 | let(:actual) { 5 } |
| 219 | |
| 220 | it 'returns a failure message' do |
| 221 | expect { |
| 222 | expect(actual).to all(be_a(String)) |
| 223 | }.to fail_with("expected #{actual.inspect} to all be a kind of String, but was not iterable") |
| 224 | end |
| 225 | end |
| 226 | |
| 227 | context 'when the actual data does not include enumerable but defines #each_with_index' do |
| 228 | let(:actual) do |
| 229 | obj = Object.new |
| 230 | def obj.each_with_index(&_block); [5].each_with_index { |o, i| yield(o, i) }; end |
| 231 | obj |
| 232 | end |
| 233 | |
| 234 | it 'passes properly' do |
| 235 | expect(actual).to all(be_a(Integer)) |
| 236 | end |
| 237 | |
| 238 | it 'fails properly' do |
| 239 | expect { |
| 240 | expect(actual).to all(be_even) |
| 241 | }.to fail_with(/to all be even/) |
| 242 | end |
| 243 | end |
| 244 | |
| 245 | end |
| 246 | end |