diff options
Diffstat (limited to 'spec/ruby/core/enumerator/shared')
| -rw-r--r-- | spec/ruby/core/enumerator/shared/each.rb | 46 | ||||
| -rw-r--r-- | spec/ruby/core/enumerator/shared/enum_for.rb | 57 | ||||
| -rw-r--r-- | spec/ruby/core/enumerator/shared/with_index.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/enumerator/shared/with_object.rb | 42 |
4 files changed, 48 insertions, 101 deletions
diff --git a/spec/ruby/core/enumerator/shared/each.rb b/spec/ruby/core/enumerator/shared/each.rb new file mode 100644 index 0000000000..18ca773207 --- /dev/null +++ b/spec/ruby/core/enumerator/shared/each.rb @@ -0,0 +1,46 @@ +# #each passes source-yielded values to the block by ordinary block arity +# (rb_yield_values2 semantics in CRuby), unlike the Enumerable collection methods +# which pack them via rb_enum_values_pack() (see enumerable/shared/value_packing.rb). +describe :enum_each, shared: true do + # @object must be set to a Proc that wraps an Enumerator into the receiver + # under test (e.g. -> e { e } for Enumerator#each, -> e { e.lazy } for Lazy#each). + describe "with a source that yields multiple values" do + before :each do + @enum = @object.call(Enumerator.new { |y| y.yield 1, 2; y.yield 3, 4 }) + end + + it "yields the first value to a single-argument block" do + collected = [] + @enum.each { |x| collected << x } + collected.should == [1, 3] + end + + it "yields each value to a multi-argument block" do + collected = [] + @enum.each { |x, y| collected << [x, y] } + collected.should == [[1, 2], [3, 4]] + end + + it "gathers the values for a splat block" do + collected = [] + @enum.each { |*args| collected << args } + collected.should == [[1, 2], [3, 4]] + end + end + + describe "with a source that yields a single value" do + it "yields the value to a single-argument block" do + collected = [] + @object.call(Enumerator.new { |y| y.yield 7; y.yield 8 }).each { |x| collected << x } + collected.should == [7, 8] + end + end + + describe "with a source that yields no value" do + it "yields nil to a single-argument block" do + collected = [] + @object.call(Enumerator.new { |y| y.yield; y.yield }).each { |x| collected << x } + collected.should == [nil, nil] + end + end +end diff --git a/spec/ruby/core/enumerator/shared/enum_for.rb b/spec/ruby/core/enumerator/shared/enum_for.rb deleted file mode 100644 index a67a76c461..0000000000 --- a/spec/ruby/core/enumerator/shared/enum_for.rb +++ /dev/null @@ -1,57 +0,0 @@ -describe :enum_for, shared: true do - it "is defined in Kernel" do - Kernel.method_defined?(@method).should be_true - end - - it "returns a new enumerator" do - "abc".send(@method).should be_an_instance_of(Enumerator) - end - - it "defaults the first argument to :each" do - enum = [1,2].send(@method) - enum.map { |v| v }.should == [1,2].each { |v| v } - end - - it "sets regexp matches in the caller" do - "wawa".send(@method, :scan, /./).map {|o| $& }.should == ["w", "a", "w", "a"] - a = [] - "wawa".send(@method, :scan, /./).each {|o| a << $& } - a.should == ["w", "a", "w", "a"] - end - - it "exposes multi-arg yields as an array" do - o = Object.new - def o.each - yield :a - yield :b1, :b2 - yield [:c] - yield :d1, :d2 - yield :e1, :e2, :e3 - end - - enum = o.send(@method) - enum.next.should == :a - enum.next.should == [:b1, :b2] - enum.next.should == [:c] - enum.next.should == [:d1, :d2] - enum.next.should == [:e1, :e2, :e3] - end - - it "uses the passed block's value to calculate the size of the enumerator" do - Object.new.enum_for { 100 }.size.should == 100 - end - - it "defers the evaluation of the passed block until #size is called" do - ScratchPad.record [] - - enum = Object.new.enum_for do - ScratchPad << :called - 100 - end - - ScratchPad.recorded.should be_empty - - enum.size - ScratchPad.recorded.should == [:called] - end -end diff --git a/spec/ruby/core/enumerator/shared/with_index.rb b/spec/ruby/core/enumerator/shared/with_index.rb index 78771ffe82..0992397e95 100644 --- a/spec/ruby/core/enumerator/shared/with_index.rb +++ b/spec/ruby/core/enumerator/shared/with_index.rb @@ -16,7 +16,7 @@ describe :enum_with_index, shared: true do end it "returns the object being enumerated when given a block" do - @enum.send(@method) { |o, i| :glark }.should equal(@origin) + @enum.send(@method) { |o, i| :glark }.should.equal?(@origin) end it "binds splat arguments properly" do @@ -27,7 +27,7 @@ describe :enum_with_index, shared: true do it "returns an enumerator if no block is supplied" do ewi = @enum.send(@method) - ewi.should be_an_instance_of(Enumerator) + ewi.should.instance_of?(Enumerator) ewi.to_a.should == [[1, 0], [2, 1], [3, 2], [4, 3]] end end diff --git a/spec/ruby/core/enumerator/shared/with_object.rb b/spec/ruby/core/enumerator/shared/with_object.rb deleted file mode 100644 index d8e9d8e9f7..0000000000 --- a/spec/ruby/core/enumerator/shared/with_object.rb +++ /dev/null @@ -1,42 +0,0 @@ -require_relative '../../../spec_helper' - -describe :enum_with_object, shared: true do - before :each do - @enum = [:a, :b].to_enum - @memo = '' - @block_params = @enum.send(@method, @memo).to_a - end - - it "receives an argument" do - @enum.method(@method).arity.should == 1 - end - - context "with block" do - it "returns the given object" do - ret = @enum.send(@method, @memo) do |elm, memo| - # nothing - end - ret.should equal(@memo) - end - - context "the block parameter" do - it "passes each element to first parameter" do - @block_params[0][0].should equal(:a) - @block_params[1][0].should equal(:b) - end - - it "passes the given object to last parameter" do - @block_params[0][1].should equal(@memo) - @block_params[1][1].should equal(@memo) - end - end - end - - context "without block" do - it "returns new Enumerator" do - ret = @enum.send(@method, @memo) - ret.should be_an_instance_of(Enumerator) - ret.should_not equal(@enum) - end - end -end |
