aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/enumerator
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2020-01-28 20:47:48 +0100
committerBenoit Daloze <eregontp@gmail.com>2020-01-28 20:47:48 +0100
commit809f0b8a1357f14f9645210d4812f4400c8d397e (patch)
tree7ce6192f94b1dc4b004798aa5d0c4d6bac02577f /spec/ruby/core/enumerator
parented377cc9aaf1ccbede19ddc6c464f5fbf3cabc34 (diff)
downloadruby-809f0b8a1357f14f9645210d4812f4400c8d397e.tar.gz
Update to ruby/spec@f8a2d54
Diffstat (limited to 'spec/ruby/core/enumerator')
-rw-r--r--spec/ruby/core/enumerator/each_spec.rb6
-rw-r--r--spec/ruby/core/enumerator/lazy/eager_spec.rb29
-rw-r--r--spec/ruby/core/enumerator/new_spec.rb38
-rw-r--r--spec/ruby/core/enumerator/produce_spec.rb36
-rw-r--r--spec/ruby/core/enumerator/yielder/to_proc_spec.rb18
5 files changed, 124 insertions, 3 deletions
diff --git a/spec/ruby/core/enumerator/each_spec.rb b/spec/ruby/core/enumerator/each_spec.rb
index d88c09cdb5..99ac3120af 100644
--- a/spec/ruby/core/enumerator/each_spec.rb
+++ b/spec/ruby/core/enumerator/each_spec.rb
@@ -40,10 +40,10 @@ describe "Enumerator#each" do
end
it "calls the method given in the constructor until it's exhausted" do
- each = mock('each')
- each.should_receive(:each).and_yield(1).and_yield(2).and_yield(3)
+ each = mock('peach')
+ each.should_receive(:peach).and_yield(1).and_yield(2).and_yield(3)
acc = []
- each.to_enum.each {|e| acc << e }
+ each.to_enum(:peach).each {|e| acc << e }
acc.should == [1,2,3]
end
diff --git a/spec/ruby/core/enumerator/lazy/eager_spec.rb b/spec/ruby/core/enumerator/lazy/eager_spec.rb
new file mode 100644
index 0000000000..30ba2dfe0e
--- /dev/null
+++ b/spec/ruby/core/enumerator/lazy/eager_spec.rb
@@ -0,0 +1,29 @@
+require_relative '../../../spec_helper'
+
+ruby_version_is "2.7" do
+ describe "Enumerator::Lazy#eager" do
+ it "returns a non-lazy Enumerator converted from the lazy enumerator" do
+ enum = [1, 2, 3].lazy
+
+ enum.class.should == Enumerator::Lazy
+ enum.eager.class.should == Enumerator
+ end
+
+ it "does not enumerate an enumerator" do
+ ScratchPad.record []
+
+ sequence = [1, 2, 3]
+ enum_lazy = Enumerator::Lazy.new(sequence) do |yielder, value|
+ yielder << value
+ ScratchPad << value
+ end
+
+ ScratchPad.recorded.should == []
+ enum = enum_lazy.eager
+ ScratchPad.recorded.should == []
+
+ enum.map { |i| i }.should == [1, 2, 3]
+ ScratchPad.recorded.should == [1, 2, 3]
+ end
+ end
+end
diff --git a/spec/ruby/core/enumerator/new_spec.rb b/spec/ruby/core/enumerator/new_spec.rb
index 170809dbc1..15e42d247e 100644
--- a/spec/ruby/core/enumerator/new_spec.rb
+++ b/spec/ruby/core/enumerator/new_spec.rb
@@ -38,4 +38,42 @@ describe "Enumerator.new" do
end
enum.to_a.should == [:bar]
end
+
+ context "when passed a block" do
+ it "defines iteration with block, yielder argument and calling << method" do
+ enum = Enumerator.new do |yielder|
+ a = 1
+
+ loop do
+ yielder << a
+ a = a + 1
+ end
+ end
+
+ enum.take(3).should == [1, 2, 3]
+ end
+
+ it "defines iteration with block, yielder argument and calling yield method" do
+ enum = Enumerator.new do |yielder|
+ a = 1
+
+ loop do
+ yielder.yield(a)
+ a = a + 1
+ end
+ end
+
+ enum.take(3).should == [1, 2, 3]
+ end
+
+ ruby_version_is "2.7" do
+ it "defines iteration with block, yielder argument and treating it as a proc" do
+ enum = Enumerator.new do |yielder|
+ "a\nb\nc".each_line(&yielder)
+ end
+
+ enum.to_a.should == ["a\n", "b\n", "c"]
+ end
+ end
+ end
end
diff --git a/spec/ruby/core/enumerator/produce_spec.rb b/spec/ruby/core/enumerator/produce_spec.rb
new file mode 100644
index 0000000000..f6f1dcd429
--- /dev/null
+++ b/spec/ruby/core/enumerator/produce_spec.rb
@@ -0,0 +1,36 @@
+require_relative '../../spec_helper'
+
+ruby_version_is "2.7" do
+ describe "Enumerator.produce" do
+ it "creates an infinite enumerator" do
+ enum = Enumerator.produce(0) { |prev| prev + 1 }
+ enum.take(5).should == [0, 1, 2, 3, 4]
+ end
+
+ it "terminates iteration when block raises StopIteration exception" do
+ enum = Enumerator.produce(0) do | prev|
+ raise StopIteration if prev >= 2
+ prev + 1
+ end
+
+ enum.to_a.should == [0, 1, 2]
+ end
+
+ context "when initial value skipped" do
+ it "uses nil instead" do
+ ScratchPad.record []
+ enum = Enumerator.produce { |prev| ScratchPad << prev; (prev || 0) + 1 }
+
+ enum.take(3).should == [1, 2, 3]
+ ScratchPad.recorded.should == [nil, 1, 2]
+ end
+
+ it "starts enumerable from result of first block call" do
+ array = "a\nb\nc\nd".lines
+ lines = Enumerator.produce { array.shift }.take_while { |s| s }
+
+ lines.should == ["a\n", "b\n", "c\n", "d"]
+ end
+ end
+ end
+end
diff --git a/spec/ruby/core/enumerator/yielder/to_proc_spec.rb b/spec/ruby/core/enumerator/yielder/to_proc_spec.rb
new file mode 100644
index 0000000000..0ed1645853
--- /dev/null
+++ b/spec/ruby/core/enumerator/yielder/to_proc_spec.rb
@@ -0,0 +1,18 @@
+require_relative '../../../spec_helper'
+
+ruby_version_is "2.7" do
+ describe "Enumerator::Yielder#to_proc" do
+ it "returns a Proc object that takes an argument and yields it to the block" do
+ ScratchPad.record []
+ y = Enumerator::Yielder.new { |*args| ScratchPad << args; "foobar" }
+
+ callable = y.to_proc
+ callable.class.should == Proc
+
+ result = callable.call(1, 2)
+ ScratchPad.recorded.should == [[1, 2]]
+
+ result.should == "foobar"
+ end
+ end
+end