aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-20 06:52:29 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-20 06:52:29 +0000
commitcc659d2f1b120fda6b085a7a51ac4115397bf9ef (patch)
tree38c2079f0d9fea985015fa30b9318687be7789ad /test
parentc0debb96b65e4a53bf79b6245a98c055db0c0924 (diff)
downloadruby-cc659d2f1b120fda6b085a7a51ac4115397bf9ef.tar.gz
* enum.c (enum_slice_when): New method: Enumerable#slice_when.
(slicewhen_i): New function. (slicewhen_ii): New function. * enumerator.c (InitVM_Enumerator): New method: Enumerator::Lazy#slice_when. [ruby-core:62499] [Feature #9826] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_enum.rb55
-rw-r--r--test/ruby/test_lazy_enumerator.rb1
2 files changed, 56 insertions, 0 deletions
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index d6795e49dd..edd389b071 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -574,6 +574,61 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal([["foo", ""], ["bar"]], e.to_a)
end
+ def test_slice_when_0
+ e = [].slice_when {|a, b| flunk "should not be called" }
+ assert_equal([], e.to_a)
+ end
+
+ def test_slice_when_1
+ e = [1].slice_when {|a, b| flunk "should not be called" }
+ assert_equal([[1]], e.to_a)
+ end
+
+ def test_slice_when_2
+ e = [1,2].slice_when {|a,b|
+ assert_equal(1, a)
+ assert_equal(2, b)
+ true
+ }
+ assert_equal([[1], [2]], e.to_a)
+
+ e = [1,2].slice_when {|a,b|
+ assert_equal(1, a)
+ assert_equal(2, b)
+ false
+ }
+ assert_equal([[1, 2]], e.to_a)
+ end
+
+ def test_slice_when_3
+ block_invocations = [
+ lambda {|a, b|
+ assert_equal(1, a)
+ assert_equal(2, b)
+ true
+ },
+ lambda {|a, b|
+ assert_equal(2, a)
+ assert_equal(3, b)
+ false
+ }
+ ]
+ e = [1,2,3].slice_when {|a,b|
+ block_invocations.shift.call(a, b)
+ }
+ assert_equal([[1], [2, 3]], e.to_a)
+ assert_equal([], block_invocations)
+ end
+
+ def test_slice_when_noblock
+ assert_raise(ArgumentError) { [].slice_when }
+ end
+
+ def test_slice_when_contiguously_increasing_integers
+ e = [1,4,9,10,11,12,15,16,19,20,21].slice_when {|i, j| i+1 != j }
+ assert_equal([[1], [4], [9,10,11,12], [15,16], [19,20,21]], e.to_a)
+ end
+
def test_detect
@obj = ('a'..'z')
assert_equal('c', @obj.detect {|x| x == 'c' })
diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb
index 549d0f104e..7bf9903ddc 100644
--- a/test/ruby/test_lazy_enumerator.rb
+++ b/test/ruby/test_lazy_enumerator.rb
@@ -481,6 +481,7 @@ EOS
assert_equal Enumerator::Lazy, [].lazy.send(method, *arg).class, bug7507
end
assert_equal Enumerator::Lazy, [].lazy.chunk{}.class, bug7507
+ assert_equal Enumerator::Lazy, [].lazy.slice_when{}.class, bug7507
end
def test_no_warnings