aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_array.rb
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-02-17 17:42:09 +0900
committerKazuki Yamaguchi <k@rhe.jp>2016-04-12 22:17:33 +0900
commit80b537f14c4af699b26e52931ae7e64a547e68c5 (patch)
tree17dd1e7babde4c90f27a14988a36bd96b6557c28 /test/ruby/test_array.rb
parent456523e2ede3073767fd8cb73cc4b159c3608890 (diff)
downloadruby-feature/enumerable-first-with-block.tar.gz
{Enumerable,Array,Range}#first, {Array,Range}#last with blockfeature/enumerable-first-with-block
* array.c (rb_ary_first, ary_last): extend Array#{first,last} to accept a block. If a block is passed, these methods collects only elements for which the block returns a truthy value. * enum.c: extend Enumerable#first to accept a block. * range.c: extend Range#{first,last} to accept a block. * gc.c: avoid using rb_ary_last(), because it may call a block. * test/ruby/test_array.rb: add test * test/ruby/test_enum.rb: ditto * test/ruby/test_range.rb: ditto
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r--test/ruby/test_array.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index fa158185f8..d0f12b280a 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -756,6 +756,8 @@ class TestArray < Test::Unit::TestCase
def test_first
assert_equal(3, @cls[3, 4, 5].first)
assert_equal(nil, @cls[].first)
+ assert_equal(2, @cls[1, 2, 3, 4].first { |i| i.even? })
+ assert_equal(nil, @cls[1, 2, 3, 4].first { |i| i > 100 })
end
def test_flatten
@@ -1059,6 +1061,8 @@ class TestArray < Test::Unit::TestCase
assert_equal(nil, @cls[].last)
assert_equal(1, @cls[1].last)
assert_equal(99, @cls[*(3..99).to_a].last)
+ assert_equal(3, @cls[1, 2, 3, 4].last { |i| i.odd? })
+ assert_equal(nil, @cls[1, 2, 3, 4].last { |i| i > 100 })
end
def test_length
@@ -1999,11 +2003,17 @@ class TestArray < Test::Unit::TestCase
def test_first2
assert_equal([0], [0].first(2))
assert_raise(ArgumentError) { [0].first(-1) }
+ assert_equal([2, 4], @cls[1, 2, 4, 6].first(2) { |i| i.even? })
+ assert_equal([2, 4, 6], @cls[2, 4, 5, 6].first(10) { |i| i.even? })
+ assert_raise(ArgumentError) { @cls[1, 2].first(-1) { |i| i.even? } }
end
def test_last2
assert_equal([0], [0].last(2))
assert_raise(ArgumentError) { [0].last(-1) }
+ assert_equal([4, 6], @cls[2, 4, 5, 6].last(2) { |i| i.even? })
+ assert_equal([2, 4, 6], @cls[2, 4, 5, 6].last(10) { |i| i.even? })
+ assert_raise(ArgumentError) { @cls[1, 2].last(-1) { |i| i.even? } }
end
def test_shift2