| 1 |
module YieldHelpers |
| 2 |
|
| 3 |
|
| 4 |
def _dont_yield(&_block) |
| 5 |
end |
| 6 |
|
| 7 |
def _yield_with_no_args |
| 8 |
yield |
| 9 |
end |
| 10 |
|
| 11 |
def _yield_with_args(*args, &_block) |
| 12 |
yield(*args) |
| 13 |
end |
| 14 |
end |
| 15 |
|
| 16 |
class InstanceEvaler |
| 17 |
def yield_with_no_args(&block) |
| 18 |
instance_exec(&block) |
| 19 |
end |
| 20 |
|
| 21 |
def yield_with_args(*args, &block) |
| 22 |
instance_exec(*args, &block) |
| 23 |
end |
| 24 |
|
| 25 |
def each_arg(*args, &block) |
| 26 |
args.each do |arg| |
| 27 |
instance_exec(arg, &block) |
| 28 |
end |
| 29 |
end |
| 30 |
end |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
RSpec.shared_examples "an RSpec probe-yielding block-only matcher" do |*options| |
| 40 |
include_examples "an RSpec block-only matcher", { :expects_lambda => true }.merge(options.first || {}) do |
| 41 |
let(:valid_expectation) { expect { |block| valid_block(&block) } } |
| 42 |
let(:invalid_expectation) { expect { |block| invalid_block(&block) } } |
| 43 |
|
| 44 |
let(:valid_block_lambda) { lambda { |block| valid_block(&block) } } |
| 45 |
let(:invalid_block_lambda) { lambda { |block| invalid_block(&block) } } |
| 46 |
end |
| 47 |
end |
| 48 |
|
| 49 |
RSpec.describe "yield_control matcher" do |
| 50 |
include YieldHelpers |
| 51 |
extend YieldHelpers |
| 52 |
|
| 53 |
it_behaves_like "an RSpec probe-yielding block-only matcher", |
| 54 |
:failure_message_uses_no_inspect => true do |
| 55 |
let(:matcher) { yield_control } |
| 56 |
def valid_block(&block) |
| 57 |
_yield_with_no_args(&block) |
| 58 |
end |
| 59 |
def invalid_block(&block) |
| 60 |
_dont_yield(&block) |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
it 'has a description' do |
| 65 |
expect(yield_control.description).to eq("yield control") |
| 66 |
end |
| 67 |
|
| 68 |
describe "expect {...}.to yield_control" do |
| 69 |
it 'passes if the block yields, regardless of the number of yielded arguments or the number of yields' do |
| 70 |
expect { |b| _yield_with_no_args(&b) }.to yield_control |
| 71 |
expect { |b| _yield_with_args(1, 2, &b) }.to yield_control |
| 72 |
expect { |b| 1.upto(10, &b) }.to yield_control |
| 73 |
end |
| 74 |
|
| 75 |
it 'passes if the block yields using instance_exec' do |
| 76 |
expect { |b| InstanceEvaler.new.yield_with_no_args(&b) }.to yield_control |
| 77 |
end |
| 78 |
|
| 79 |
it 'fails if the block does not yield' do |
| 80 |
expect { |
| 81 |
expect { |b| _dont_yield(&b) }.to yield_control |
| 82 |
}.to fail_with(/expected given block to yield control but/) |
| 83 |
end |
| 84 |
|
| 85 |
it 'fails if the block does not yield the correct number of times' do |
| 86 |
expect { |
| 87 |
expect { |b| 0.times.each(&b) }.to yield_control.at_least(:once) |
| 88 |
}.to fail_with(/expected given block to yield control at least once but did not yield/) |
| 89 |
|
| 90 |
expect { |
| 91 |
expect { |b| 2.times.each(&b) }.to yield_control.at_most(:once) |
| 92 |
}.to fail_with(/expected given block to yield control at most once but yielded twice/) |
| 93 |
|
| 94 |
expect { |
| 95 |
expect { |b| 1.times.each(&b) }.to yield_control.at_least(:twice) |
| 96 |
}.to fail_with(/expected given block to yield control at least twice but yielded once/) |
| 97 |
|
| 98 |
expect { |
| 99 |
expect { |b| 3.times.each(&b) }.to yield_control.at_least(:once).at_most(2) |
| 100 |
}.to fail_with(/expected given block to yield control between 1 and 2 times but yielded 3 times/) |
| 101 |
|
| 102 |
expect { |
| 103 |
expect { |b| 0.times.each(&b) }.to yield_control |
| 104 |
}.to fail_with(/expected given block to yield control but did not yield/) |
| 105 |
end |
| 106 |
|
| 107 |
it 'does not return a meaningful value from the block' do |
| 108 |
val = nil |
| 109 |
expect { |b| val = _yield_with_args(&b) }.to yield_control |
| 110 |
expect(val).to be_nil |
| 111 |
end |
| 112 |
|
| 113 |
it 'raises an error if given an invalid count argument' do |
| 114 |
expect { yield_control.exactly('2') }.to raise_error(ArgumentError) |
| 115 |
expect { yield_control.at_least(:trice_with_typo) }.to raise_error(ArgumentError) |
| 116 |
expect { yield_control.at_most(nil) }.to raise_error(ArgumentError) |
| 117 |
expect { yield_control.at_least(2).at_least(1) }.to raise_error(ArgumentError) |
| 118 |
expect { yield_control.at_most(2).at_most(1) }.to raise_error(ArgumentError) |
| 119 |
expect { yield_control.at_most(2).at_least(1).at_most(1) }.to raise_error(ArgumentError) |
| 120 |
expect { yield_control.at_most(1).at_least(2) }.to raise_error(ArgumentError) |
| 121 |
expect { yield_control.at_least(2).at_most(1) }.to raise_error(ArgumentError) |
| 122 |
end |
| 123 |
|
| 124 |
it 'is supports multiple calls to compatible count constraints' do |
| 125 |
expect { |b| 1.upto(4, &b) }.to yield_control.at_least(3).at_most(4).times |
| 126 |
expect { |b| 1.upto(2, &b) }.not_to yield_control.at_least(3).at_most(4).times |
| 127 |
end |
| 128 |
|
| 129 |
it 'raises an error on multiple incompatible calls to count constraints' do |
| 130 |
expect { yield_control.once.twice }.to raise_error(/multiple/i) |
| 131 |
end |
| 132 |
|
| 133 |
context "with exact count" do |
| 134 |
it 'fails if the block yields wrong number of times' do |
| 135 |
expect { |
| 136 |
expect { |b| [1, 2].each(&b) }.to yield_control.once |
| 137 |
}.to fail_with(/expected given block to yield control once but yielded twice/) |
| 138 |
|
| 139 |
expect { |
| 140 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.twice |
| 141 |
}.to fail_with(/expected given block to yield control twice but yielded 3 times/) |
| 142 |
|
| 143 |
expect { |
| 144 |
expect { |b| [1, 2].each(&b) }.to yield_control.thrice |
| 145 |
}.to fail_with(/expected given block to yield control 3 times/) |
| 146 |
end |
| 147 |
|
| 148 |
it 'passes if the block yields the specified number of times' do |
| 149 |
expect { |b| [1].each(&b) }.to yield_control.once |
| 150 |
expect { |b| [1, 2].each(&b) }.to yield_control.twice |
| 151 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.thrice |
| 152 |
expect { |b| [1, 2, 3, 4].each(&b) }.to yield_control.exactly(4).times |
| 153 |
end |
| 154 |
end |
| 155 |
|
| 156 |
context "with at_least count" do |
| 157 |
it 'passes if the block yields the given number of times' do |
| 158 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_least(2).times |
| 159 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(3).times |
| 160 |
end |
| 161 |
|
| 162 |
it 'passes if the block yields more times' do |
| 163 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(2).times |
| 164 |
expect { |b| [1, 2, 3, 4].each(&b) }.to yield_control.at_least(3).times |
| 165 |
end |
| 166 |
|
| 167 |
it 'allows :once, :twice, and :thrice to be passed as counts' do |
| 168 |
expect { |b| [1].each(&b) }.to yield_control.at_least(:once) |
| 169 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_least(:once) |
| 170 |
|
| 171 |
expect { |
| 172 |
expect { |b| [].each(&b) }.to yield_control.at_least(:once) |
| 173 |
}.to fail_with(/at least once/) |
| 174 |
|
| 175 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_least(:twice) |
| 176 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(:twice) |
| 177 |
|
| 178 |
expect { |
| 179 |
expect { |b| [1].each(&b) }.to yield_control.at_least(:twice) |
| 180 |
}.to fail_with(/at least twice/) |
| 181 |
|
| 182 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(:thrice) |
| 183 |
expect { |b| [1, 2, 3, 4].each(&b) }.to yield_control.at_least(:thrice) |
| 184 |
|
| 185 |
expect { |
| 186 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_least(:thrice) |
| 187 |
}.to fail_with(/at least 3 times/) |
| 188 |
end |
| 189 |
|
| 190 |
it 'fails if the block yields too few times' do |
| 191 |
expect { |
| 192 |
expect { |b| _yield_with_no_args(&b) }.to yield_control.at_least(2).times |
| 193 |
}.to fail_with(/expected given block to yield control at least twice/) |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
context "with at_most count" do |
| 198 |
it 'passes if the block yields the given number of times' do |
| 199 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_most(2).times |
| 200 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(3).times |
| 201 |
end |
| 202 |
|
| 203 |
it 'passes if the block yields fewer times' do |
| 204 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_most(3).times |
| 205 |
end |
| 206 |
|
| 207 |
it 'allows :once, :twice, and :thrice to be passed as counts' do |
| 208 |
expect { |b| [1].each(&b) }.to yield_control.at_most(:once) |
| 209 |
|
| 210 |
expect { |
| 211 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_most(:once) |
| 212 |
}.to fail_with(/expected given block to yield control at most once/) |
| 213 |
|
| 214 |
expect { |b| [1, 2].each(&b) }.to yield_control.at_most(:twice) |
| 215 |
|
| 216 |
expect { |
| 217 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(:twice) |
| 218 |
}.to fail_with(/expected given block to yield control at most twice/) |
| 219 |
|
| 220 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(:thrice) |
| 221 |
|
| 222 |
expect { |
| 223 |
expect { |b| [1, 2, 3, 4].each(&b) }.to yield_control.at_most(:thrice) |
| 224 |
}.to fail_with(/expected given block to yield control at most 3 times/) |
| 225 |
end |
| 226 |
|
| 227 |
it 'fails if the block yields too many times' do |
| 228 |
expect { |
| 229 |
expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(2).times |
| 230 |
}.to fail_with(/expected given block to yield control at most twice/) |
| 231 |
end |
| 232 |
end |
| 233 |
end |
| 234 |
|
| 235 |
describe "expect {...}.not_to yield_control" do |
| 236 |
it 'passes if the block does not yield' do |
| 237 |
expect { |b| _dont_yield(&b) }.not_to yield_control |
| 238 |
end |
| 239 |
|
| 240 |
it 'fails if the block does yield' do |
| 241 |
expect { |
| 242 |
expect { |b| _yield_with_no_args(&b) }.not_to yield_control |
| 243 |
}.to fail_with(/expected given block not to yield control/) |
| 244 |
end |
| 245 |
|
| 246 |
it 'fails if the expect block does not accept an argument', :if => (RUBY_VERSION.to_f > 1.8) do |
| 247 |
expect { |
| 248 |
expect {}.not_to yield_control |
| 249 |
}.to raise_error(/expect block must accept an argument/) |
| 250 |
end |
| 251 |
|
| 252 |
it 'still works when the block uses an arg splat' do |
| 253 |
expect { |*args| _dont_yield(&args.first) }.not_to yield_control |
| 254 |
end |
| 255 |
|
| 256 |
it 'raises an error if the expect block arg is not passed to a method as a block' do |
| 257 |
expect { |
| 258 |
expect { |b| }.not_to yield_control |
| 259 |
}.to raise_error(/must pass the argument.*as a block/) |
| 260 |
end |
| 261 |
end |
| 262 |
end |
| 263 |
|
| 264 |
RSpec.describe "yield_with_no_args matcher" do |
| 265 |
include YieldHelpers |
| 266 |
extend YieldHelpers |
| 267 |
|
| 268 |
it_behaves_like "an RSpec probe-yielding block-only matcher" do |
| 269 |
let(:matcher) { yield_with_no_args } |
| 270 |
def valid_block(&block) |
| 271 |
_yield_with_no_args(&block) |
| 272 |
end |
| 273 |
def invalid_block(&block) |
| 274 |
_yield_with_args(1, &block) |
| 275 |
end |
| 276 |
end |
| 277 |
|
| 278 |
it 'has a description' do |
| 279 |
expect(yield_with_no_args.description).to eq("yield with no args") |
| 280 |
end |
| 281 |
|
| 282 |
it 'does not return a meaningful value from the block' do |
| 283 |
val = nil |
| 284 |
expect { |b| val = _yield_with_no_args(&b) }.to yield_with_no_args |
| 285 |
expect(val).to be_nil |
| 286 |
end |
| 287 |
|
| 288 |
describe "expect {...}.to yield_with_no_args" do |
| 289 |
it 'passes if the block yields with no args' do |
| 290 |
expect { |b| _yield_with_no_args(&b) }.to yield_with_no_args |
| 291 |
end |
| 292 |
|
| 293 |
it 'passes if the block yields with no args using instance_exec' do |
| 294 |
expect { |b| InstanceEvaler.new.yield_with_no_args(&b) }.to yield_with_no_args |
| 295 |
end |
| 296 |
|
| 297 |
it 'fails if the block does not yield' do |
| 298 |
expect { |
| 299 |
expect { |b| _dont_yield(&b) }.to yield_with_no_args |
| 300 |
}.to fail_with(/expected given block to yield with no arguments, but did not yield/) |
| 301 |
end |
| 302 |
|
| 303 |
it 'fails if the block yields with args' do |
| 304 |
expect { |
| 305 |
expect { |b| _yield_with_args(1, &b) }.to yield_with_no_args |
| 306 |
}.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/) |
| 307 |
end |
| 308 |
|
| 309 |
it 'fails if the block yields with arg false' do |
| 310 |
expect { |
| 311 |
expect { |b| _yield_with_args(false, &b) }.to yield_with_no_args |
| 312 |
}.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/) |
| 313 |
end |
| 314 |
|
| 315 |
it 'raises an error if it yields multiple times' do |
| 316 |
expect { |
| 317 |
expect { |b| [1, 2].each(&b) }.to yield_with_no_args |
| 318 |
}.to raise_error(/not designed.*yields multiple times/) |
| 319 |
end |
| 320 |
end |
| 321 |
|
| 322 |
describe "expect {...}.not_to yield_with_no_args" do |
| 323 |
it "passes if the block does not yield" do |
| 324 |
expect { |b| _dont_yield(&b) }.not_to yield_with_no_args |
| 325 |
end |
| 326 |
|
| 327 |
it "passes if the block yields with args" do |
| 328 |
expect { |b| _yield_with_args(1, &b) }.not_to yield_with_no_args |
| 329 |
end |
| 330 |
|
| 331 |
it "fails if the block yields with no args" do |
| 332 |
expect { |
| 333 |
expect { |b| _yield_with_no_args(&b) }.not_to yield_with_no_args |
| 334 |
}.to fail_with(/expected given block not to yield with no arguments, but did/) |
| 335 |
end |
| 336 |
|
| 337 |
it 'fails if the expect block does not accept an argument', :if => (RUBY_VERSION.to_f > 1.8) do |
| 338 |
expect { |
| 339 |
expect {}.not_to yield_with_no_args |
| 340 |
}.to raise_error(/expect block must accept an argument/) |
| 341 |
end |
| 342 |
|
| 343 |
it 'raises an error if the expect block arg is not passed to a method as a block' do |
| 344 |
expect { |
| 345 |
expect { |b| }.not_to yield_with_no_args |
| 346 |
}.to raise_error(/must pass the argument.*as a block/) |
| 347 |
end |
| 348 |
end |
| 349 |
end |
| 350 |
|
| 351 |
RSpec.describe "yield_with_args matcher" do |
| 352 |
include YieldHelpers |
| 353 |
extend YieldHelpers |
| 354 |
|
| 355 |
it_behaves_like "an RSpec probe-yielding block-only matcher" do |
| 356 |
let(:matcher) { yield_with_args(1) } |
| 357 |
def valid_block(&block) |
| 358 |
_yield_with_args(1, &block) |
| 359 |
end |
| 360 |
def invalid_block(&block) |
| 361 |
_yield_with_args(2, &block) |
| 362 |
end |
| 363 |
end |
| 364 |
|
| 365 |
it 'has a description' do |
| 366 |
expect(yield_with_args.description).to eq("yield with args") |
| 367 |
expect(yield_with_args(1, 3).description).to eq("yield with args(1, 3)") |
| 368 |
expect(yield_with_args(false).description).to eq("yield with args(false)") |
| 369 |
end |
| 370 |
|
| 371 |
it 'does not return a meaningful value from the block' do |
| 372 |
val = nil |
| 373 |
expect { |b| val = _yield_with_args(1, &b) }.to yield_with_args(1) |
| 374 |
expect(val).to be_nil |
| 375 |
end |
| 376 |
|
| 377 |
describe "expect {...}.to yield_with_args" do |
| 378 |
it 'passes if the block yields with arguments' do |
| 379 |
expect { |b| _yield_with_args(1, &b) }.to yield_with_args |
| 380 |
end |
| 381 |
|
| 382 |
it 'passes if the matchers match at yield time only' do |
| 383 |
expect { |b| |
| 384 |
val = [] |
| 385 |
_yield_with_args(val, &b) |
| 386 |
val << 1 |
| 387 |
}.to yield_with_args(be_empty) |
| 388 |
end |
| 389 |
|
| 390 |
it 'fails if the block does not yield' do |
| 391 |
expect { |
| 392 |
expect { |b| _dont_yield(&b) }.to yield_with_args |
| 393 |
}.to fail_with(/expected given block to yield with arguments, but did not yield/) |
| 394 |
end |
| 395 |
|
| 396 |
it 'fails if the block yields with no arguments' do |
| 397 |
expect { |
| 398 |
expect { |b| _yield_with_no_args(&b) }.to yield_with_args |
| 399 |
}.to fail_with(/expected given block to yield with arguments, but yielded with no arguments/) |
| 400 |
end |
| 401 |
|
| 402 |
it 'fails if the matchers match at return time only' do |
| 403 |
expect { |
| 404 |
expect { |b| |
| 405 |
val = [1] |
| 406 |
_yield_with_args(val, &b) |
| 407 |
val.clear |
| 408 |
}.to yield_with_args(be_empty) |
| 409 |
}.to fail_with(dedent <<-EOS) |
| 410 |
|expected given block to yield with arguments, but yielded with unexpected arguments |
| 411 |
|expected: [(be empty)] |
| 412 |
| got: [[1]] |
| 413 |
EOS |
| 414 |
end |
| 415 |
|
| 416 |
it 'raises an error if it yields multiple times' do |
| 417 |
expect { |
| 418 |
expect { |b| [1, 2].each(&b) }.to yield_with_args |
| 419 |
}.to raise_error(/not designed.*yields multiple times/) |
| 420 |
end |
| 421 |
end |
| 422 |
|
| 423 |
describe "expect {...}.not_to yield_with_args" do |
| 424 |
it 'fails if the block yields with arguments' do |
| 425 |
expect { |
| 426 |
expect { |b| _yield_with_args(1, &b) }.not_to yield_with_args |
| 427 |
}.to fail_with(/expected given block not to yield with arguments, but did/) |
| 428 |
end |
| 429 |
|
| 430 |
it 'fails if the matchers match at yield time only' do |
| 431 |
expect { |
| 432 |
expect { |b| |
| 433 |
val = [] |
| 434 |
_yield_with_args(val, &b) |
| 435 |
val << 1 |
| 436 |
}.not_to yield_with_args(be_empty) |
| 437 |
}.to fail_with(dedent <<-EOS) |
| 438 |
|expected given block not to yield with arguments, but yielded with expected arguments |
| 439 |
|expected not: [(be empty)] |
| 440 |
| got: [[]] |
| 441 |
EOS |
| 442 |
end |
| 443 |
|
| 444 |
it 'passes if the block does not yield' do |
| 445 |
expect { |b| _dont_yield(&b) }.not_to yield_with_args |
| 446 |
end |
| 447 |
|
| 448 |
it 'passes if the block yields with no arguments' do |
| 449 |
expect { |b| _yield_with_no_args(&b) }.not_to yield_with_args |
| 450 |
end |
| 451 |
|
| 452 |
it 'passes if the matchers match at return time only' do |
| 453 |
expect { |b| |
| 454 |
val = [1] |
| 455 |
_yield_with_args(val, &b) |
| 456 |
val.clear |
| 457 |
}.not_to yield_with_args(be_empty) |
| 458 |
end |
| 459 |
|
| 460 |
it 'fails if the expect block does not accept an argument', :if => (RUBY_VERSION.to_f > 1.8) do |
| 461 |
expect { |
| 462 |
expect {}.not_to yield_with_args |
| 463 |
}.to raise_error(/expect block must accept an argument/) |
| 464 |
end |
| 465 |
|
| 466 |
it 'raises an error if the expect block arg is not passed to a method as a block' do |
| 467 |
expect { |
| 468 |
expect { |b| }.not_to yield_with_args |
| 469 |
}.to raise_error(/must pass the argument.*as a block/) |
| 470 |
end |
| 471 |
end |
| 472 |
|
| 473 |
describe "expect {...}.to yield_with_args(3, 17)" do |
| 474 |
it 'passes if the block yields with the given arguments' do |
| 475 |
expect { |b| _yield_with_args(3, 17, &b) }.to yield_with_args(3, 17) |
| 476 |
end |
| 477 |
|
| 478 |
it 'passes if the block yields with the given arguments using instance_exec' do |
| 479 |
expect { |b| InstanceEvaler.new.yield_with_args(3, 17, &b) }.to yield_with_args(3, 17) |
| 480 |
end |
| 481 |
|
| 482 |
it 'fails if the block does not yield' do |
| 483 |
expect { |
| 484 |
expect { |b| _dont_yield(&b) }.to yield_with_args(3, 17) |
| 485 |
}.to fail_with(/expected given block to yield with arguments, but did not yield/) |
| 486 |
end |
| 487 |
|
| 488 |
it 'fails if the block yields with no arguments' do |
| 489 |
expect { |
| 490 |
expect { |b| _yield_with_no_args(&b) }.to yield_with_args(3, 17) |
| 491 |
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) |
| 492 |
end |
| 493 |
|
| 494 |
it 'fails if the block yields with different arguments' do |
| 495 |
expect { |
| 496 |
expect { |b| _yield_with_args("a", "b", &b) }.to yield_with_args("a", "c") |
| 497 |
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) |
| 498 |
end |
| 499 |
end |
| 500 |
|
| 501 |
describe "expect {...}.to yield_with_args(matcher, matcher)" do |
| 502 |
it 'passes when the matchers match the args' do |
| 503 |
expect { |b| |
| 504 |
_yield_with_args(1.1, "food", &b) |
| 505 |
}.to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/)) |
| 506 |
end |
| 507 |
|
| 508 |
it 'provides a description' do |
| 509 |
description = yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/)).description |
| 510 |
expect(description).to eq("yield with args(a value within 0.2 of 1, a string matching /foo/)") |
| 511 |
end |
| 512 |
|
| 513 |
it 'fails with a useful error message when the matchers do not match the args' do |
| 514 |
expect { |
| 515 |
expect { |b| |
| 516 |
_yield_with_args(2.1, "food", &b) |
| 517 |
}.to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/)) |
| 518 |
}.to fail_with(dedent <<-EOS) |
| 519 |
|expected given block to yield with arguments, but yielded with unexpected arguments |
| 520 |
|expected: [(a value within 0.2 of 1), (a string matching /foo/)] |
| 521 |
| got: [2.1, "food"] |
| 522 |
EOS |
| 523 |
end |
| 524 |
end |
| 525 |
|
| 526 |
describe "expect {...}.not_to yield_with_args(3, 17)" do |
| 527 |
it 'passes if the block yields with different arguments' do |
| 528 |
expect { |b| _yield_with_args("a", "b", &b) }.not_to yield_with_args("a", "c") |
| 529 |
end |
| 530 |
|
| 531 |
it 'fails if the block yields with the given arguments' do |
| 532 |
expect { |
| 533 |
expect { |b| _yield_with_args("a", "b", &b) }.not_to yield_with_args("a", "b") |
| 534 |
}.to fail_with(/expected given block not to yield with arguments, but yielded with expected arguments/) |
| 535 |
end |
| 536 |
end |
| 537 |
|
| 538 |
describe "expect {...}.not_to yield_with_args(matcher, matcher)" do |
| 539 |
it 'passes when the matchers do not match the args' do |
| 540 |
expect { |b| |
| 541 |
_yield_with_args(2.1, "food", &b) |
| 542 |
}.not_to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/)) |
| 543 |
end |
| 544 |
|
| 545 |
it 'fails with a useful error message when the matchers do not match the args' do |
| 546 |
expect { |
| 547 |
expect { |b| |
| 548 |
_yield_with_args(1.1, "food", &b) |
| 549 |
}.not_to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/)) |
| 550 |
}.to fail_with(dedent <<-EOS) |
| 551 |
|expected given block not to yield with arguments, but yielded with expected arguments |
| 552 |
|expected not: [(a value within 0.2 of 1), (a string matching /foo/)] |
| 553 |
| got: [1.1, "food"] |
| 554 |
EOS |
| 555 |
end |
| 556 |
end |
| 557 |
|
| 558 |
describe "expect {...}.to yield_with_args( false )" do |
| 559 |
it 'passes if the block yields with the given arguments' do |
| 560 |
expect { |b| _yield_with_args(false, &b) }.to yield_with_args(false) |
| 561 |
end |
| 562 |
|
| 563 |
it 'passes if the block yields with the given arguments using instance_exec' do |
| 564 |
expect { |b| InstanceEvaler.new.yield_with_args(false, &b) }.to yield_with_args(false) |
| 565 |
end |
| 566 |
|
| 567 |
it 'fails if the block does not yield' do |
| 568 |
expect { |
| 569 |
expect { |b| _dont_yield(&b) }.to yield_with_args(false) |
| 570 |
}.to fail_with(/expected given block to yield with arguments, but did not yield/) |
| 571 |
end |
| 572 |
|
| 573 |
it 'fails if the block yields with no arguments' do |
| 574 |
expect { |
| 575 |
expect { |b| _yield_with_no_args(&b) }.to yield_with_args(false) |
| 576 |
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) |
| 577 |
end |
| 578 |
|
| 579 |
it 'fails if the block yields with different arguments' do |
| 580 |
expect { |
| 581 |
expect { |b| _yield_with_args(false, &b) }.to yield_with_args(true) |
| 582 |
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) |
| 583 |
end |
| 584 |
end |
| 585 |
|
| 586 |
describe "expect {...}.to yield_with_args(/reg/, /ex/)" do |
| 587 |
it "passes if the block yields strings matching the regexes" do |
| 588 |
expect { |b| _yield_with_args("regular", "expression", &b) }.to yield_with_args(/reg/, /ex/) |
| 589 |
end |
| 590 |
|
| 591 |
it "fails if the block yields strings that do not match the regexes" do |
| 592 |
expect { |
| 593 |
expect { |b| _yield_with_args("no", "match", &b) }.to yield_with_args(/reg/, /ex/) |
| 594 |
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) |
| 595 |
end |
| 596 |
end |
| 597 |
|
| 598 |
describe "expect {...}.to yield_with_args(String, Integer)" do |
| 599 |
it "passes if the block yields objects of the given classes" do |
| 600 |
expect { |b| _yield_with_args("string", 15, &b) }.to yield_with_args(String, Integer) |
| 601 |
end |
| 602 |
|
| 603 |
it "passes if the block yields the given classes" do |
| 604 |
expect { |b| _yield_with_args(String, Integer, &b) }.to yield_with_args(String, Integer) |
| 605 |
end |
| 606 |
|
| 607 |
it "fails if the block yields objects of different classes" do |
| 608 |
expect { |
| 609 |
expect { |b| _yield_with_args(15, "string", &b) }.to yield_with_args(String, Integer) |
| 610 |
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/) |
| 611 |
end |
| 612 |
end |
| 613 |
end |
| 614 |
|
| 615 |
RSpec.describe "yield_successive_args matcher" do |
| 616 |
include YieldHelpers |
| 617 |
extend YieldHelpers |
| 618 |
|
| 619 |
it_behaves_like "an RSpec probe-yielding block-only matcher" do |
| 620 |
let(:matcher) { yield_successive_args(1, 2) } |
| 621 |
def valid_block(&block) |
| 622 |
[1, 2].each(&block) |
| 623 |
end |
| 624 |
def invalid_block(&block) |
| 625 |
[3, 4].each(&block) |
| 626 |
end |
| 627 |
end |
| 628 |
|
| 629 |
it 'has a description' do |
| 630 |
expect(yield_successive_args(1, 3).description).to eq("yield successive args(1, 3)") |
| 631 |
expect(yield_successive_args([:a, 1], [:b, 2]).description).to eq("yield successive args([:a, 1], [:b, 2])") |
| 632 |
end |
| 633 |
|
| 634 |
it 'does not return a meaningful value from the block' do |
| 635 |
val = nil |
| 636 |
expect { |b| val = _yield_with_args(1, &b) }.to yield_successive_args(1) |
| 637 |
expect(val).to be_nil |
| 638 |
end |
| 639 |
|
| 640 |
it "works correctly when the method yields multiple args each time (passing case)" do |
| 641 |
expect { |block| |
| 642 |
['football', 'barstool'].each_with_index(&block) |
| 643 |
}.to yield_successive_args( |
| 644 |
[/foo/, 0], |
| 645 |
[a_string_starting_with('bar'), 1] |
| 646 |
) |
| 647 |
end |
| 648 |
|
| 649 |
it "works correctly when the method yields multiple args each time (failing case)" do |
| 650 |
expect { |
| 651 |
expect { |block| |
| 652 |
['football', 'barstool'].each_with_index(&block) |
| 653 |
}.to yield_successive_args( |
| 654 |
[/foo/, 0], |
| 655 |
[a_string_starting_with('abr'), 1] |
| 656 |
) |
| 657 |
}.to fail_with(dedent <<-EOS) |
| 658 |
|expected given block to yield successively with arguments, but yielded with unexpected arguments |
| 659 |
|expected: [[/foo/, 0], [(a string starting with "abr"), 1]] |
| 660 |
| got: [["football", 0], ["barstool", 1]] |
| 661 |
EOS |
| 662 |
end |
| 663 |
|
| 664 |
describe "expect {...}.to yield_successive_args([:a, 1], [:b, 2])" do |
| 665 |
it 'passes when the block successively yields the given args' do |
| 666 |
expect { |b| [[:a, 1], [:b, 2]].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) |
| 667 |
end |
| 668 |
|
| 669 |
it 'passes if matched at yield time only' do |
| 670 |
expect { |b| |
| 671 |
[[:a, 1], [:b, 2]].each do |val| |
| 672 |
_yield_with_args(val, &b) |
| 673 |
val.clear |
| 674 |
end |
| 675 |
}.to yield_successive_args([:a, 1], [:b, 2]) |
| 676 |
end |
| 677 |
|
| 678 |
it 'fails when the block does not yield that many times' do |
| 679 |
expect { |
| 680 |
expect { |b| [[:a, 1]].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) |
| 681 |
}.to fail_with(/but yielded with unexpected arguments/) |
| 682 |
end |
| 683 |
|
| 684 |
it 'fails when the block yields the right number of times but with different arguments' do |
| 685 |
expect { |
| 686 |
expect { |b| [[:a, 1], [:b, 3]].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) |
| 687 |
}.to fail_with(/but yielded with unexpected arguments/) |
| 688 |
end |
| 689 |
|
| 690 |
it 'fails if matched at return time only' do |
| 691 |
expect { |
| 692 |
expect { |b| |
| 693 |
[[:a, 1], [:b, 2]].each do |eventual| |
| 694 |
initial = [] |
| 695 |
_yield_with_args(initial, &b) |
| 696 |
initial.concat(eventual) |
| 697 |
end |
| 698 |
}.to yield_successive_args([:a, 1], [:b, 2]) |
| 699 |
}.to fail_with(dedent <<-EOS) |
| 700 |
|expected given block to yield successively with arguments, but yielded with unexpected arguments |
| 701 |
|expected: [[:a, 1], [:b, 2]] |
| 702 |
| got: [[], []] |
| 703 |
EOS |
| 704 |
end |
| 705 |
end |
| 706 |
|
| 707 |
describe "expect {...}.to yield_successive_args(1, 2, 3)" do |
| 708 |
it 'passes when the block successively yields the given args' do |
| 709 |
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3) |
| 710 |
end |
| 711 |
|
| 712 |
it 'passes when the block successively yields the given args using instance_exec' do |
| 713 |
expect { |b| InstanceEvaler.new.each_arg(1, 2, 3, &b) }.to yield_successive_args(1, 2, 3) |
| 714 |
end |
| 715 |
|
| 716 |
it 'fails when the block does not yield the expected args' do |
| 717 |
expect { |
| 718 |
expect { |b| [1, 2, 4].each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) |
| 719 |
}.to fail_with(/but yielded with unexpected arguments/) |
| 720 |
end |
| 721 |
end |
| 722 |
|
| 723 |
describe "expect {...}.to yield_successive_args(matcher, matcher)" do |
| 724 |
it 'passes when the successively yielded args match the matchers' do |
| 725 |
expect { |b| |
| 726 |
%w[ food barn ].each(&b) |
| 727 |
}.to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)) |
| 728 |
end |
| 729 |
|
| 730 |
it 'fails when the successively yielded args do not match the matchers' do |
| 731 |
expect { |
| 732 |
expect { |b| |
| 733 |
%w[ barn food ].each(&b) |
| 734 |
}.to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)) |
| 735 |
}.to fail_with(dedent <<-EOS) |
| 736 |
|expected given block to yield successively with arguments, but yielded with unexpected arguments |
| 737 |
|expected: [(a string matching /foo/), (a string matching /bar/)] |
| 738 |
| got: ["barn", "food"] |
| 739 |
EOS |
| 740 |
end |
| 741 |
|
| 742 |
it 'provides a description' do |
| 743 |
description = yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)).description |
| 744 |
expect(description).to eq("yield successive args(a string matching /foo/, a string matching /bar/)") |
| 745 |
end |
| 746 |
end |
| 747 |
|
| 748 |
describe "expect {...}.not_to yield_successive_args(1, 2, 3)" do |
| 749 |
it 'passes when the block does not yield' do |
| 750 |
expect { |b| _dont_yield(&b) }.not_to yield_successive_args(1, 2, 3) |
| 751 |
end |
| 752 |
|
| 753 |
it 'passes when the block yields the wrong number of times' do |
| 754 |
expect { |b| [1, 2].each(&b) }.not_to yield_successive_args(1, 2, 3) |
| 755 |
end |
| 756 |
|
| 757 |
it 'passes when the block yields the wrong arguments' do |
| 758 |
expect { |b| [1, 2, 4].each(&b) }.not_to yield_successive_args(1, 2, 3) |
| 759 |
end |
| 760 |
|
| 761 |
it 'fails when the block yields the given arguments' do |
| 762 |
expect { |
| 763 |
expect { |b| [1, 2, 3].each(&b) }.not_to yield_successive_args(1, 2, 3) |
| 764 |
}.to fail_with(/expected given block not to yield successively/) |
| 765 |
end |
| 766 |
|
| 767 |
it 'fails if the expect block does not accept an argument', :if => (RUBY_VERSION.to_f > 1.8) do |
| 768 |
expect { |
| 769 |
expect {}.not_to yield_successive_args(1, 2, 3) |
| 770 |
}.to raise_error(/expect block must accept an argument/) |
| 771 |
end |
| 772 |
|
| 773 |
it 'raises an error if the expect block arg is not passed to a method as a block' do |
| 774 |
expect { |
| 775 |
expect { |b| }.not_to yield_successive_args(1, 2, 3) |
| 776 |
}.to raise_error(/must pass the argument.*as a block/) |
| 777 |
end |
| 778 |
end |
| 779 |
|
| 780 |
describe "expect {...}.not_to yield_successive_args(matcher, matcher)" do |
| 781 |
it 'passes when the successively yielded args do not match the matchers' do |
| 782 |
expect { |b| |
| 783 |
%w[ barn food ].each(&b) |
| 784 |
}.not_to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)) |
| 785 |
end |
| 786 |
|
| 787 |
it 'passes when the successively yielded args do not match the matchers (at yield time only)' do |
| 788 |
expect { |b| |
| 789 |
%w[ food barn ].each do |eventual| |
| 790 |
initial = '' |
| 791 |
_yield_with_args(initial, &b) |
| 792 |
initial + eventual |
| 793 |
end |
| 794 |
}.not_to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)) |
| 795 |
end |
| 796 |
|
| 797 |
it 'fails when the successively yielded args match the matchers' do |
| 798 |
expect { |
| 799 |
expect { |b| |
| 800 |
%w[ food barn ].each(&b) |
| 801 |
}.not_to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)) |
| 802 |
}.to fail_with(dedent <<-EOS) |
| 803 |
|expected given block not to yield successively with arguments, but yielded with expected arguments |
| 804 |
|expected not: [(a string matching /foo/), (a string matching /bar/)] |
| 805 |
| got: ["food", "barn"] |
| 806 |
EOS |
| 807 |
end |
| 808 |
|
| 809 |
it 'fails when the successively yielded args match the matchers (at yield time only)' do |
| 810 |
values = %w[ food barn ].collect { |value| value.dup } |
| 811 |
expect { |
| 812 |
expect { |b| |
| 813 |
values.each do |val| |
| 814 |
_yield_with_args(val, &b) |
| 815 |
val.sub!(/.+/, '') |
| 816 |
end |
| 817 |
}.not_to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)) |
| 818 |
}.to fail_with(dedent <<-EOS) |
| 819 |
|expected given block not to yield successively with arguments, but yielded with expected arguments |
| 820 |
|expected not: [(a string matching /foo/), (a string matching /bar/)] |
| 821 |
| got: ["food", "barn"] |
| 822 |
EOS |
| 823 |
end |
| 824 |
end |
| 825 |
|
| 826 |
describe "expect {...}.to yield_successive_args(String, Integer)" do |
| 827 |
it "passes if the block successively yields objects of the given classes" do |
| 828 |
expect { |b| ["string", 15].each(&b) }.to yield_successive_args(String, Integer) |
| 829 |
end |
| 830 |
|
| 831 |
it "passes if the block yields the given classes" do |
| 832 |
expect { |b| [String, Integer].each(&b) }.to yield_successive_args(String, Integer) |
| 833 |
end |
| 834 |
|
| 835 |
it "fails if the block yields objects of different classes" do |
| 836 |
expect { |
| 837 |
expect { |b| [15, "string"].each(&b) }.to yield_successive_args(String, Integer) |
| 838 |
}.to fail_with(/expected given block to yield successively with arguments/) |
| 839 |
end |
| 840 |
end |
| 841 |
end |