git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2019-01-20 20:38:57 +00:00
parent 58573c33e4
commit 6204e0804b
147 changed files with 2728 additions and 21522 deletions

View file

@ -1,89 +0,0 @@
# -*- encoding: us-ascii -*-
describe :enum_each, shared: true do
before :each do
object_each_with_arguments = Object.new
def object_each_with_arguments.each_with_arguments(arg, *args)
yield arg, *args
:method_returned
end
@enum_with_arguments = object_each_with_arguments.to_enum(:each_with_arguments, :arg0, :arg1, :arg2)
@enum_with_yielder = Enumerator.new {|y| y.yield :ok}
end
it "yields each element of self to the given block" do
acc = []
[1,2,3].to_enum.each {|e| acc << e }
acc.should == [1,2,3]
end
it "calls #each on the object given in the constructor by default" do
each = mock('each')
each.should_receive(:each)
each.to_enum.each {|e| e }
end
it "calls #each on the underlying object until it's exhausted" do
each = mock('each')
each.should_receive(:each).and_yield(1).and_yield(2).and_yield(3)
acc = []
each.to_enum.each {|e| acc << e }
acc.should == [1,2,3]
end
it "calls the method given in the constructor instead of #each" do
each = mock('peach')
each.should_receive(:peach)
each.to_enum(:peach).each {|e| e }
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)
acc = []
each.to_enum.each {|e| acc << e }
acc.should == [1,2,3]
end
it "raises a NoMethodError if the object doesn't respond to #each" do
enum = Object.new.to_enum
lambda do
enum.each { |e| e }
end.should raise_error(NoMethodError)
end
it "returns self if not given arguments and not given a block" do
@enum_with_arguments.each.should equal(@enum_with_arguments)
@enum_with_yielder.each.should equal(@enum_with_yielder)
end
it "returns the same value from receiver.each if block is given" do
@enum_with_arguments.each {}.should equal(:method_returned)
end
it "passes given arguments at initialized to receiver.each" do
@enum_with_arguments.each.to_a.should == [[:arg0, :arg1, :arg2]]
end
it "requires multiple arguments" do
Enumerator.instance_method(:each).arity.should < 0
end
it "appends given arguments to receiver.each" do
@enum_with_arguments.each(:each0, :each1).to_a.should == [[:arg0, :arg1, :arg2, :each0, :each1]]
@enum_with_arguments.each(:each2, :each3).to_a.should == [[:arg0, :arg1, :arg2, :each2, :each3]]
end
it "returns the same value from receiver.each if block and arguments are given" do
@enum_with_arguments.each(:each1, :each2) {}.should equal(:method_returned)
end
it "returns new Enumerator if given arguments but not given a block" do
ret = @enum_with_arguments.each 1
ret.should be_an_instance_of(Enumerator)
ret.should_not equal(@enum_with_arguments)
end
end

View file

@ -1,12 +0,0 @@
require_relative '../../spec_helper'
require_relative '../../fixtures/enumerator/classes'
describe :enum_cons, shared: true do
it "returns an enumerator of the receiver with iteration of each_cons for each array of n concecutive elements" do
a = []
enum = EnumSpecs::Numerous.new.enum_cons(3)
enum.each {|x| a << x}
enum.should be_an_instance_of(Enumerator)
a.should == [[2, 5, 3], [5, 3, 6], [3, 6, 1], [6, 1, 4]]
end
end

View file

@ -1,42 +0,0 @@
require_relative '../../spec_helper'
describe :enum_new, shared: true do
it "creates a new custom enumerator with the given object, iterator and arguments" do
enum = Enumerator.new(1, :upto, 3)
enum.should be_an_instance_of(Enumerator)
end
it "creates a new custom enumerator that responds to #each" do
enum = Enumerator.new(1, :upto, 3)
enum.respond_to?(:each).should == true
end
it "creates a new custom enumerator that runs correctly" do
Enumerator.new(1, :upto, 3).map{|x|x}.should == [1,2,3]
end
it "aliases the second argument to :each" do
Enumerator.new(1..2).to_a.should == Enumerator.new(1..2, :each).to_a
end
it "doesn't check for the presence of the iterator method" do
Enumerator.new(nil).should be_an_instance_of(Enumerator)
end
it "uses the latest define iterator method" do
class StrangeEach
def each
yield :foo
end
end
enum = Enumerator.new(StrangeEach.new)
enum.to_a.should == [:foo]
class StrangeEach
def each
yield :bar
end
end
enum.to_a.should == [:bar]
end
end

View file

@ -1,28 +0,0 @@
require_relative '../../spec_helper'
describe :enum_next, shared: true do
before :each do
@enum = 1.upto(3)
end
it "returns the next element of the enumeration" do
@enum.next.should == 1
@enum.next.should == 2
@enum.next.should == 3
end
it "raises a StopIteration exception at the end of the stream" do
3.times { @enum.next }
lambda { @enum.next }.should raise_error(StopIteration)
end
it "cannot be called again until the enumerator is rewound" do
3.times { @enum.next }
lambda { @enum.next }.should raise_error(StopIteration)
lambda { @enum.next }.should raise_error(StopIteration)
lambda { @enum.next }.should raise_error(StopIteration)
@enum.rewind
@enum.next.should == 1
end
end

View file

@ -1,39 +0,0 @@
require_relative '../../spec_helper'
describe :enum_rewind, shared: true do
before :each do
@enum = 1.upto(3)
end
it "resets the enumerator to its initial state" do
@enum.next.should == 1
@enum.next.should == 2
@enum.rewind
@enum.next.should == 1
end
it "returns self" do
@enum.rewind.should == @enum
end
it "has no effect on a new enumerator" do
@enum.rewind
@enum.next.should == 1
end
it "has no effect if called multiple, consecutive times" do
@enum.next.should == 1
@enum.rewind
@enum.rewind
@enum.next.should == 1
end
it "works with peek to reset the position" do
@enum.next
@enum.next
@enum.rewind
@enum.next
@enum.peek.should == 2
end
end