summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-29 21:33:59 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-30 07:06:42 -0700
commit3073404e741df19ae16248126640777ed36110e8 (patch)
tree3e47a5336c27487329f43205746b17b3f03e5276 /test
parent5ddc2ba13ed6964a0383894e3728dc1bc708c404 (diff)
Add rb_enumeratorize_with_size_kw and related macros
Currently, there is not a way to create a sized enumerator in C with a different set of arguments than provided by Ruby, and correctly handle keyword arguments. This function allows that. The need for this is fairly uncommon, but it occurs at least in Enumerator.produce, which takes arugments from Ruby but calls rb_enumeratorize_with_size with a different set of arguments.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2509
Diffstat (limited to 'test')
-rw-r--r--test/-ext-/test_enumerator_kw.rb11
-rw-r--r--test/ruby/test_enumerator.rb8
2 files changed, 19 insertions, 0 deletions
diff --git a/test/-ext-/test_enumerator_kw.rb b/test/-ext-/test_enumerator_kw.rb
new file mode 100644
index 0000000000..fb47c2bcf8
--- /dev/null
+++ b/test/-ext-/test_enumerator_kw.rb
@@ -0,0 +1,11 @@
+require 'test/unit'
+require '-test-/enumerator_kw'
+
+class TestEnumeratorKw < Test::Unit::TestCase
+ def test_enumerator_kw
+ o = Object.new
+ o.extend Bug::EnumeratorKw
+ assert_equal([nil, [], {:a=>1}, o], o.m(a: 1) { |*a| a })
+ assert_equal([nil, [[], {:a=>1}, o], nil, o], o.m(a: 1).each { |*a| a })
+ end
+end
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index 1e306c33a2..65adb5a438 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -831,6 +831,14 @@ class TestEnumerator < Test::Unit::TestCase
assert_equal [1, 2, 3], enum.take(3)
assert_equal [1, 2], passed_args
+ # With initial keyword arguments
+ passed_args = []
+ enum = Enumerator.produce(a: 1, b: 1) { |obj| passed_args << obj; obj.shift if obj.respond_to?(:shift)}
+ assert_instance_of(Enumerator, enum)
+ assert_equal Float::INFINITY, enum.size
+ assert_equal [{b: 1}, [1], :a, nil], enum.take(4)
+ assert_equal [{b: 1}, [1], :a], passed_args
+
# Raising StopIteration
words = "The quick brown fox jumps over the lazy dog.".scan(/\w+/)
enum = Enumerator.produce { words.shift or raise StopIteration }