rspec / rspec-expectations

Provides a readable API to express expected outcomes of a code example
https://rspec.info
MIT License
1.26k stars 397 forks source link

`include` breaks with a time/float ranges #1191

Open pirj opened 4 years ago

pirj commented 4 years ago

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

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?'
schwern commented 4 years ago

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
bclayman-sq commented 2 years ago

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?

pirj commented 2 years ago

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

bclayman-sq commented 2 years ago

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?

schwern commented 9 months ago

Bump.