Skip to content

Commit

Permalink
Delegate Capybara::Result#to_ary (#2733)
Browse files Browse the repository at this point in the history
Delegate to `Capybara::Result#full_results` in order to support Ruby's
multiple assignment:

```ruby
node = Capybara.string <<~HTML
  <ul>
    <li>Alpha</li>
    <li>Beta</li>
    <li>Gamma</li>
    <li>Delta</li>
  </ul>
HTML

first, *rest, last = node.all("//li", minimum: 0)

expect(first).to have_text "Alpha"
expect(rest.first).to have_text "Beta"
expect(rest.last).to have_text "Gamma"
expect(last).to have_text "Delta"
```
  • Loading branch information
seanpdoyle authored Jan 26, 2024
1 parent 2cabbc6 commit 1857cb1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Release date: unreleased
* Dropped support for Ruby 2.7, 3.0+ is now required
* Dropped support for Selenium < 4.8

### Added

* Add `Capybara::Result#to_ary` to support multiple assignment

# Version 3.39.2
Release date: 2023-06-10

Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initialize(elements, query)
@allow_reload = false
end

def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample
def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample, :to_ary

alias index find_index

Expand Down
9 changes: 9 additions & 0 deletions spec/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
result.last.text == 'Delta'
end

it 'splats into multiple assignment' do
first, *rest, last = result

expect(first).to have_text 'Alpha'
expect(rest.first).to have_text 'Beta'
expect(rest.last).to have_text 'Gamma'
expect(last).to have_text 'Delta'
end

it 'can supports values_at method' do
expect(result.values_at(0, 2).map(&:text)).to eq(%w[Alpha Gamma])
end
Expand Down

0 comments on commit 1857cb1

Please sign in to comment.