1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Merge pull request #2303 from teamcapybara/index_tests

Fix results with negative max bounded ranges
This commit is contained in:
Thomas Walpole 2020-02-04 08:03:29 -08:00 committed by GitHub
commit 06f6c3dbd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 17 deletions

View file

@ -63,6 +63,7 @@ module Capybara
# idx.max is broken with beginless ranges
# idx.end && idx.max # endless range will have end == nil
max = idx.end
max = nil if max&.negative?
max -= 1 if max && idx.exclude_end?
max
end

View file

@ -65,25 +65,48 @@ RSpec.describe Capybara::Result do
end).to eq(2)
end
it 'supports all modes of []' do
expect(result[1].text).to eq 'Beta'
expect(result[0, 2].map(&:text)).to eq %w[Alpha Beta]
expect(result[1..3].map(&:text)).to eq %w[Beta Gamma Delta]
expect(result[-1].text).to eq 'Delta'
expect(result[-2, 3].map(&:text)).to eq %w[Gamma Delta]
expect(result[1..7].map(&:text)).to eq %w[Beta Gamma Delta]
expect(result[1...3].map(&:text)).to eq %w[Beta Gamma]
expect(result[2..-1].map(&:text)).to eq %w[Gamma Delta]
expect(result[2...-1].map(&:text)).to eq %w[Gamma]
eval <<~TEST, binding, __FILE__, __LINE__ + 1 if RUBY_VERSION.to_f > 2.5
expect(result[2..].map(&:text)).to eq %w[Gamma Delta]
TEST
eval <<~TEST, binding, __FILE__, __LINE__ + 1 if RUBY_VERSION.to_f > 2.6
expect(result[..2].map(&:text)).to eq %w[Alpha Beta Gamma]
expect(result[...2].map(&:text)).to eq %w[Alpha Beta]
TEST
def recalc_result
string.all '//li', minimum: 0 # pass minimum: 0 so lazy evaluation doesn't get triggered yet
end
it 'supports all modes of []' do
expect(recalc_result[1].text).to eq 'Beta'
expect(recalc_result[0, 2].map(&:text)).to eq %w[Alpha Beta]
expect(recalc_result[1..3].map(&:text)).to eq %w[Beta Gamma Delta]
expect(recalc_result[-1].text).to eq 'Delta'
expect(recalc_result[-2, 3].map(&:text)).to eq %w[Gamma Delta]
expect(recalc_result[1...3].map(&:text)).to eq %w[Beta Gamma]
expect(recalc_result[1..7].map(&:text)).to eq %w[Beta Gamma Delta]
expect(recalc_result[2...-1].map(&:text)).to eq %w[Gamma]
expect(recalc_result[2..-1].map(&:text)).to eq %w[Gamma Delta]
end
eval <<~TEST, binding, __FILE__, __LINE__ + 1 if RUBY_VERSION.to_f > 2.5
it 'supports endless ranges' do
expect(result[2..].map(&:text)).to eq %w[Gamma Delta]
end
TEST
eval <<~TEST, binding, __FILE__, __LINE__ + 1 if RUBY_VERSION.to_f > 2.6
it 'supports inclusive positive beginless ranges' do
expect(result[..2].map(&:text)).to eq %w[Alpha Beta Gamma]
end
it 'supports inclusive negative beginless ranges' do
expect(result[..-2].map(&:text)).to eq %w[Alpha Beta Gamma]
expect(result[..-1].map(&:text)).to eq %w[Alpha Beta Gamma Delta]
end
it 'supports exclusive positive beginless ranges' do
expect(result[...2].map(&:text)).to eq %w[Alpha Beta]
end
it 'supports exclusive negative beginless ranges' do
expect(result[...-2].map(&:text)).to eq %w[Alpha Beta]
expect(result[...-1].map(&:text)).to eq %w[Alpha Beta Gamma]
end
TEST
it 'works with filter blocks' do
result = string.all('//li') { |node| node.text == 'Alpha' }
expect(result.size).to eq 1