2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2012-08-09 20:38:07 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2014-04-03 13:25:03 -04:00
|
|
|
RSpec.describe Capybara::Result do
|
2012-08-09 20:38:07 -04:00
|
|
|
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
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has a length" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.length).to eq(4)
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has a first element" do
|
|
|
|
result.first.text == 'Alpha'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a last element" do
|
|
|
|
result.last.text == 'Delta'
|
|
|
|
end
|
|
|
|
|
2013-10-29 16:43:11 -04:00
|
|
|
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])
|
2013-10-29 16:43:11 -04:00
|
|
|
end
|
|
|
|
|
2012-08-09 20:38:07 -04:00
|
|
|
it "can return an element by its index" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.at(1).text).to eq('Beta')
|
|
|
|
expect(result[2].text).to eq('Gamma')
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be mapped" do
|
2018-02-28 19:11:41 -05:00
|
|
|
expect(result.map(&:text)).to eq(%w[Alpha Beta Gamma Delta])
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be selected" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.select do |element|
|
2012-08-09 20:38:07 -04:00
|
|
|
element.text.include? 't'
|
2013-11-14 12:43:36 -05:00
|
|
|
end.length).to eq(2)
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be reduced" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.reduce('') do |memo, element|
|
2018-02-28 19:11:41 -05:00
|
|
|
memo + element.text[0]
|
2013-11-14 12:43:36 -05:00
|
|
|
end).to eq('ABGD')
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
2013-09-16 07:19:06 -04:00
|
|
|
|
|
|
|
it 'can be sampled' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result).to include(result.sample)
|
2013-09-16 07:19:06 -04:00
|
|
|
end
|
2013-10-29 16:43:11 -04:00
|
|
|
|
|
|
|
it 'can be indexed' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.index do |el|
|
2013-10-29 16:43:11 -04:00
|
|
|
el.text == 'Gamma'
|
2013-11-14 12:43:36 -05:00
|
|
|
end).to eq(2)
|
2013-10-29 16:43:11 -04:00
|
|
|
end
|
2016-08-05 17:44:33 -04:00
|
|
|
|
|
|
|
it 'supports all modes of []' do
|
|
|
|
expect(result[1].text).to eq 'Beta'
|
2018-02-28 19:11:41 -05:00
|
|
|
expect(result[0, 2].map(&:text)).to eq %w[Alpha Beta]
|
|
|
|
expect(result[1..3].map(&:text)).to eq %w[Beta Gamma Delta]
|
2016-08-05 17:44:33 -04:00
|
|
|
expect(result[-1].text).to eq 'Delta'
|
2018-05-11 18:39:34 -04:00
|
|
|
expect(result[-2, 3].map(&:text)).to eq %w[Gamma Delta]
|
|
|
|
expect(result[1..7].map(&:text)).to eq %w[Beta Gamma Delta]
|
2016-08-05 17:44:33 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 14:50:32 -04:00
|
|
|
it 'works with filter blocks' do
|
|
|
|
result = string.all('//li') { |node| node.text == 'Alpha' }
|
|
|
|
expect(result.size).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should catch invalid element errors during filtering' do
|
2017-05-31 17:07:34 -04:00
|
|
|
allow_any_instance_of(Capybara::Node::Simple).to receive(:text).and_raise(StandardError)
|
2018-04-27 14:01:47 -04:00
|
|
|
allow_any_instance_of(Capybara::Node::Simple).to receive(:session).and_return(
|
|
|
|
instance_double("Capybara::Session", driver: instance_double("Capybara::Driver::Base", invalid_element_errors: [StandardError]))
|
|
|
|
)
|
2017-05-31 17:07:34 -04:00
|
|
|
result = string.all('//li', text: 'Alpha')
|
2017-05-31 14:50:32 -04:00
|
|
|
expect(result.size).to eq 0
|
|
|
|
end
|
|
|
|
|
2017-05-31 17:07:34 -04:00
|
|
|
it 'should return non-invalid element errors during filtering' do
|
|
|
|
allow_any_instance_of(Capybara::Node::Simple).to receive(:text).and_raise(StandardError)
|
2018-04-27 14:01:47 -04:00
|
|
|
allow_any_instance_of(Capybara::Node::Simple).to receive(:session).and_return(
|
|
|
|
instance_double("Capybara::Session", driver: instance_double("Capybara::Driver::Base", invalid_element_errors: [ArgumentError]))
|
|
|
|
)
|
2017-05-31 17:07:34 -04:00
|
|
|
expect do
|
|
|
|
string.all('//li', text: 'Alpha').to_a
|
|
|
|
end.to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-02-28 19:11:41 -05:00
|
|
|
# Not a great test but it indirectly tests what is needed
|
2018-05-11 18:14:51 -04:00
|
|
|
it "should evaluate filters lazily for idx" do
|
2017-02-16 15:31:33 -05:00
|
|
|
skip 'JRuby has an issue with lazy enumerator evaluation' if RUBY_PLATFORM == 'java'
|
2018-02-28 19:11:41 -05:00
|
|
|
# Not processed until accessed
|
2016-08-05 17:44:33 -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
|
2016-08-05 17:44:33 -04:00
|
|
|
result.first
|
|
|
|
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]
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to be 1
|
|
|
|
|
2016-08-05 17:44:33 -04:00
|
|
|
result[2]
|
|
|
|
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
|
2016-08-05 17:44:33 -04:00
|
|
|
result.to_a
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to eq 4
|
|
|
|
end
|
2016-10-18 11:54:57 -04:00
|
|
|
|
2018-05-11 18:14:51 -04:00
|
|
|
it "should evaluate filters lazily for range" do
|
|
|
|
skip 'JRuby has an issue with lazy enumerator evaluation' if RUBY_PLATFORM == 'java'
|
|
|
|
result[0..1]
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to be 2
|
2018-05-11 18:39:34 -04:00
|
|
|
|
|
|
|
expect(result[0..7].size).to eq 4
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to be 4
|
2018-05-11 18:14:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should evaluate filters lazily for idx and length" do
|
|
|
|
skip 'JRuby has an issue with lazy enumerator evaluation' if RUBY_PLATFORM == 'java'
|
|
|
|
result[1, 2]
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to be 3
|
2018-05-11 18:39:34 -04:00
|
|
|
|
|
|
|
expect(result[2, 5].size).to eq 2
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to be 4
|
2018-05-11 18:14:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should evaluate all elements when #to_a called" do
|
|
|
|
# All cached when converted to array
|
|
|
|
result.to_a
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to eq 4
|
|
|
|
end
|
|
|
|
|
2016-10-18 11:54:57 -04:00
|
|
|
context '#each' do
|
|
|
|
it 'lazily evaluates' do
|
2017-02-16 15:31:33 -05:00
|
|
|
skip 'JRuby has an issue with lazy enumerator evaluation' if RUBY_PLATFORM == 'java'
|
2018-02-28 19:11:41 -05:00
|
|
|
results = []
|
2016-10-18 11:54:57 -04:00
|
|
|
result.each do |el|
|
|
|
|
results << el
|
|
|
|
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
|
2017-02-16 15:31:33 -05:00
|
|
|
skip 'JRuby has an issue with lazy enumerator evaluation' if RUBY_PLATFORM == 'java'
|
2018-02-28 19:11:41 -05:00
|
|
|
result.each.with_index do |_el, idx|
|
|
|
|
expect(result.instance_variable_get('@result_cache').size).to eq(idx + 1) # 0 indexing
|
2016-10-18 11:54:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-03 17:19:03 -04:00
|
|
|
|
2018-05-04 13:22:40 -04:00
|
|
|
context 'lazy select' do
|
2018-05-03 17:19:03 -04:00
|
|
|
it 'is compatible' do
|
|
|
|
# This test will let us know when JRuby fixes lazy select so we can re-enable it in Result
|
|
|
|
pending 'JRuby has an issue with lazy enumberator evaluation' if RUBY_PLATFORM == 'java'
|
|
|
|
eval_count = 0
|
2018-05-04 13:22:40 -04:00
|
|
|
enum = %w[Text1 Text2 Text3].lazy.select do
|
2018-05-03 17:19:03 -04:00
|
|
|
eval_count += 1
|
|
|
|
true
|
|
|
|
end
|
|
|
|
expect(eval_count).to eq 0
|
|
|
|
enum.next
|
|
|
|
sleep 1
|
|
|
|
expect(eval_count).to eq 1
|
|
|
|
end
|
|
|
|
end
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|