From 1ec3b6a1aecb148905b2c0270f25627611215e4d Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 8 Dec 2015 07:23:43 +0000 Subject: enumerator.c: lazy_grep_v * enumerator.c (lazy_grep_v): add Enumerator::Lazy#grep_v as well as Enumerable, to enumerate lazily. [ruby-core:71845] [Feature #11773] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52943 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_lazy_enumerator.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'test') diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index 952a773d58..a0c5068710 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -197,6 +197,34 @@ class TestLazyEnumerator < Test::Unit::TestCase e.lazy.grep(proc {|x| x == [2, "2"]}, &:join).force) end + def test_grep_v + a = Step.new('a'..'f') + assert_equal('b', a.grep_v(/a/).first) + assert_equal('f', a.current) + assert_equal('a', a.lazy.grep_v(/c/).first) + assert_equal('a', a.current) + assert_equal(%w[b c d f], a.grep_v(proc {|x| /[aeiou]/ =~ x})) + assert_equal(%w[b c d f], a.lazy.grep_v(proc {|x| /[aeiou]/ =~ x}).to_a) + end + + def test_grep_v_with_block + a = Step.new('a'..'f') + assert_equal('B', a.grep_v(/a/) {|i| i.upcase}.first) + assert_equal('B', a.lazy.grep_v(/a/) {|i| i.upcase}.first) + end + + def test_grep_v_multiple_values + e = Enumerator.new { |yielder| + 3.times { |i| + yielder.yield(i, i.to_s) + } + } + assert_equal([[0, "0"], [1, "1"]], e.grep_v(proc {|x| x == [2, "2"]})) + assert_equal([[0, "0"], [1, "1"]], e.lazy.grep_v(proc {|x| x == [2, "2"]}).force) + assert_equal(["00", "11"], + e.lazy.grep_v(proc {|x| x == [2, "2"]}, &:join).force) + end + def test_zip a = Step.new(1..3) assert_equal([1, "a"], a.zip("a".."c").first) -- cgit v1.2.3