From 5727c9afa0d33fc2ca785b9fbd5b202f691dbcc0 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 28 Aug 2023 13:36:38 -0500 Subject: [PATCH] Use ruby_ prefixed first/last to avoid collision 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. --- .../extensions/collection_spec.rb | 18 +++++----- spec/java_integration/extensions/list_spec.rb | 33 +++++-------------- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/spec/java_integration/extensions/collection_spec.rb b/spec/java_integration/extensions/collection_spec.rb index 583eab0743f..51be8784d71 100644 --- a/spec/java_integration/extensions/collection_spec.rb +++ b/spec/java_integration/extensions/collection_spec.rb @@ -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 diff --git a/spec/java_integration/extensions/list_spec.rb b/spec/java_integration/extensions/list_spec.rb index 9d5fcbb1912..489e279bf96 100644 --- a/spec/java_integration/extensions/list_spec.rb +++ b/spec/java_integration/extensions/list_spec.rb @@ -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! @@ -210,12 +191,14 @@ 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 @@ -223,7 +206,7 @@ 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]