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

optimize Result#matches_count?

This commit is contained in:
Thomas Walpole 2016-08-31 14:55:03 -07:00
parent 99bf40d049
commit cb85ed5d13
2 changed files with 27 additions and 9 deletions

View file

@ -45,7 +45,7 @@ module Capybara
end
def [](*args)
if (args.size == 1) && ((idx = args[0]).is_a? Integer) && (idx > 0)
if (args.size == 1) && ((idx = args[0]).is_a? Integer) && (idx >= 0)
@result_cache << @results_enum.next while @result_cache.size <= idx
@result_cache[idx]
else
@ -61,26 +61,43 @@ module Capybara
end
def matches_count?
return Integer(@query.options[:count]) == count if @query.options[:count]
return false if @query.options[:between] && !(@query.options[:between] === count)
# Only check filters for as many elements as necessary to determine result
if @query.options[:count]
count_opt = Integer(@query.options[:count])
loop do
break if @result_cache.size > count_opt
@result_cache << @results_enum.next
end
return @result_cache.size == count_opt
end
if @query.options[:minimum]
min_opt = Integer(@query.options[:minimum])
begin
@result_cache << @results_enum.next while @result_cache.size < Integer(@query.options[:minimum])
@result_cache << @results_enum.next while @result_cache.size < min_opt
rescue StopIteration
return false
end
end
if @query.options[:maximum]
max_opt = Integer(@query.options[:maximum])
begin
@result_cache << @results_enum.next while @result_cache.size <= Integer(@query.options[:maximum])
@result_cache << @results_enum.next while @result_cache.size <= max_opt
return false
rescue StopIteration
end
end
if @query.options[:between]
max = Integer(@query.options[:between].max)
loop do
break if @result_cache.size > max
@result_cache << @results_enum.next
end
return false unless (@query.options[:between] === @result_cache.size)
end
return true
end
@ -105,9 +122,7 @@ module Capybara
private
def full_results
loop do
@result_cache << @results_enum.next
end
loop { @result_cache << @results_enum.next }
@result_cache
end

View file

@ -81,6 +81,9 @@ RSpec.describe Capybara::Result do
expect(result.instance_variable_get('@result_cache').size).to be 1
#works for indexed access
result[0]
expect(result.instance_variable_get('@result_cache').size).to be 1
result[2]
expect(result.instance_variable_get('@result_cache').size).to be 3