From 4a380967fd790714b89a714289b784f7ab78b756 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Sat, 25 Jan 2020 17:18:07 -0800 Subject: [PATCH] Support beginless ranges --- History.md | 6 ++++++ lib/capybara/queries/base_query.rb | 3 ++- lib/capybara/result.rb | 10 ++++++++-- lib/capybara/spec/session/all_spec.rb | 7 +++++++ spec/result_spec.rb | 4 ++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/History.md b/History.md index 34303e57..2db59b6f 100644 --- a/History.md +++ b/History.md @@ -1,10 +1,16 @@ # Version 3.31.0 Release date: unreleased +### Added + * Support setting range inputs with the selenium driver [Andrew White] * Support setting range inputs with the rack driver * Support drop modifier keys in drag & drop with selenium driver [Elliot Crosby-McCullough] * `enabled_options` and `disabled options` filters for select selector +* Support beingless ranges + +### Fixed + * Fix Ruby 2.7 deprecation notices around keyword arguments. I have tried to do this without any breaking changes, but due to the nature of the 2.7 changes and some selector types accepting Hashes as locators there are a lot of edge cases. If you find any broken cases please report diff --git a/lib/capybara/queries/base_query.rb b/lib/capybara/queries/base_query.rb index 607098c0..a810c614 100644 --- a/lib/capybara/queries/base_query.rb +++ b/lib/capybara/queries/base_query.rb @@ -79,7 +79,8 @@ module Capybara if count message << " #{occurrences count}" elsif between - message << " between #{between.first} and #{between.end ? between.last : 'infinite'} times" + message << " between #{between.begin ? between.first : 1} and" \ + " #{between.end ? between.last : 'infinite'} times" elsif maximum message << " at most #{occurrences maximum}" elsif minimum diff --git a/lib/capybara/result.rb b/lib/capybara/result.rb index 1bd3aa40..2d9c72d9 100644 --- a/lib/capybara/result.rb +++ b/lib/capybara/result.rb @@ -60,7 +60,11 @@ module Capybara nil end when Range - idx.end && idx.max # endless range will have end == nil + # idx.max is broken with beginless ranges + # idx.end && idx.max # endless range will have end == nil + max = idx.end + max -= 1 if max && idx.exclude_end? + max end if max_idx.nil? @@ -95,7 +99,9 @@ module Capybara end if between - min, max = between.min, (between.end && between.max) + min, max = (between.begin && between.min) || 1, between.end + max -= 1 if max && between.exclude_end? + size = load_up_to(max ? max + 1 : min) return size <=> min unless between.include?(size) end diff --git a/lib/capybara/spec/session/all_spec.rb b/lib/capybara/spec/session/all_spec.rb index 621e5ed9..bb7125f9 100644 --- a/lib/capybara/spec/session/all_spec.rb +++ b/lib/capybara/spec/session/all_spec.rb @@ -209,6 +209,13 @@ Capybara::SpecHelper.spec '#all' do expect { @session.all(:css, 'h1, p', between: 5..) }.to raise_error(Capybara::ExpectationNotMet) end TEST + + eval <<~TEST, binding, __FILE__, __LINE__ + 1 if RUBY_VERSION.to_f > 2.6 + it'treats a beginless range as maximum' do + expect { @session.all(:css, 'h1, p', between: ..7) }.not_to raise_error + expect { @session.all(:css, 'h1, p', between: ..3) }.to raise_error(Capybara::ExpectationNotMet) + end + TEST end context 'with multiple count filters' do diff --git a/spec/result_spec.rb b/spec/result_spec.rb index 43d0f6ef..a4f0d527 100644 --- a/spec/result_spec.rb +++ b/spec/result_spec.rb @@ -78,6 +78,10 @@ RSpec.describe Capybara::Result do 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 end it 'works with filter blocks' do