aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_lazy_enumerator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_lazy_enumerator.rb')
-rw-r--r--test/ruby/test_lazy_enumerator.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb
index 7064f3b178..b096821191 100644
--- a/test/ruby/test_lazy_enumerator.rb
+++ b/test/ruby/test_lazy_enumerator.rb
@@ -72,6 +72,18 @@ class TestLazyEnumerator < Test::Unit::TestCase
assert_equal("word", a.current)
end
+ def test_select_multiple_values
+ e = Enumerator.new { |yielder|
+ for i in 1..5
+ yielder.yield(i, i.to_s)
+ end
+ }
+ assert_equal([[2, "2"], [4, "4"]],
+ e.select {|x| x[0] % 2 == 0})
+ assert_equal([[2, "2"], [4, "4"]],
+ e.lazy.select {|x| x[0] % 2 == 0}.force)
+ end
+
def test_map
a = Step.new(1..3)
assert_equal(2, a.map {|x| x * 2}.first)
@@ -112,6 +124,18 @@ class TestLazyEnumerator < Test::Unit::TestCase
assert_equal(nil, a.current)
end
+ def test_reject_multiple_values
+ e = Enumerator.new { |yielder|
+ for i in 1..5
+ yielder.yield(i, i.to_s)
+ end
+ }
+ assert_equal([[2, "2"], [4, "4"]],
+ e.reject {|x| x[0] % 2 != 0})
+ assert_equal([[2, "2"], [4, "4"]],
+ e.lazy.reject {|x| x[0] % 2 != 0}.force)
+ end
+
def test_grep
a = Step.new('a'..'f')
assert_equal('c', a.grep(/c/).first)
@@ -122,6 +146,24 @@ class TestLazyEnumerator < Test::Unit::TestCase
assert_equal(%w[a e], a.lazy.grep(proc {|x| /[aeiou]/ =~ x}).to_a)
end
+ def test_grep_with_block
+ a = Step.new('a'..'f')
+ assert_equal('C', a.grep(/c/) {|i| i.upcase}.first)
+ assert_equal('C', a.lazy.grep(/c/) {|i| i.upcase}.first)
+ end
+
+ def test_grep_multiple_values
+ e = Enumerator.new { |yielder|
+ 3.times { |i|
+ yielder.yield(i, i.to_s)
+ }
+ }
+ assert_equal([[2, "2"]], e.grep(proc {|x| x == [2, "2"]}))
+ assert_equal([[2, "2"]], e.lazy.grep(proc {|x| x == [2, "2"]}).force)
+ assert_equal(["22"],
+ e.lazy.grep(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)