rspec-expectations icon indicating copy to clipboard operation
rspec-expectations copied to clipboard

`include` breaks with a time/float ranges

Open pirj opened this issue 4 years ago • 5 comments

Subject of the issue

include breaks with a time range

Originally reported by @schwern in https://github.com/rubocop-hq/rubocop-rspec/issues/926

Your environment

  • Ruby version: 2.7.1
  • rspec-expectations version: master (eb1787f)

Steps to reproduce

RSpec.describe 'include' do
  it do # works
    range = (3..5)
    expect(range).not_to include(6)
  end

  range = (Time.now-20...Time.now-10)
  now = Time.now

  it do # works
    expect(range.include?(now)).not_to be true
  end

  it do # breaks
    expect(range).not_to include(now)
  end
end

Expected behavior

All examples pass

Actual behavior

The example with a time range and include matcher fails with:

  1) include is expected not to include 2020-06-11 10:47:01.120378000 +0300
     Failure/Error: expect(range).not_to include(now)

     TypeError:
       can't iterate from Time
     # ./lib/rspec/matchers/built_in/include.rb:134:in `each'
     # ./lib/rspec/matchers/built_in/include.rb:134:in `any?'
     # ./lib/rspec/matchers/built_in/include.rb:134:in `actual_collection_includes?'

pirj avatar Jun 11 '20 07:06 pirj

It breaks on Float as well.

require 'rspec'

describe 'include' do
  it 'is not equivalent' do
    obj = 2.5
    range = (obj...obj)
    expect(range.include?(obj)).to be_falsey  # pass
    expect(range).not_to include(obj)  # TypeError: can't iterate from Float
  end
end

schwern avatar Jun 14 '20 18:06 schwern

Hi @pirj, @schwern, @JonRowe! I just put out a PR that attempts to address this issue. Would one of you mind giving it a review when you get a chance?

bclayman-sq avatar Oct 04 '21 20:10 bclayman-sq

@bclayman-sq Certainly. We're always watching. 👀

pirj avatar Oct 04 '21 21:10 pirj

Hi @pirj, thanks again for kicking off those builds for me!

I've fixed up a couple issues causing a failing build and think this one should pass. In particular, I've now targeted ruby versions >= 2.1.9 for this improvement. Assuming CI passes, I'll update the docs to indicate these improvements are available for >= 2.1.9. How does that sound to you?

bclayman-sq avatar Oct 05 '21 19:10 bclayman-sq

Bump.

schwern avatar Dec 14 '23 00:12 schwern