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

194 lines
5.7 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-02-28 19:11:41 -05:00
require 'spec_helper'
2019-08-17 20:24:40 -04:00
RSpec.describe Capybara::Result do
let :string do
Capybara.string <<-STRING
<ul>
<li>Alpha</li>
<li>Beta</li>
<li>Gamma</li>
<li>Delta</li>
</ul>
STRING
end
let :result do
2017-11-14 15:14:24 -05:00
string.all '//li', minimum: 0 # pass minimum: 0 so lazy evaluation doesn't get triggered yet
end
2018-07-10 17:18:39 -04:00
it 'has a length' do
expect(result.length).to eq(4)
end
2018-07-10 17:18:39 -04:00
it 'has a first element' do
result.first.text == 'Alpha'
end
2018-07-10 17:18:39 -04:00
it 'has a last element' do
result.last.text == 'Delta'
end
it 'can supports values_at method' do
2018-02-28 19:11:41 -05:00
expect(result.values_at(0, 2).map(&:text)).to eq(%w[Alpha Gamma])
end
2018-07-10 17:18:39 -04:00
it 'can return an element by its index' do
expect(result.at(1).text).to eq('Beta')
expect(result[2].text).to eq('Gamma')
end
2018-07-10 17:18:39 -04:00
it 'can be mapped' do
2018-02-28 19:11:41 -05:00
expect(result.map(&:text)).to eq(%w[Alpha Beta Gamma Delta])
end
2018-07-10 17:18:39 -04:00
it 'can be selected' do
2019-10-15 21:06:45 -04:00
expect(result.count do |element|
element.text.include? 't'
2019-10-15 21:06:45 -04:00
end).to eq(2)
end
2018-07-10 17:18:39 -04:00
it 'can be reduced' do
expect(result.reduce('') do |memo, element|
2018-02-28 19:11:41 -05:00
memo + element.text[0]
end).to eq('ABGD')
end
it 'can be sampled' do
expect(result).to include(result.sample)
end
it 'can be indexed' do
expect(result.index do |el|
el.text == 'Gamma'
end).to eq(2)
end
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]
2021-03-27 15:23:04 -04:00
expect(recalc_result[2..-1].map(&:text)).to eq %w[Gamma Delta] # rubocop:disable Style/SlicingWithRange
expect(recalc_result[2..].map(&:text)).to eq %w[Gamma Delta]
end
2021-03-27 15:23:04 -04:00
it 'supports endless ranges' do
expect(result[2..].map(&:text)).to eq %w[Gamma Delta]
end
2022-04-30 13:26:55 -04:00
it 'supports inclusive positive beginless ranges' do
expect(result[..2].map(&:text)).to eq %w[Alpha Beta Gamma]
end
2022-04-30 13:26:55 -04:00
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
2022-04-30 13:26:55 -04:00
it 'supports exclusive positive beginless ranges' do
expect(result[...2].map(&:text)).to eq %w[Alpha Beta]
end
2022-04-30 13:26:55 -04:00
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
it 'works with filter blocks' do
result = string.all('//li') { |node| node.text == 'Alpha' }
expect(result.size).to eq 1
end
2018-02-28 19:11:41 -05:00
# Not a great test but it indirectly tests what is needed
2018-07-10 17:18:39 -04:00
it 'should evaluate filters lazily for idx' do
2019-04-11 18:01:59 -04:00
skip 'JRuby has an issue with lazy enumerator evaluation' if jruby_lazy_enumerator_workaround?
2018-02-28 19:11:41 -05:00
# Not processed until accessed
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 0
2018-02-28 19:11:41 -05:00
# Only one retrieved when needed
result.first
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 1
2018-02-28 19:11:41 -05:00
# works for indexed access
2016-08-31 17:55:03 -04:00
result[0]
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 1
2016-08-31 17:55:03 -04:00
result[2]
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 3
2018-02-28 19:11:41 -05:00
# All cached when converted to array
result.to_a
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to eq 4
end
2018-07-10 17:18:39 -04:00
it 'should evaluate filters lazily for range' do
2019-04-11 18:01:59 -04:00
skip 'JRuby has an issue with lazy enumerator evaluation' if jruby_lazy_enumerator_workaround?
result[0..1]
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 2
expect(result[0..7].size).to eq 4
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 4
end
2018-07-10 17:18:39 -04:00
it 'should evaluate filters lazily for idx and length' do
2019-04-11 18:01:59 -04:00
skip 'JRuby has an issue with lazy enumerator evaluation' if jruby_lazy_enumerator_workaround?
result[1, 2]
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 3
expect(result[2, 5].size).to eq 2
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 4
end
2018-07-10 17:18:39 -04:00
it 'should only need to evaluate one result for any?' do
2019-04-11 18:01:59 -04:00
skip 'JRuby has an issue with lazy enumerator evaluation' if jruby_lazy_enumerator_workaround?
2018-05-16 15:47:08 -04:00
result.any?
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to be 1
2018-05-16 15:47:08 -04:00
end
2018-07-10 17:18:39 -04:00
it 'should evaluate all elements when #to_a called' do
# All cached when converted to array
result.to_a
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to eq 4
end
2019-10-15 20:40:27 -04:00
describe '#each' do
it 'lazily evaluates' do
2019-04-11 18:01:59 -04:00
skip 'JRuby has an issue with lazy enumerator evaluation' if jruby_lazy_enumerator_workaround?
2018-02-28 19:11:41 -05:00
results = []
result.each do |el|
results << el
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to eq results.size
end
expect(results.size).to eq 4
end
context 'without a block' do
it 'returns an iterator' do
expect(result.each).to be_a(Enumerator)
end
it 'lazily evaluates' do
2019-04-11 18:01:59 -04:00
skip 'JRuby has an issue with lazy enumerator evaluation' if jruby_lazy_enumerator_workaround?
2018-02-28 19:11:41 -05:00
result.each.with_index do |_el, idx|
2022-04-30 13:26:55 -04:00
expect(result.instance_variable_get(:@result_cache).size).to eq(idx + 1) # 0 indexing
end
end
end
end
2019-04-11 18:01:59 -04:00
def jruby_lazy_enumerator_workaround?
RUBY_PLATFORM == 'java'
2019-04-11 18:01:59 -04:00
end
end