rspec / rspec/rspec

Range comparison takes longer than needed for sufficiently large ranges when expectation not met

Open
#81 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
117
Forks
51
Avg merge
59m
Merged PRs (30d)
3

Description

Due to a failed expectation running a diff on the objects involved, an expectation which involves sufficiently large ranges will iterate over every element of each range for message output upon failure. This is due to line 27 of lib/rspec/expectations/fail_with.rb

Example:

expect(1..10).to eq (1..2)

will result in:

RSpec::Expectations::ExpectationNotMetError:
expected: 1..2
     got: 1..10

(compared using ==)

Diff:
@@ -1,2 +1,2 @@
-[1, 2]
+[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

As you can see, if the range is of, say a date range spanning years, or a boundary is Float::INFINITY, the diff can take a lot of unnecessary time and screen space.

Suggested fix: wrap a conditional to check if expected or actual are ranges, and specialize output to deal with the boundaries of those ranges, rather than each element.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at line 27 of lib/rspec/expectations/fail_with.rb and reproduce the range comparison example from the issue. Trace how the failure message invokes the diff for large or unbounded ranges, then verify that the resulting output avoids iterating through every range element while still identifying the expected and actual boundaries.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.