Skip to content

Commit

Permalink
Use ruby_ prefixed first/last to avoid collision
Browse files Browse the repository at this point in the history
Java 21 adds a SequencedCollection interface that defines getFirst
and getLast methods. Due to our conversion of these bean-like get
methods into Ruby accessors, the existing Array and Enumerable
first/last methods are overwritten and calling the Java 21
versions may fail with an arity error.

This commit modifies the spec to always test the ruby_ prefixed
versions, which will always work and which are functionally
equivalent to the unprefixed versions prior to Java 21.
  • Loading branch information
headius committed Aug 28, 2023
1 parent 3ae90d3 commit 5727c9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
18 changes: 10 additions & 8 deletions spec/java_integration/extensions/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@
end

it 'supports (Enumerable\'s) first' do
# Java 21 adds getFirst and getLast via a new SequencedCollection interface, so we always test the prefixed
# ruby_first method here. Prior to Java 21, it is equivalent to the #first methods.
set = java.util.LinkedHashSet.new [ 'foo', 'bar', 'baz' ]
expect( set.first ).to eq 'foo'
expect( set.first(2) ).to eq ['foo', 'bar']
expect( set.first(1) ).to eq ['foo']
expect( set.first(8) ).to eq ['foo', 'bar', 'baz']
expect( set.first(0) ).to eq []
expect( set.ruby_first ).to eq 'foo'
expect( set.ruby_first(2) ).to eq ['foo', 'bar']
expect( set.ruby_first(1) ).to eq ['foo']
expect( set.ruby_first(8) ).to eq ['foo', 'bar', 'baz']
expect( set.ruby_first(0) ).to eq []
set.clear
expect( set.first ).to be nil
expect( set.ruby_first ).to be nil

# java.util.Queue conflicts since it has getFirst :
# java.util.Queue also conflicts since it has getFirst
que = java.util.ArrayDeque.new [1, 2, 3]
expect( que.first ).to eq 1
expect( que.ruby_first ).to eq 1
expect( que.ruby_first(2).to_a ).to eq [1, 2]
expect( que.ruby_first(0).to_a ).to eq []
que.clear
Expand Down
33 changes: 8 additions & 25 deletions spec/java_integration/extensions/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,6 @@
expect( list.rindex.each { |x| x < 2 } ).to eq(3)
end

# Java 8 adds a single-parameter sort method to List that sorts in-place
# if ENV_JAVA['java.specification.version'] < '1.8'
# it "should be sortable with sort() without block" do
# expect(@list.sort.to_a).to eq(@data.sort)
# end
#
# it "should be sortable with sort() with block" do
# result = @list.sort do |a, b|
# a.length <=> b.length
# end
#
# expected = @data.sort do |a, b|
# a.length <=> b.length
# end
#
# expect(result.to_a).to eq(expected)
# end
# end

it "should be sortable with sort!() without block" do
list = java.util.LinkedList.new(@data)
list.sort!
Expand Down Expand Up @@ -210,20 +191,22 @@
end

it 'supports (Array-like) first/last' do
expect( @list.first ).to eq 'foo'
# Java 21 adds getFirst and getLast via a new SequencedCollection interface, so we always test the "ruby_" prefixed
# versions here. Prior to Java 21, they are equivalent to the #first and #last methods.
expect( @list.ruby_first ).to eq 'foo'
list = java.util.ArrayList.new [1, 2, 3]
expect( list.first(2).to_a ).to eq [1, 2]
expect( list.first(1).to_a ).to eq [1]
expect( list.first(0).to_a ).to eq []
expect( list.first(5).to_a ).to eq [1, 2, 3]
expect( list.ruby_first(2).to_a ).to eq [1, 2]
expect( list.ruby_first(1).to_a ).to eq [1]
expect( list.ruby_first(0).to_a ).to eq []
expect( list.ruby_first(5).to_a ).to eq [1, 2, 3]

# LinkedList does getList, unless we alias first ruby_first
expect( java.util.LinkedList.new.ruby_first ).to be nil
expect( java.util.LinkedList.new.ruby_last ).to be nil

list = java.util.LinkedList.new [1, 2, 3]
expect( list.ruby_last ).to eq 3
expect( java.util.Vector.new.last ).to be nil
expect( java.util.Vector.new.ruby_last ).to be nil
expect( list.ruby_last(1).to_a ).to eq [3]
expect( list.ruby_last(2) ).to be_a java.util.List
expect( list.ruby_last(2).to_a ).to eq [2, 3]
Expand Down

0 comments on commit 5727c9a

Please sign in to comment.