mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
75bfc6440d
commit
1d15d5f080
4370 changed files with 0 additions and 0 deletions
5
spec/ruby/core/enumerator/each_spec.rb
Normal file
5
spec/ruby/core/enumerator/each_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../../../shared/enumerator/each', __FILE__)
|
||||
|
||||
describe "Enumerator#each" do
|
||||
it_behaves_like(:enum_each, :each)
|
||||
end
|
38
spec/ruby/core/enumerator/each_with_index_spec.rb
Normal file
38
spec/ruby/core/enumerator/each_with_index_spec.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/with_index', __FILE__)
|
||||
require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__)
|
||||
|
||||
describe "Enumerator#each_with_index" do
|
||||
it_behaves_like(:enum_with_index, :each_with_index)
|
||||
it_behaves_like(:enumeratorized_with_origin_size, :each_with_index, [1,2,3].select)
|
||||
|
||||
it "returns a new Enumerator when no block is given" do
|
||||
enum1 = [1,2,3].select
|
||||
enum2 = enum1.each_with_index
|
||||
enum2.should be_an_instance_of(Enumerator)
|
||||
enum1.should_not === enum2
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if passed extra arguments" do
|
||||
lambda do
|
||||
[1].to_enum.each_with_index(:glark)
|
||||
end.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "passes on the given block's return value" do
|
||||
arr = [1,2,3]
|
||||
arr.delete_if.with_index { |a,b| false }
|
||||
arr.should == [1,2,3]
|
||||
end
|
||||
|
||||
it "returns the iterator's return value" do
|
||||
[1,2,3].select.with_index { |a,b| false }.should == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "Enumerator#each_with_index" do
|
||||
it "returns the correct value if chained with itself" do
|
||||
[:a].each_with_index.each_with_index.to_a.should == [[[:a,0],0]]
|
||||
[:a].each.with_index.with_index.to_a.should == [[[:a,0],0]]
|
||||
end
|
||||
end
|
6
spec/ruby/core/enumerator/each_with_object_spec.rb
Normal file
6
spec/ruby/core/enumerator/each_with_object_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/with_object', __FILE__)
|
||||
|
||||
describe "Enumerator#each_with_object" do
|
||||
it_behaves_like :enum_with_object, :each_with_object
|
||||
end
|
6
spec/ruby/core/enumerator/enum_for_spec.rb
Normal file
6
spec/ruby/core/enumerator/enum_for_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/enum_for', __FILE__)
|
||||
|
||||
describe "Enumerator#enum_for" do
|
||||
it_behaves_like :enum_for, :enum_for
|
||||
end
|
7
spec/ruby/core/enumerator/enumerator_spec.rb
Normal file
7
spec/ruby/core/enumerator/enumerator_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator" do
|
||||
it "includes Enumerable" do
|
||||
Enumerator.include?(Enumerable).should == true
|
||||
end
|
||||
end
|
52
spec/ruby/core/enumerator/feed_spec.rb
Normal file
52
spec/ruby/core/enumerator/feed_spec.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "Enumerator#feed" do
|
||||
before :each do
|
||||
ScratchPad.record []
|
||||
@enum = EnumeratorSpecs::Feed.new.to_enum(:each)
|
||||
end
|
||||
|
||||
it "sets the future return value of yield if called before advancing the iterator" do
|
||||
@enum.feed :a
|
||||
@enum.next
|
||||
@enum.next
|
||||
@enum.next
|
||||
ScratchPad.recorded.should == [:a, nil]
|
||||
end
|
||||
|
||||
it "causes yield to return the value if called during iteration" do
|
||||
@enum.next
|
||||
@enum.feed :a
|
||||
@enum.next
|
||||
@enum.next
|
||||
ScratchPad.recorded.should == [:a, nil]
|
||||
end
|
||||
|
||||
it "can be called for each iteration" do
|
||||
@enum.next
|
||||
@enum.feed :a
|
||||
@enum.next
|
||||
@enum.feed :b
|
||||
@enum.next
|
||||
ScratchPad.recorded.should == [:a, :b]
|
||||
end
|
||||
|
||||
it "returns nil" do
|
||||
@enum.feed(:a).should be_nil
|
||||
end
|
||||
|
||||
it "raises a TypeError if called more than once without advancing the enumerator" do
|
||||
@enum.feed :a
|
||||
@enum.next
|
||||
lambda { @enum.feed :b }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "sets the return value of Yielder#yield" do
|
||||
enum = Enumerator.new { |y| ScratchPad << y.yield }
|
||||
enum.next
|
||||
enum.feed :a
|
||||
lambda { enum.next }.should raise_error(StopIteration)
|
||||
ScratchPad.recorded.should == [:a]
|
||||
end
|
||||
end
|
7
spec/ruby/core/enumerator/first_spec.rb
Normal file
7
spec/ruby/core/enumerator/first_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator#first" do
|
||||
it "returns arrays correctly when calling #first (2376)" do
|
||||
Enumerator.new {|y| y << [42] }.first.should == [42]
|
||||
end
|
||||
end
|
9
spec/ruby/core/enumerator/fixtures/common.rb
Normal file
9
spec/ruby/core/enumerator/fixtures/common.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module EnumeratorSpecs
|
||||
class Feed
|
||||
def each
|
||||
ScratchPad << yield
|
||||
ScratchPad << yield
|
||||
ScratchPad << yield
|
||||
end
|
||||
end
|
||||
end
|
40
spec/ruby/core/enumerator/generator/each_spec.rb
Normal file
40
spec/ruby/core/enumerator/generator/each_spec.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
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 be_kind_of(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
|
||||
lambda { @generator.each }.should raise_error(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
|
26
spec/ruby/core/enumerator/generator/initialize_spec.rb
Normal file
26
spec/ruby/core/enumerator/generator/initialize_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator::Generator#initialize" do
|
||||
before :each do
|
||||
@class = Enumerator::Generator
|
||||
@uninitialized = @class.allocate
|
||||
end
|
||||
|
||||
it "is a private method" do
|
||||
@class.should have_private_instance_method(:initialize, false)
|
||||
end
|
||||
|
||||
it "returns self when given a block" do
|
||||
@uninitialized.send(:initialize) {}.should equal(@uninitialized)
|
||||
end
|
||||
|
||||
describe "on frozen instance" do
|
||||
it "raises a RuntimeError" do
|
||||
lambda {
|
||||
@uninitialized.freeze.send(:initialize) {}
|
||||
}.should raise_error(RuntimeError)
|
||||
end
|
||||
end
|
||||
end
|
61
spec/ruby/core/enumerator/initialize_spec.rb
Normal file
61
spec/ruby/core/enumerator/initialize_spec.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator#initialize" do
|
||||
before :each do
|
||||
@uninitialized = Enumerator.allocate
|
||||
end
|
||||
|
||||
it "is a private method" do
|
||||
Enumerator.should have_private_instance_method(:initialize, false)
|
||||
end
|
||||
|
||||
it "returns self when given an object" do
|
||||
@uninitialized.send(:initialize, Object.new).should equal(@uninitialized)
|
||||
end
|
||||
|
||||
it "returns self when given a block" do
|
||||
@uninitialized.send(:initialize) {}.should equal(@uninitialized)
|
||||
end
|
||||
|
||||
# Maybe spec should be broken up?
|
||||
it "accepts a block" do
|
||||
@uninitialized.send(:initialize) do |yielder|
|
||||
r = yielder.yield 3
|
||||
yielder << r << 2 << 1
|
||||
end
|
||||
@uninitialized.should be_an_instance_of(Enumerator)
|
||||
r = []
|
||||
@uninitialized.each{|x| r << x; x * 2}
|
||||
r.should == [3, 6, 2, 1]
|
||||
end
|
||||
|
||||
it "sets size to nil if size is not given" do
|
||||
@uninitialized.send(:initialize) {}.size.should be_nil
|
||||
end
|
||||
|
||||
it "sets size to nil if the given size is nil" do
|
||||
@uninitialized.send(:initialize, nil) {}.size.should be_nil
|
||||
end
|
||||
|
||||
it "sets size to the given size if the given size is Float::INFINITY" do
|
||||
@uninitialized.send(:initialize, Float::INFINITY) {}.size.should equal(Float::INFINITY)
|
||||
end
|
||||
|
||||
it "sets size to the given size if the given size is a Fixnum" do
|
||||
@uninitialized.send(:initialize, 100) {}.size.should == 100
|
||||
end
|
||||
|
||||
it "sets size to the given size if the given size is a Proc" do
|
||||
@uninitialized.send(:initialize, lambda { 200 }) {}.size.should == 200
|
||||
end
|
||||
|
||||
describe "on frozen instance" do
|
||||
it "raises a RuntimeError" do
|
||||
lambda {
|
||||
@uninitialized.freeze.send(:initialize) {}
|
||||
}.should raise_error(RuntimeError)
|
||||
end
|
||||
end
|
||||
end
|
15
spec/ruby/core/enumerator/inject_spec.rb
Normal file
15
spec/ruby/core/enumerator/inject_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
require File.expand_path('../../../shared/enumerator/each', __FILE__)
|
||||
|
||||
describe "Enumerator#inject" do
|
||||
it_behaves_like(:enum_each, :each)
|
||||
|
||||
it "works when chained against each_with_index" do
|
||||
passed_values = []
|
||||
[:a].each_with_index.inject(0) do |accumulator,value|
|
||||
passed_values << value
|
||||
accumulator + 1
|
||||
end.should == 1
|
||||
passed_values.should == [[:a,0]]
|
||||
end
|
||||
|
||||
end
|
17
spec/ruby/core/enumerator/inspect_spec.rb
Normal file
17
spec/ruby/core/enumerator/inspect_spec.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator#inspect" do
|
||||
describe "shows a representation of the Enumerator" do
|
||||
it "including receiver and method" do
|
||||
(1..3).each.inspect.should == "#<Enumerator: 1..3:each>"
|
||||
end
|
||||
|
||||
it "including receiver and method and arguments" do
|
||||
(1..3).each_slice(2).inspect.should == "#<Enumerator: 1..3:each_slice(2)>"
|
||||
end
|
||||
|
||||
it "including the nested Enumerator" do
|
||||
(1..3).each.each_slice(2).inspect.should == "#<Enumerator: #<Enumerator: 1..3:each>:each_slice(2)>"
|
||||
end
|
||||
end
|
||||
end
|
8
spec/ruby/core/enumerator/lazy/collect_concat_spec.rb
Normal file
8
spec/ruby/core/enumerator/lazy/collect_concat_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/collect_concat', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#collect_concat" do
|
||||
it_behaves_like :enumerator_lazy_collect_concat, :collect_concat
|
||||
end
|
8
spec/ruby/core/enumerator/lazy/collect_spec.rb
Normal file
8
spec/ruby/core/enumerator/lazy/collect_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/collect', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#collect" do
|
||||
it_behaves_like :enumerator_lazy_collect, :collect
|
||||
end
|
52
spec/ruby/core/enumerator/lazy/drop_spec.rb
Normal file
52
spec/ruby/core/enumerator/lazy/drop_spec.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#drop" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.drop(1)
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets difference of given count with old size to new size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.drop(20).size.should == 80
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.drop(200).size.should == 0
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.drop(2).first(2).should == [2, 3]
|
||||
|
||||
@eventsmixed.drop(0).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets difference of given count with old size to new size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.drop(20).drop(50).size.should == 30
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.drop(50).drop(20).size.should == 30
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.drop(2).drop(2).first(2).should == [4, 5]
|
||||
|
||||
@eventsmixed.drop(0).drop(0).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
60
spec/ruby/core/enumerator/lazy/drop_while_spec.rb
Normal file
60
spec/ruby/core/enumerator/lazy/drop_while_spec.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#drop_while" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.drop_while {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.drop_while { |v| v }.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.drop_while { |n| n < 5 }.first(2).should == [5, 6]
|
||||
|
||||
@eventsmixed.drop_while { false }.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with initial values when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.drop_while { |v| yields << v; true }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when not given a block" do
|
||||
lambda { @yieldsmixed.drop_while }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(20).drop_while { |v| v }.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.drop_while { |n| n < 5 }.drop_while { |n| n.odd? }.first(2).should == [6, 7]
|
||||
|
||||
@eventsmixed.drop_while { false }.drop_while { false }.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
8
spec/ruby/core/enumerator/lazy/enum_for_spec.rb
Normal file
8
spec/ruby/core/enumerator/lazy/enum_for_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/to_enum', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#enum_for" do
|
||||
it_behaves_like :enumerator_lazy_to_enum, :enum_for
|
||||
end
|
8
spec/ruby/core/enumerator/lazy/find_all_spec.rb
Normal file
8
spec/ruby/core/enumerator/lazy/find_all_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/select', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#find_all" do
|
||||
it_behaves_like :enumerator_lazy_select, :find_all
|
||||
end
|
54
spec/ruby/core/enumerator/lazy/fixtures/classes.rb
Normal file
54
spec/ruby/core/enumerator/lazy/fixtures/classes.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
module EnumeratorLazySpecs
|
||||
class SpecificError < Exception; end
|
||||
|
||||
class YieldsMixed
|
||||
def self.initial_yields
|
||||
[nil, 0, 0, 0, 0, nil, :default_arg, [], [], [0], [0, 1], [0, 1, 2]]
|
||||
end
|
||||
|
||||
def self.gathered_yields
|
||||
[nil, 0, [0, 1], [0, 1, 2], [0, 1, 2], nil, :default_arg, [], [], [0], [0, 1], [0, 1, 2]]
|
||||
end
|
||||
|
||||
def self.gathered_non_array_yields
|
||||
[nil, 0, nil, :default_arg]
|
||||
end
|
||||
|
||||
def self.gathered_yields_with_args(arg, *args)
|
||||
[nil, 0, [0, 1], [0, 1, 2], [0, 1, 2], nil, arg, args, [], [0], [0, 1], [0, 1, 2]]
|
||||
end
|
||||
|
||||
def each(arg=:default_arg, *args)
|
||||
yield
|
||||
yield 0
|
||||
yield 0, 1
|
||||
yield 0, 1, 2
|
||||
yield(*[0, 1, 2])
|
||||
yield nil
|
||||
yield arg
|
||||
yield args
|
||||
yield []
|
||||
yield [0]
|
||||
yield [0, 1]
|
||||
yield [0, 1, 2]
|
||||
end
|
||||
end
|
||||
|
||||
class EventsMixed
|
||||
def each
|
||||
ScratchPad << :before_yield
|
||||
|
||||
yield 0
|
||||
|
||||
ScratchPad << :after_yield
|
||||
|
||||
raise SpecificError
|
||||
|
||||
ScratchPad << :after_error
|
||||
|
||||
:should_not_reach_here
|
||||
end
|
||||
end
|
||||
end
|
8
spec/ruby/core/enumerator/lazy/flat_map_spec.rb
Normal file
8
spec/ruby/core/enumerator/lazy/flat_map_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/collect_concat', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#flat_map" do
|
||||
it_behaves_like :enumerator_lazy_collect_concat, :flat_map
|
||||
end
|
30
spec/ruby/core/enumerator/lazy/force_spec.rb
Normal file
30
spec/ruby/core/enumerator/lazy/force_spec.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#force" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "passes given arguments to receiver.each" do
|
||||
@yieldsmixed.force(:arg1, :arg2, :arg3).should ==
|
||||
EnumeratorLazySpecs::YieldsMixed.gathered_yields_with_args(:arg1, :arg2, :arg3)
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "calls all block and returns an Array" do
|
||||
(0..Float::INFINITY).lazy.map(&:succ).take(2).force.should == [1, 2]
|
||||
|
||||
@eventsmixed.take(1).map(&:succ).force.should == [1]
|
||||
ScratchPad.recorded == [:after_yields]
|
||||
end
|
||||
end
|
||||
end
|
82
spec/ruby/core/enumerator/lazy/grep_spec.rb
Normal file
82
spec/ruby/core/enumerator/lazy/grep_spec.rb
Normal file
|
@ -0,0 +1,82 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#grep" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "requires an argument" do
|
||||
Enumerator::Lazy.instance_method(:grep).arity.should == 1
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.grep(Object) {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
|
||||
ret = @yieldsmixed.grep(Object)
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep(Object) {}.size.should == nil
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep(Object).size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times when not given a block" do
|
||||
(0..Float::INFINITY).lazy.grep(Integer).first(3).should == [0, 1, 2]
|
||||
|
||||
@eventsmixed.grep(BasicObject).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "stops after specified times when given a block" do
|
||||
(0..Float::INFINITY).lazy.grep(Integer, &:succ).first(3).should == [1, 2, 3]
|
||||
|
||||
@eventsmixed.grep(BasicObject) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with a gathered array when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.grep(BasicObject) { |v| yields << v }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_yields
|
||||
|
||||
@yieldsmixed.grep(BasicObject).force.should == yields
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep(Object) {}.size.should == nil
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep(Object).size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times when not given a block" do
|
||||
(0..Float::INFINITY).lazy.grep(Integer).grep(Object).first(3).should == [0, 1, 2]
|
||||
|
||||
@eventsmixed.grep(BasicObject).grep(Object).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "stops after specified times when given a block" do
|
||||
(0..Float::INFINITY).lazy.grep(Integer) { |n| n > 3 ? n : false }.grep(Integer) { |n| n.even? ? n : false }.first(3).should == [4, false, 6]
|
||||
|
||||
@eventsmixed.grep(BasicObject) {}.grep(Object) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
86
spec/ruby/core/enumerator/lazy/grep_v_spec.rb
Normal file
86
spec/ruby/core/enumerator/lazy/grep_v_spec.rb
Normal file
|
@ -0,0 +1,86 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
ruby_version_is "2.3" do
|
||||
describe "Enumerator::Lazy#grep_v" do
|
||||
before(:each) do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "requires an argument" do
|
||||
Enumerator::Lazy.instance_method(:grep_v).arity.should == 1
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.grep_v(Object) {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
|
||||
ret = @yieldsmixed.grep_v(Object)
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object) {}.size.should == nil
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object).size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times when not given a block" do
|
||||
(0..Float::INFINITY).lazy.grep_v(3..5).first(3).should == [0, 1, 2]
|
||||
|
||||
@eventsmixed.grep_v(Symbol).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "stops after specified times when given a block" do
|
||||
(0..Float::INFINITY).lazy.grep_v(4..8, &:succ).first(3).should == [1, 2, 3]
|
||||
|
||||
@eventsmixed.grep_v(Symbol) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with a gathered array when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.grep_v(Array) { |v| yields << v }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_non_array_yields
|
||||
|
||||
@yieldsmixed.grep_v(Array).force.should == yields
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object).grep_v(Object) {}.size.should == nil
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object).grep_v(Object).size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times when not given a block" do
|
||||
(0..Float::INFINITY).lazy.grep_v(3..5).grep_v(6..10).first(3).should == [0, 1, 2]
|
||||
|
||||
@eventsmixed.grep_v(Symbol).grep_v(String).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "stops after specified times when given a block" do
|
||||
(0..Float::INFINITY).lazy
|
||||
.grep_v(1..2) { |n| n > 3 ? n : false }
|
||||
.grep_v(false) { |n| n.even? ? n : false }
|
||||
.first(3)
|
||||
.should == [4, false, 6]
|
||||
|
||||
@eventsmixed.grep_v(Symbol) {}.grep_v(String) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
63
spec/ruby/core/enumerator/lazy/initialize_spec.rb
Normal file
63
spec/ruby/core/enumerator/lazy/initialize_spec.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#initialize" do
|
||||
before :each do
|
||||
@receiver = receiver = Object.new
|
||||
|
||||
def receiver.each
|
||||
yield 0
|
||||
yield 1
|
||||
yield 2
|
||||
end
|
||||
|
||||
@uninitialized = Enumerator::Lazy.allocate
|
||||
end
|
||||
|
||||
it "is a private method" do
|
||||
Enumerator::Lazy.should have_private_instance_method(:initialize, false)
|
||||
end
|
||||
|
||||
it "returns self" do
|
||||
@uninitialized.send(:initialize, @receiver) {}.should equal(@uninitialized)
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
@uninitialized.send(:initialize, @receiver) do |yielder, *values|
|
||||
yielder.<<(*values)
|
||||
end.first(2).should == [0, 1]
|
||||
end
|
||||
end
|
||||
|
||||
it "sets #size to nil if not given a size" do
|
||||
@uninitialized.send(:initialize, @receiver) {}.size.should be_nil
|
||||
end
|
||||
|
||||
it "sets #size to nil if given size is nil" do
|
||||
@uninitialized.send(:initialize, @receiver, nil) {}.size.should be_nil
|
||||
end
|
||||
|
||||
it "sets given size to own size if the given size is Float::INFINITY" do
|
||||
@uninitialized.send(:initialize, @receiver, Float::INFINITY) {}.size.should equal(Float::INFINITY)
|
||||
end
|
||||
|
||||
it "sets given size to own size if the given size is a Fixnum" do
|
||||
@uninitialized.send(:initialize, @receiver, 100) {}.size.should == 100
|
||||
end
|
||||
|
||||
it "sets given size to own size if the given size is a Proc" do
|
||||
@uninitialized.send(:initialize, @receiver, lambda { 200 }) {}.size.should == 200
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when block is not given" do
|
||||
lambda { @uninitialized.send :initialize, @receiver }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
describe "on frozen instance" do
|
||||
it "raises a RuntimeError" do
|
||||
lambda { @uninitialized.freeze.send(:initialize, @receiver) {} }.should raise_error(RuntimeError)
|
||||
end
|
||||
end
|
||||
end
|
16
spec/ruby/core/enumerator/lazy/lazy_spec.rb
Normal file
16
spec/ruby/core/enumerator/lazy/lazy_spec.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy" do
|
||||
it "is a subclass of Enumerator" do
|
||||
Enumerator::Lazy.superclass.should equal(Enumerator)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Enumerator::Lazy#lazy" do
|
||||
it "returns self" do
|
||||
lazy = (1..3).to_enum.lazy
|
||||
lazy.lazy.should equal(lazy)
|
||||
end
|
||||
end
|
12
spec/ruby/core/enumerator/lazy/map_spec.rb
Normal file
12
spec/ruby/core/enumerator/lazy/map_spec.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/collect', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#map" do
|
||||
it_behaves_like :enumerator_lazy_collect, :map
|
||||
|
||||
it "doesn't unwrap Arrays" do
|
||||
Enumerator.new {|y| y.yield([1])}.lazy.to_a.should == [[1]]
|
||||
end
|
||||
end
|
60
spec/ruby/core/enumerator/lazy/reject_spec.rb
Normal file
60
spec/ruby/core/enumerator/lazy/reject_spec.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#reject" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.reject {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.reject {}.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.reject(&:even?).first(3).should == [1, 3, 5]
|
||||
|
||||
@eventsmixed.reject { false }.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with a gathered array when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.reject { |v| yields << v }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_yields
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when not given a block" do
|
||||
lambda { @yieldsmixed.reject }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(20).reject {}.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.reject { |n| n < 4 }.reject(&:even?).first(3).should == [5, 7, 9]
|
||||
|
||||
@eventsmixed.reject { false }.reject { false }.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
8
spec/ruby/core/enumerator/lazy/select_spec.rb
Normal file
8
spec/ruby/core/enumerator/lazy/select_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/select', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#select" do
|
||||
it_behaves_like :enumerator_lazy_select, :select
|
||||
end
|
56
spec/ruby/core/enumerator/lazy/shared/collect.rb
Normal file
56
spec/ruby/core/enumerator/lazy/shared/collect.rb
Normal file
|
@ -0,0 +1,56 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe :enumerator_lazy_collect, shared: true do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.send(@method) {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "keeps size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.send(@method) {}.size.should == 100
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.send(@method, &:succ).first(3).should == [1, 2, 3]
|
||||
|
||||
@eventsmixed.send(@method) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with initial values when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.send(@method) { |v| yields << v }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "keeps size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.send(@method) {}.send(@method) {}.size.should == 100
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.send(@method, &:succ).send(@method, &:succ).first(3).should == [2, 3, 4]
|
||||
|
||||
@eventsmixed.send(@method) {}.send(@method) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
72
spec/ruby/core/enumerator/lazy/shared/collect_concat.rb
Normal file
72
spec/ruby/core/enumerator/lazy/shared/collect_concat.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe :enumerator_lazy_collect_concat, shared: true do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.send(@method) {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.send(@method) { true }.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s }.first(6).should == %w[0 10 20 30 40 50]
|
||||
|
||||
@eventsmixed.send(@method) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "flattens elements when the given block returned an array or responding to .each and .force" do
|
||||
(0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s.chars }.first(6).should == %w[0 1 0 2 0 3]
|
||||
(0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s.each_char }.first(6).all? { |o| o.instance_of? Enumerator }.should be_true
|
||||
(0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s.each_char.lazy }.first(6).should == %w[0 1 0 2 0 3]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with initial values when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.send(@method) { |v| yields << v }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when not given a block" do
|
||||
lambda { @yieldsmixed.send(@method) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(50) {}.send(@method) {}.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s }.first(6).should == %w[0 10 20 30 40 50]
|
||||
|
||||
@eventsmixed.send(@method) {}.send(@method) {}.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "flattens elements when the given block returned an array or responding to .each and .force" do
|
||||
(0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s.chars }.first(6).should == %w[0 1 0 2 0 3]
|
||||
(0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s.each_char }.first(6).all? { |o| o.instance_of? Enumerator }.should be_true
|
||||
(0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s.each_char.lazy }.first(6).should == %w[0 1 0 2 0 3]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
60
spec/ruby/core/enumerator/lazy/shared/select.rb
Normal file
60
spec/ruby/core/enumerator/lazy/shared/select.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe :enumerator_lazy_select, shared: true do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.send(@method) {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.send(@method) { true }.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.send(@method, &:even?).first(3).should == [0, 2, 4]
|
||||
|
||||
@eventsmixed.send(@method) { true }.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with a gathered array when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.send(@method) { |v| yields << v }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_yields
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when not given a block" do
|
||||
lambda { @yieldsmixed.send(@method) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(50) {}.send(@method) { true }.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.send(@method) { |n| n > 5 }.send(@method, &:even?).first(3).should == [6, 8, 10]
|
||||
|
||||
@eventsmixed.send(@method) { true }.send(@method) { true }.first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
50
spec/ruby/core/enumerator/lazy/shared/to_enum.rb
Normal file
50
spec/ruby/core/enumerator/lazy/shared/to_enum.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../../spec_helper', __FILE__)
|
||||
|
||||
describe :enumerator_lazy_to_enum, shared: true do
|
||||
before :each do
|
||||
@infinite = (0..Float::INFINITY).lazy
|
||||
end
|
||||
|
||||
it "requires multiple arguments" do
|
||||
Enumerator::Lazy.instance_method(@method).arity.should < 0
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @infinite.send @method
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@infinite)
|
||||
end
|
||||
|
||||
it "sets #size to nil when not given a block" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.send(@method).size.should == nil
|
||||
end
|
||||
|
||||
it "sets given block to size when given a block" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.send(@method) { 30 }.size.should == 30
|
||||
end
|
||||
|
||||
it "generates a lazy enumerator from the given name" do
|
||||
@infinite.send(@method, :with_index, 10).first(3).should == [[0, 10], [1, 11], [2, 12]]
|
||||
end
|
||||
|
||||
it "passes given arguments to wrapped method" do
|
||||
@infinite.send(@method, :each_slice, 2).map { |assoc| assoc.first * assoc.last }.first(4).should == [0, 6, 20, 42]
|
||||
end
|
||||
|
||||
it "used by some parent's methods though returning Lazy" do
|
||||
{ each_with_index: [],
|
||||
with_index: [],
|
||||
cycle: [1],
|
||||
each_with_object: [Object.new],
|
||||
with_object: [Object.new],
|
||||
each_slice: [2],
|
||||
each_entry: [],
|
||||
each_cons: [2]
|
||||
}.each_pair do |method, args|
|
||||
@infinite.method(method).owner.should_not equal(Enumerator::Lazy)
|
||||
@infinite.send(method, *args).should be_an_instance_of(Enumerator::Lazy)
|
||||
end
|
||||
end
|
||||
end
|
66
spec/ruby/core/enumerator/lazy/take_spec.rb
Normal file
66
spec/ruby/core/enumerator/lazy/take_spec.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#take" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.take(1)
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets given count to size if the given count is less than old size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(20).size.should == 20
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(200).size.should == 100
|
||||
end
|
||||
|
||||
it "sets given count to size if the old size is Infinity" do
|
||||
loop.lazy.take(20).size.should == 20
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by .force" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.take(2).force.should == [0, 1]
|
||||
|
||||
@eventsmixed.take(1).force
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "stops without iterations if the given argument is 0" do
|
||||
@eventsmixed.take(0).force
|
||||
ScratchPad.recorded.should == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets given count to size if the given count is less than old size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(20).take(50).size.should == 20
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(50).take(20).size.should == 20
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by .force" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.map(&:succ).take(2).force.should == [1, 2]
|
||||
|
||||
@eventsmixed.take(10).take(1).force
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
|
||||
it "stops without iterations if the given argument is 0" do
|
||||
@eventsmixed.take(10).take(0).force
|
||||
ScratchPad.recorded.should == []
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
60
spec/ruby/core/enumerator/lazy/take_while_spec.rb
Normal file
60
spec/ruby/core/enumerator/lazy/take_while_spec.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#take_while" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.take_while {}
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take_while { true }.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by .force" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.take_while { |n| n < 3 }.force.should == [0, 1, 2]
|
||||
|
||||
@eventsmixed.take_while { false }.force
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with initial values when yield with multiple arguments" do
|
||||
yields = []
|
||||
@yieldsmixed.take_while { |v| yields << v; true }.force
|
||||
yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when not given a block" do
|
||||
lambda { @yieldsmixed.take_while }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "sets #size to nil" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.take(20).take_while { true }.size.should == nil
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by .force" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.take_while { |n| n < 3 }.take_while(&:even?).force.should == [0]
|
||||
|
||||
@eventsmixed.take_while { true }.take_while { false }.force
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
8
spec/ruby/core/enumerator/lazy/to_enum_spec.rb
Normal file
8
spec/ruby/core/enumerator/lazy/to_enum_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/to_enum', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#to_enum" do
|
||||
it_behaves_like :enumerator_lazy_to_enum, :to_enum
|
||||
end
|
39
spec/ruby/core/enumerator/lazy/uniq_spec.rb
Normal file
39
spec/ruby/core/enumerator/lazy/uniq_spec.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
ruby_version_is '2.4' do
|
||||
describe 'Enumerator::Lazy#uniq' do
|
||||
context 'when yielded with an argument' do
|
||||
before :each do
|
||||
@lazy = [0, 1, 2, 3].to_enum.lazy.uniq(&:even?)
|
||||
end
|
||||
|
||||
it 'returns a lazy enumerator' do
|
||||
@lazy.should be_an_instance_of(Enumerator::Lazy)
|
||||
@lazy.force.should == [0, 1]
|
||||
end
|
||||
|
||||
it 'sets the size to nil' do
|
||||
@lazy.size.should == nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when yielded with multiple arguments' do
|
||||
before :each do
|
||||
enum = Object.new.to_enum
|
||||
class << enum
|
||||
def each
|
||||
yield 0, 'foo'
|
||||
yield 1, 'FOO'
|
||||
yield 2, 'bar'
|
||||
end
|
||||
end
|
||||
@lazy = enum.lazy
|
||||
end
|
||||
|
||||
it 'returns all yield arguments as an array' do
|
||||
@lazy.uniq { |_, label| label.downcase }.force.should == [[0, 'foo'], [2, 'bar']]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
74
spec/ruby/core/enumerator/lazy/zip_spec.rb
Normal file
74
spec/ruby/core/enumerator/lazy/zip_spec.rb
Normal file
|
@ -0,0 +1,74 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Enumerator::Lazy#zip" do
|
||||
before :each do
|
||||
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
|
||||
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
after :each do
|
||||
ScratchPad.clear
|
||||
end
|
||||
|
||||
it "returns a new instance of Enumerator::Lazy" do
|
||||
ret = @yieldsmixed.zip []
|
||||
ret.should be_an_instance_of(Enumerator::Lazy)
|
||||
ret.should_not equal(@yieldsmixed)
|
||||
end
|
||||
|
||||
it "keeps size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.zip([], []).size.should == 100
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.zip([4, 5], [8]).first(2).should == [[0, 4, 8], [1, 5, nil]]
|
||||
|
||||
@eventsmixed.zip([0, 1]).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the block with a gathered array when yield with multiple arguments" do
|
||||
yields = @yieldsmixed.zip(EnumeratorLazySpecs::YieldsMixed.new.to_enum).force
|
||||
yields.should == [EnumeratorLazySpecs::YieldsMixed.gathered_yields,
|
||||
EnumeratorLazySpecs::YieldsMixed.gathered_yields].transpose
|
||||
end
|
||||
|
||||
it "returns a Lazy when no arguments given" do
|
||||
@yieldsmixed.zip.should be_an_instance_of(Enumerator::Lazy)
|
||||
end
|
||||
|
||||
it "raises a TypeError if arguments contain non-list object" do
|
||||
lambda { @yieldsmixed.zip [], Object.new, [] }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
describe "on a nested Lazy" do
|
||||
it "keeps size" do
|
||||
Enumerator::Lazy.new(Object.new, 100) {}.map {}.zip([], []).size.should == 100
|
||||
end
|
||||
|
||||
it "behaves as Enumerable#zip when given a block" do
|
||||
lazy_yields = []
|
||||
lazy_ret = @yieldsmixed.zip(EnumeratorLazySpecs::YieldsMixed.new.to_enum) { |lists| lazy_yields << lists }
|
||||
enum_yields = []
|
||||
enum_ret = EnumeratorLazySpecs::YieldsMixed.new.to_enum.zip(EnumeratorLazySpecs::YieldsMixed.new.to_enum) { |lists| enum_yields << lists }
|
||||
|
||||
lazy_yields.should == enum_yields
|
||||
lazy_ret.should == enum_ret
|
||||
end
|
||||
|
||||
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
|
||||
it "stops after specified times" do
|
||||
(0..Float::INFINITY).lazy.map(&:succ).zip([4, 5], [8]).first(2).should == [[1, 4, 8], [2, 5, nil]]
|
||||
|
||||
@eventsmixed.zip([0, 1]).zip([0, 1]).first(1)
|
||||
ScratchPad.recorded.should == [:before_yield]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
6
spec/ruby/core/enumerator/new_spec.rb
Normal file
6
spec/ruby/core/enumerator/new_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/new', __FILE__)
|
||||
|
||||
describe "Enumerator.new" do
|
||||
it_behaves_like(:enum_new, :new)
|
||||
end
|
6
spec/ruby/core/enumerator/next_spec.rb
Normal file
6
spec/ruby/core/enumerator/next_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/next', __FILE__)
|
||||
|
||||
describe "Enumerator#next" do
|
||||
it_behaves_like(:enum_next,:next)
|
||||
end
|
55
spec/ruby/core/enumerator/next_values_spec.rb
Normal file
55
spec/ruby/core/enumerator/next_values_spec.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator#next_values" do
|
||||
before :each do
|
||||
o = Object.new
|
||||
def o.each
|
||||
yield :a
|
||||
yield :b1, :b2
|
||||
yield :c
|
||||
yield :d1, :d2
|
||||
yield :e1, :e2, :e3
|
||||
yield nil
|
||||
yield
|
||||
end
|
||||
|
||||
@e = o.to_enum
|
||||
end
|
||||
|
||||
it "returns the next element in self" do
|
||||
@e.next_values.should == [:a]
|
||||
end
|
||||
|
||||
it "advances the position of the current element" do
|
||||
@e.next.should == :a
|
||||
@e.next_values.should == [:b1, :b2]
|
||||
@e.next.should == :c
|
||||
end
|
||||
|
||||
it "advances the position of the enumerator each time when called multiple times" do
|
||||
2.times { @e.next_values }
|
||||
@e.next_values.should == [:c]
|
||||
@e.next.should == [:d1, :d2]
|
||||
end
|
||||
|
||||
it "works in concert with #rewind" do
|
||||
2.times { @e.next }
|
||||
@e.rewind
|
||||
@e.next_values.should == [:a]
|
||||
end
|
||||
|
||||
it "returns an array with only nil if yield is called with nil" do
|
||||
5.times { @e.next }
|
||||
@e.next_values.should == [nil]
|
||||
end
|
||||
|
||||
it "returns an empty array if yield is called without arguments" do
|
||||
6.times { @e.next }
|
||||
@e.next_values.should == []
|
||||
end
|
||||
|
||||
it "raises StopIteration if called on a finished enumerator" do
|
||||
7.times { @e.next }
|
||||
lambda { @e.next_values }.should raise_error(StopIteration)
|
||||
end
|
||||
end
|
36
spec/ruby/core/enumerator/peek_spec.rb
Normal file
36
spec/ruby/core/enumerator/peek_spec.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator#peek" do
|
||||
before :each do
|
||||
@e = (1..5).to_a.to_enum
|
||||
end
|
||||
|
||||
it "returns the next element in self" do
|
||||
@e.peek.should == 1
|
||||
end
|
||||
|
||||
it "does not advance the position of the current element" do
|
||||
@e.next.should == 1
|
||||
@e.peek.should == 2
|
||||
@e.next.should == 2
|
||||
end
|
||||
|
||||
it "can be called repeatedly without advancing the position of the current element" do
|
||||
@e.peek
|
||||
@e.peek
|
||||
@e.peek.should == 1
|
||||
@e.next.should == 1
|
||||
end
|
||||
|
||||
it "works in concert with #rewind" do
|
||||
@e.next
|
||||
@e.next
|
||||
@e.rewind
|
||||
@e.peek.should == 1
|
||||
end
|
||||
|
||||
it "raises StopIteration if called on a finished enumerator" do
|
||||
5.times { @e.next }
|
||||
lambda { @e.peek }.should raise_error(StopIteration)
|
||||
end
|
||||
end
|
57
spec/ruby/core/enumerator/peek_values_spec.rb
Normal file
57
spec/ruby/core/enumerator/peek_values_spec.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator#peek_values" do
|
||||
before :each do
|
||||
o = Object.new
|
||||
def o.each
|
||||
yield :a
|
||||
yield :b1, :b2
|
||||
yield :c
|
||||
yield :d1, :d2
|
||||
yield :e1, :e2, :e3
|
||||
yield nil
|
||||
yield
|
||||
end
|
||||
|
||||
@e = o.to_enum
|
||||
end
|
||||
|
||||
it "returns the next element in self" do
|
||||
@e.peek_values.should == [:a]
|
||||
end
|
||||
|
||||
it "does not advance the position of the current element" do
|
||||
@e.next.should == :a
|
||||
@e.peek_values.should == [:b1, :b2]
|
||||
@e.next.should == [:b1, :b2]
|
||||
end
|
||||
|
||||
it "can be called repeatedly without advancing the position of the current element" do
|
||||
@e.peek_values
|
||||
@e.peek_values
|
||||
@e.peek_values.should == [:a]
|
||||
@e.next.should == :a
|
||||
end
|
||||
|
||||
it "works in concert with #rewind" do
|
||||
@e.next
|
||||
@e.next
|
||||
@e.rewind
|
||||
@e.peek_values.should == [:a]
|
||||
end
|
||||
|
||||
it "returns an array with only nil if yield is called with nil" do
|
||||
5.times { @e.next }
|
||||
@e.peek_values.should == [nil]
|
||||
end
|
||||
|
||||
it "returns an empty array if yield is called without arguments" do
|
||||
6.times { @e.next }
|
||||
@e.peek_values.should == []
|
||||
end
|
||||
|
||||
it "raises StopIteration if called on a finished enumerator" do
|
||||
7.times { @e.next }
|
||||
lambda { @e.peek_values }.should raise_error(StopIteration)
|
||||
end
|
||||
end
|
38
spec/ruby/core/enumerator/rewind_spec.rb
Normal file
38
spec/ruby/core/enumerator/rewind_spec.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/rewind', __FILE__)
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "Enumerator#rewind" do
|
||||
it_behaves_like(:enum_rewind, :rewind)
|
||||
|
||||
it "calls the enclosed object's rewind method if one exists" do
|
||||
obj = mock('rewinder')
|
||||
enum = obj.to_enum
|
||||
obj.should_receive(:each).at_most(1)
|
||||
obj.should_receive(:rewind)
|
||||
enum.rewind
|
||||
end
|
||||
|
||||
it "does nothing if the object doesn't have a #rewind method" do
|
||||
obj = mock('rewinder')
|
||||
enum = obj.to_enum
|
||||
obj.should_receive(:each).at_most(1)
|
||||
lambda { enum.rewind.should == enum }.should_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe "Enumerator#rewind" do
|
||||
before :each do
|
||||
ScratchPad.record []
|
||||
@enum = EnumeratorSpecs::Feed.new.to_enum(:each)
|
||||
end
|
||||
|
||||
it "clears a pending #feed value" do
|
||||
@enum.next
|
||||
@enum.feed :a
|
||||
@enum.rewind
|
||||
@enum.next
|
||||
@enum.next
|
||||
ScratchPad.recorded.should == [nil]
|
||||
end
|
||||
end
|
26
spec/ruby/core/enumerator/size_spec.rb
Normal file
26
spec/ruby/core/enumerator/size_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator#size" do
|
||||
it "returns same value if set size is an Integer" do
|
||||
Enumerator.new(100) {}.size.should == 100
|
||||
end
|
||||
|
||||
it "returns nil if set size is nil" do
|
||||
Enumerator.new(nil) {}.size.should be_nil
|
||||
end
|
||||
|
||||
it "returns returning value from size.call if set size is a Proc" do
|
||||
base_size = 100
|
||||
enum = Enumerator.new(lambda { base_size + 1 }) {}
|
||||
base_size = 200
|
||||
enum.size.should == 201
|
||||
base_size = 300
|
||||
enum.size.should == 301
|
||||
end
|
||||
|
||||
it "returns the result from size.call if the size respond to call" do
|
||||
obj = mock('call')
|
||||
obj.should_receive(:call).and_return(42)
|
||||
Enumerator.new(obj) {}.size.should == 42
|
||||
end
|
||||
end
|
6
spec/ruby/core/enumerator/to_enum_spec.rb
Normal file
6
spec/ruby/core/enumerator/to_enum_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/enum_for', __FILE__)
|
||||
|
||||
describe "Enumerator#to_enum" do
|
||||
it_behaves_like :enum_for, :enum_for
|
||||
end
|
72
spec/ruby/core/enumerator/with_index_spec.rb
Normal file
72
spec/ruby/core/enumerator/with_index_spec.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/with_index', __FILE__)
|
||||
require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__)
|
||||
|
||||
describe "Enumerator#with_index" do
|
||||
it_behaves_like(:enum_with_index, :with_index)
|
||||
it_behaves_like(:enumeratorized_with_origin_size, :with_index, [1,2,3].select)
|
||||
|
||||
it "returns a new Enumerator when no block is given" do
|
||||
enum1 = [1,2,3].select
|
||||
enum2 = enum1.with_index
|
||||
enum2.should be_an_instance_of(Enumerator)
|
||||
enum1.should_not === enum2
|
||||
end
|
||||
|
||||
it "accepts an optional argument when given a block" do
|
||||
lambda do
|
||||
@enum.with_index(1) { |f| f}
|
||||
end.should_not raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "accepts an optional argument when not given a block" do
|
||||
lambda do
|
||||
@enum.with_index(1)
|
||||
end.should_not raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "numbers indices from the given index when given an offset but no block" do
|
||||
@enum.with_index(1).to_a.should == [[1,1], [2,2], [3,3], [4,4]]
|
||||
end
|
||||
|
||||
it "numbers indices from the given index when given an offset and block" do
|
||||
acc = []
|
||||
@enum.with_index(1) {|e,i| acc << [e,i] }
|
||||
acc.should == [[1,1], [2,2], [3,3], [4,4]]
|
||||
end
|
||||
|
||||
it "raises a TypeError when the argument cannot be converted to numeric" do
|
||||
lambda do
|
||||
@enum.with_index('1') {|*i| i}
|
||||
end.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "converts non-numeric arguments to Integer via #to_int" do
|
||||
(o = mock('1')).should_receive(:to_int).and_return(1)
|
||||
@enum.with_index(o).to_a.should == [[1,1], [2,2], [3,3], [4,4]]
|
||||
end
|
||||
|
||||
it "coerces the given numeric argument to an Integer" do
|
||||
@enum.with_index(1.678).to_a.should == [[1,1], [2,2], [3,3], [4,4]]
|
||||
|
||||
res = []
|
||||
@enum.with_index(1.001) { |*x| res << x}
|
||||
res.should == [[1,1], [2,2], [3,3], [4,4]]
|
||||
end
|
||||
|
||||
it "treats nil argument as no argument" do
|
||||
@enum.with_index(nil).to_a.should == [[1,0], [2,1], [3,2], [4,3]]
|
||||
|
||||
res = []
|
||||
@enum.with_index(nil) { |*x| res << x}
|
||||
res.should == [[1,0], [2,1], [3,2], [4,3]]
|
||||
end
|
||||
|
||||
it "accepts negative argument" do
|
||||
@enum.with_index(-1).to_a.should == [[1,-1], [2,0], [3,1], [4,2]]
|
||||
|
||||
res = []
|
||||
@enum.with_index(-1) { |*x| res << x}
|
||||
res.should == [[1,-1], [2,0], [3,1], [4,2]]
|
||||
end
|
||||
end
|
6
spec/ruby/core/enumerator/with_object_spec.rb
Normal file
6
spec/ruby/core/enumerator/with_object_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../shared/enumerator/with_object', __FILE__)
|
||||
|
||||
describe "Enumerator#with_object" do
|
||||
it_behaves_like :enum_with_object, :with_object
|
||||
end
|
35
spec/ruby/core/enumerator/yielder/append_spec.rb
Normal file
35
spec/ruby/core/enumerator/yielder/append_spec.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
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
|
||||
|
||||
it "requires multiple arguments" do
|
||||
Enumerator::Yielder.instance_method(:<<).arity.should < 0
|
||||
end
|
||||
|
||||
it "yields with passed arguments" do
|
||||
yields = []
|
||||
y = Enumerator::Yielder.new {|*args| yields << args }
|
||||
y.<<(1, 2)
|
||||
yields.should == [[1, 2]]
|
||||
end
|
||||
end
|
18
spec/ruby/core/enumerator/yielder/initialize_spec.rb
Normal file
18
spec/ruby/core/enumerator/yielder/initialize_spec.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# -*- encoding: us-ascii -*-
|
||||
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Enumerator::Yielder#initialize" do
|
||||
before :each do
|
||||
@class = Enumerator::Yielder
|
||||
@uninitialized = @class.allocate
|
||||
end
|
||||
|
||||
it "is a private method" do
|
||||
@class.should have_private_instance_method(:initialize, false)
|
||||
end
|
||||
|
||||
it "returns self when given a block" do
|
||||
@uninitialized.send(:initialize) {}.should equal(@uninitialized)
|
||||
end
|
||||
end
|
16
spec/ruby/core/enumerator/yielder/yield_spec.rb
Normal file
16
spec/ruby/core/enumerator/yielder/yield_spec.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
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 "returns the result of the block for the given value" do
|
||||
y = Enumerator::Yielder.new {|x| x + 1}
|
||||
y.yield(1).should == 2
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue