From 8a6da41c07c784e6522f65c8eda6c8ff7c81bc25 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 19 May 2026 20:18:12 +0200 Subject: [PATCH] Remove implementation detail specs for `Enumerator` `Enumerator::Yielder`, `Enumerator::Generator` and `Enumerator::Producer` are not supposed to be exposed to the user. Instead they will interact with them trough other means, like for example `Enumerator.new { |yielder| }`. Some specs I ported, others I removed, and some were already present. --- core/enumerator/generator/each_spec.rb | 40 ------------- core/enumerator/generator/initialize_spec.rb | 26 --------- core/enumerator/new_spec.rb | 59 ++++++++++++++++---- core/enumerator/yielder/append_spec.rb | 35 ------------ core/enumerator/yielder/initialize_spec.rb | 18 ------ core/enumerator/yielder/to_proc_spec.rb | 16 ------ core/enumerator/yielder/yield_spec.rb | 33 ----------- 7 files changed, 49 insertions(+), 178 deletions(-) delete mode 100644 core/enumerator/generator/each_spec.rb delete mode 100644 core/enumerator/generator/initialize_spec.rb delete mode 100644 core/enumerator/yielder/append_spec.rb delete mode 100644 core/enumerator/yielder/initialize_spec.rb delete mode 100644 core/enumerator/yielder/to_proc_spec.rb delete mode 100644 core/enumerator/yielder/yield_spec.rb diff --git a/core/enumerator/generator/each_spec.rb b/core/enumerator/generator/each_spec.rb deleted file mode 100644 index 41a494298b..0000000000 --- a/core/enumerator/generator/each_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -require_relative '../../../spec_helper' - -describe "Enumerator::Generator#each" do - before :each do - @generator = Enumerator::Generator.new do |y, *args| - y << 3 << 2 << 1 - y << args unless args.empty? - :block_returned - end - end - - it "is an enumerable" do - @generator.should.is_a?(Enumerable) - end - - it "supports enumeration with a block" do - r = [] - @generator.each { |v| r << v } - - r.should == [3, 2, 1] - end - - it "raises a LocalJumpError if no block given" do - -> { @generator.each }.should.raise(LocalJumpError) - end - - it "returns the block returned value" do - @generator.each {}.should.equal?(:block_returned) - end - - it "requires multiple arguments" do - Enumerator::Generator.instance_method(:each).arity.should < 0 - end - - it "appends given arguments to receiver.each" do - yields = [] - @generator.each(:each0, :each1) { |yielded| yields << yielded } - yields.should == [3, 2, 1, [:each0, :each1]] - end -end diff --git a/core/enumerator/generator/initialize_spec.rb b/core/enumerator/generator/initialize_spec.rb deleted file mode 100644 index 0f77d591d9..0000000000 --- a/core/enumerator/generator/initialize_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# -*- encoding: us-ascii -*- - -require_relative '../../../spec_helper' - -describe "Enumerator::Generator#initialize" do - before :each do - @class = Enumerator::Generator - @uninitialized = @class.allocate - end - - it "is a private method" do - @class.private_instance_methods(false).should.include?(:initialize) - end - - it "returns self when given a block" do - @uninitialized.send(:initialize) {}.should.equal?(@uninitialized) - end - - describe "on frozen instance" do - it "raises a FrozenError" do - -> { - @uninitialized.freeze.send(:initialize) {} - }.should.raise(FrozenError) - end - end -end diff --git a/core/enumerator/new_spec.rb b/core/enumerator/new_spec.rb index 539328a6c9..eb6c13759e 100644 --- a/core/enumerator/new_spec.rb +++ b/core/enumerator/new_spec.rb @@ -42,35 +42,74 @@ enum.to_a.should == ["a\n", "b\n", "c"] end - describe 'yielded values' do - it 'handles yield arguments properly' do + describe '#yield' do + it 'accepts a single argument' do Enumerator.new { |y| y.yield(1) }.to_a.should == [1] Enumerator.new { |y| y.yield(1) }.first.should == 1 + end - Enumerator.new { |y| y.yield([1]) }.to_a.should == [[1]] - Enumerator.new { |y| y.yield([1]) }.first.should == [1] - + it 'accepts multiple arguments' do Enumerator.new { |y| y.yield(1, 2) }.to_a.should == [[1, 2]] Enumerator.new { |y| y.yield(1, 2) }.first.should == [1, 2] + end + + it "doesn't double-wrap arrays" do + Enumerator.new { |y| y.yield([1]) }.to_a.should == [[1]] + Enumerator.new { |y| y.yield([1]) }.first.should == [1] Enumerator.new { |y| y.yield([1, 2]) }.to_a.should == [[1, 2]] Enumerator.new { |y| y.yield([1, 2]) }.first.should == [1, 2] end - it 'handles << arguments properly' do + it 'returns nil' do + ScratchPad.record [] + Enumerator.new do |y| + ScratchPad << y.yield(1) + end.to_a + + ScratchPad.recorded.should == [nil] + end + + it 'accepts keyword arguments and treats them as a positional hash' do + Enumerator.new { |y| y.yield(foo: 42) }.to_a.should == [{ foo: 42 }] + Enumerator.new { |y| y.yield(foo: 42) }.first.should == { foo: 42 } + + Enumerator.new { |y| y.yield(123, foo: 42) }.to_a.should == [[123, { foo: 42 }]] + Enumerator.new { |y| y.yield(123, foo: 42) }.first.should == [123, { foo: 42 }] + end + end + + describe '#<<' do + it 'accepts a single argument' do Enumerator.new { |y| y.<<(1) }.to_a.should == [1] Enumerator.new { |y| y.<<(1) }.first.should == 1 + end + it "doesn't double-wrap arrays" do Enumerator.new { |y| y.<<([1]) }.to_a.should == [[1]] Enumerator.new { |y| y.<<([1]) }.first.should == [1] - # << doesn't accept multiple arguments - # Enumerator.new { |y| y.<<(1, 2) }.to_a.should == [[1, 2]] - # Enumerator.new { |y| y.<<(1, 2) }.first.should == [1, 2] - Enumerator.new { |y| y.<<([1, 2]) }.to_a.should == [[1, 2]] Enumerator.new { |y| y.<<([1, 2]) }.first.should == [1, 2] end + + it 'accepts keyword arguments and treats them as a positional hash' do + Enumerator.new { |y| y.<<(foo: 42) }.to_a.should == [{ foo: 42 }] + Enumerator.new { |y| y.<<(foo: 42) }.first.should == { foo: 42 } + end + + it 'can be chained' do + enum = Enumerator.new do |y| + y << 1 << 2 + end + enum.to_a.should == [1, 2] + end + + it 'raises ArgumentError when given more than one argument' do + -> { + Enumerator.new { |y| y.<<(1, 2) }.to_a + }.should.raise(ArgumentError, "wrong number of arguments (given 2, expected 1)") + end end end end diff --git a/core/enumerator/yielder/append_spec.rb b/core/enumerator/yielder/append_spec.rb deleted file mode 100644 index 2e1f5203d9..0000000000 --- a/core/enumerator/yielder/append_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -require_relative '../../../spec_helper' - -describe "Enumerator::Yielder#<<" do - # TODO: There's some common behavior between yield and <<; move to a shared spec - it "yields the value to the block" do - ary = [] - y = Enumerator::Yielder.new {|x| ary << x} - y << 1 - - ary.should == [1] - end - - it "doesn't double-wrap Arrays" do - yields = [] - y = Enumerator::Yielder.new {|args| yields << args } - y << [1] - yields.should == [[1]] - end - - it "returns self" do - y = Enumerator::Yielder.new {|x| x + 1} - (y << 1).should.equal?(y) - end - - context "when multiple arguments passed" do - it "raises an ArgumentError" do - ary = [] - y = Enumerator::Yielder.new { |*x| ary << x } - - -> { - y.<<(1, 2) - }.should.raise(ArgumentError, /wrong number of arguments/) - end - end -end diff --git a/core/enumerator/yielder/initialize_spec.rb b/core/enumerator/yielder/initialize_spec.rb deleted file mode 100644 index 925f561ec4..0000000000 --- a/core/enumerator/yielder/initialize_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -# -*- encoding: us-ascii -*- - -require_relative '../../../spec_helper' - -describe "Enumerator::Yielder#initialize" do - before :each do - @class = Enumerator::Yielder - @uninitialized = @class.allocate - end - - it "is a private method" do - @class.private_instance_methods(false).should.include?(:initialize) - end - - it "returns self when given a block" do - @uninitialized.send(:initialize) {}.should.equal?(@uninitialized) - end -end diff --git a/core/enumerator/yielder/to_proc_spec.rb b/core/enumerator/yielder/to_proc_spec.rb deleted file mode 100644 index 1d3681ab50..0000000000 --- a/core/enumerator/yielder/to_proc_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require_relative '../../../spec_helper' - -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 diff --git a/core/enumerator/yielder/yield_spec.rb b/core/enumerator/yielder/yield_spec.rb deleted file mode 100644 index acfdf114b6..0000000000 --- a/core/enumerator/yielder/yield_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require_relative '../../../spec_helper' - -describe "Enumerator::Yielder#yield" do - it "yields the value to the block" do - ary = [] - y = Enumerator::Yielder.new {|x| ary << x} - y.yield 1 - - ary.should == [1] - end - - it "yields with passed arguments" do - yields = [] - y = Enumerator::Yielder.new {|*args| yields << args } - y.yield 1, 2 - yields.should == [[1, 2]] - end - - it "returns the result of the block for the given value" do - y = Enumerator::Yielder.new {|x| x + 1} - y.yield(1).should == 2 - end - - context "when multiple arguments passed" do - it "yields the arguments list to the block" do - ary = [] - y = Enumerator::Yielder.new { |*x| ary << x } - y.yield(1, 2) - - ary.should == [[1, 2]] - end - end -end