[Feature #20702] Tests for Array#fetch_values

This commit is contained in:
Nobuyoshi Nakada 2025-03-18 17:55:46 +09:00
parent 76aaf8ddf4
commit c7f31c88ae
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
2 changed files with 13 additions and 0 deletions

View File

@ -11,6 +11,7 @@ describe "Array#fetch_values" do
it "returns the values for indexes" do it "returns the values for indexes" do
@array.fetch_values(0).should == [:a] @array.fetch_values(0).should == [:a]
@array.fetch_values(0, 2).should == [:a, :c] @array.fetch_values(0, 2).should == [:a, :c]
@array.fetch_values(-1).should == [:c]
end end
it "returns the values for indexes ordered in the order of the requested indexes" do it "returns the values for indexes ordered in the order of the requested indexes" do

View File

@ -2716,6 +2716,18 @@ class TestArray < Test::Unit::TestCase
assert_equal(2, [0, 1].fetch(2, 2)) assert_equal(2, [0, 1].fetch(2, 2))
end end
def test_fetch_values
ary = @cls[1, 2, 3]
assert_equal([], ary.fetch_values())
assert_equal([1], ary.fetch_values(0))
assert_equal([3, 1, 3], ary.fetch_values(2, 0, -1))
assert_raise(TypeError) {ary.fetch_values("")}
assert_raise(IndexError) {ary.fetch_values(10)}
assert_raise(IndexError) {ary.fetch_values(-20)}
assert_equal(["10 not found"], ary.fetch_values(10) {|i| "#{i} not found"})
assert_equal(["10 not found", 3], ary.fetch_values(10, 2) {|i| "#{i} not found"})
end
def test_index2 def test_index2
a = [0, 1, 2] a = [0, 1, 2]
assert_equal(a, a.index.to_a) assert_equal(a, a.index.to_a)