/usr/lib/ruby/vendor_ruby/capybara/result.rb is in ruby-capybara 2.10.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | # frozen_string_literal: true
require 'forwardable'
module Capybara
##
# A {Capybara::Result} represents a collection of {Capybara::Node::Element} on the page. It is possible to interact with this
# collection similar to an Array because it implements Enumerable and offers the following Array methods through delegation:
#
# * []
# * each()
# * at()
# * size()
# * count()
# * length()
# * first()
# * last()
# * empty?()
#
# @see Capybara::Node::Element
#
class Result
include Enumerable
extend Forwardable
def initialize(elements, query)
@elements = elements
@result_cache = []
@results_enum = lazy_select_elements { |node| query.matches_filters?(node) }
@query = query
# JRuby has an issue with eagerly finding next in lazy enumerators which
# causes a concurrency issue with network requests here
# https://github.com/jruby/jruby/issues/4212
# Just force all the results to be evaluated
full_results if RUBY_PLATFORM == 'java'
end
def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample
alias :index :find_index
def each(&block)
@result_cache.each(&block)
loop do
next_result = @results_enum.next
@result_cache << next_result
block.call(next_result)
end
self
end
def [](*args)
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
full_results[*args]
end
rescue StopIteration
return nil
end
alias :at :[]
def empty?
!any?
end
def matches_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 < 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 <= 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
def failure_message
message = @query.failure_message
if count > 0
message << ", found #{count} #{Capybara::Helpers.declension("match", "matches", count)}: " << full_results.map(&:text).map(&:inspect).join(", ")
else
message << " but there were no matches"
end
unless rest.empty?
elements = rest.map(&:text).map(&:inspect).join(", ")
message << ". Also found " << elements << ", which matched the selector but not all filters."
end
message
end
def negative_failure_message
failure_message.sub(/(to find)/, 'not \1')
end
private
def full_results
loop { @result_cache << @results_enum.next }
@result_cache
end
def rest
@rest ||= @elements - full_results
end
def lazy_select_elements(&block)
if @elements.respond_to? :lazy #Ruby 2.0+
@elements.lazy.select &block
else
Enumerator.new do |yielder|
@elements.each do |val|
yielder.yield(val) if block.call(val)
end
end
end
end
end
end
|