Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add cases to Enumerable regarding yielding multiple arguments

Each of these methods needs to have defined how it behaves when
having multiple arguments. The behavior for each method can
be different and therefore has to be specced.
  • Loading branch information...
commit 4b6d9701de326d037aeb4692c80435fc895f58c1 1 parent b46a141
Dirkjan Bussink dbussink authored brixen committed
13 core/enumerable/all_spec.rb
View
@@ -52,6 +52,12 @@
EnumerableSpecs::Numerous.new(0, "x", false, true).all?.should == false
@enum2.all?.should == false
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMultiWithFalse.new
+ multi.all?.should be_true
+ end
+
end
describe "with block" do
@@ -113,5 +119,12 @@
end
end
+ it "yields multiple arguments when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ yielded = []
+ multi.all? {|e, i| yielded << [e, i] }
+ yielded.should == [[1, 2], [3, 4], [6, 7]]
+ end
+
end
end
12 core/enumerable/any_spec.rb
View
@@ -59,6 +59,11 @@
EnumerableSpecs::Numerous.new(nil, nil).any?.should == false
EnumerableSpecs::Numerous.new(nil, false, nil).any?.should == false
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMultiWithFalse.new
+ multi.any?.should be_true
+ end
end
describe "with block" do
@@ -134,5 +139,12 @@
end
end
+ it "yields multiple arguments when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ yielded = []
+ multi.any? {|e, i| yielded << [e, i] }
+ yielded.should == [[1, 2]]
+ end
+
end
end
18 core/enumerable/fixtures/classes.rb
View
@@ -153,6 +153,24 @@ def each
end
end
+ class YieldsMultiWithFalse
+ include Enumerable
+ def each
+ yield false,2
+ yield false,4,5
+ yield false,7,8,9
+ end
+ end
+
+ class YieldsMultiWithSingleTrue
+ include Enumerable
+ def each
+ yield false,2
+ yield true,4,5
+ yield false,7,8,9
+ end
+ end
+
class YieldsMixed
include Enumerable
def each
26 core/enumerable/group_by_spec.rb
View
@@ -2,18 +2,22 @@
require File.expand_path('../fixtures/classes', __FILE__)
describe "Enumerable#group_by" do
- ruby_version_is "1.8.7" do
- it "returns a hash with values grouped according to the block" do
- EnumerableSpecs::Numerous.new(*%w(foo bar baz)).group_by{ |word| word[0..0].to_sym }.
- should == { :f => ["foo"], :b => ["bar", "baz"]}
- end
+ it "returns a hash with values grouped according to the block" do
+ EnumerableSpecs::Numerous.new(*%w(foo bar baz)).group_by{ |word| word[0..0].to_sym }.
+ should == { :f => ["foo"], :b => ["bar", "baz"]}
+ end
- it "returns an empty hash for empty enumerables" do
- EnumerableSpecs::Empty.new.group_by { |x| x}.should == {}
- end
+ it "returns an empty hash for empty enumerables" do
+ EnumerableSpecs::Empty.new.group_by { |x| x}.should == {}
+ end
- it "returns an Enumerator if called without a block" do
- EnumerableSpecs::Numerous.new.group_by.should be_an_instance_of(enumerator_class)
- end
+ it "returns an Enumerator if called without a block" do
+ EnumerableSpecs::Numerous.new.group_by.should be_an_instance_of(enumerator_class)
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.group_by {|e| e}.should == {[1, 2]=>[[1, 2]], [6, 7, 8, 9]=>[[6, 7, 8, 9]], [3, 4, 5]=>[[3, 4, 5]]}
+ end
+
end
68 core/enumerable/max_by_spec.rb
View
@@ -2,37 +2,41 @@
require File.expand_path('../fixtures/classes', __FILE__)
describe "Enumerable#max_by" do
- ruby_version_is '1.8.7' do
- it "returns an enumerator if no block" do
- EnumerableSpecs::Numerous.new(42).max_by.should be_an_instance_of(enumerator_class)
- end
-
- it "returns nil if #each yields no objects" do
- EnumerableSpecs::Empty.new.max_by {|o| o.nonesuch }.should == nil
- end
-
-
- it "returns the object for whom the value returned by block is the largest" do
- EnumerableSpecs::Numerous.new(*%w[1 2 3]).max_by {|obj| obj.to_i }.should == '3'
- EnumerableSpecs::Numerous.new(*%w[three five]).max_by {|obj| obj.length }.should == 'three'
- end
-
- it "returns the object that appears first in #each in case of a tie" do
- a, b, c = '1', '2', '2'
- EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj.to_i }.should equal(b)
- end
-
- it "uses max.<=>(current) to determine order" do
- a, b, c = (1..3).map{|n| EnumerableSpecs::ReverseComparable.new(n)}
-
- # Just using self here to avoid additional complexity
- EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj }.should == a
- end
-
- it "is able to return the maximum for enums that contain nils" do
- enum = EnumerableSpecs::Numerous.new(nil, nil, true)
- enum.max_by {|o| o.nil? ? 0 : 1 }.should == true
- enum.max_by {|o| o.nil? ? 1 : 0 }.should == nil
- end
+
+ it "returns an enumerator if no block" do
+ EnumerableSpecs::Numerous.new(42).max_by.should be_an_instance_of(enumerator_class)
+ end
+
+ it "returns nil if #each yields no objects" do
+ EnumerableSpecs::Empty.new.max_by {|o| o.nonesuch }.should == nil
+ end
+
+ it "returns the object for whom the value returned by block is the largest" do
+ EnumerableSpecs::Numerous.new(*%w[1 2 3]).max_by {|obj| obj.to_i }.should == '3'
+ EnumerableSpecs::Numerous.new(*%w[three five]).max_by {|obj| obj.length }.should == 'three'
+ end
+
+ it "returns the object that appears first in #each in case of a tie" do
+ a, b, c = '1', '2', '2'
+ EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj.to_i }.should equal(b)
end
+
+ it "uses max.<=>(current) to determine order" do
+ a, b, c = (1..3).map{|n| EnumerableSpecs::ReverseComparable.new(n)}
+
+ # Just using self here to avoid additional complexity
+ EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj }.should == a
+ end
+
+ it "is able to return the maximum for enums that contain nils" do
+ enum = EnumerableSpecs::Numerous.new(nil, nil, true)
+ enum.max_by {|o| o.nil? ? 0 : 1 }.should == true
+ enum.max_by {|o| o.nil? ? 1 : 0 }.should == nil
+ end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.max_by {|e| e.size}.should == [6, 7, 8, 9]
+ end
+
end
6 core/enumerable/max_spec.rb
View
@@ -87,4 +87,10 @@
x <=> y
}.should == nil
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.max.should == [6, 7, 8, 9]
+ end
+
end
67 core/enumerable/min_by_spec.rb
View
@@ -2,37 +2,40 @@
require File.expand_path('../fixtures/classes', __FILE__)
describe "Enumerable#min_by" do
- ruby_version_is '1.8.7' do
- it "returns an enumerator if no block" do
- EnumerableSpecs::Numerous.new(42).min_by.should be_an_instance_of(enumerator_class)
- end
-
- it "returns nil if #each yields no objects" do
- EnumerableSpecs::Empty.new.min_by {|o| o.nonesuch }.should == nil
- end
-
-
- it "returns the object for whom the value returned by block is the largest" do
- EnumerableSpecs::Numerous.new(*%w[3 2 1]).min_by {|obj| obj.to_i }.should == '1'
- EnumerableSpecs::Numerous.new(*%w[five three]).min_by {|obj| obj.length }.should == 'five'
- end
-
- it "returns the object that appears first in #each in case of a tie" do
- a, b, c = '2', '1', '1'
- EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj.to_i }.should equal(b)
- end
-
- it "uses min.<=>(current) to determine order" do
- a, b, c = (1..3).map{|n| EnumerableSpecs::ReverseComparable.new(n)}
-
- # Just using self here to avoid additional complexity
- EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj }.should == c
- end
-
- it "is able to return the maximum for enums that contain nils" do
- enum = EnumerableSpecs::Numerous.new(nil, nil, true)
- enum.min_by {|o| o.nil? ? 0 : 1 }.should == nil
- enum.min_by {|o| o.nil? ? 1 : 0 }.should == true
- end
+ it "returns an enumerator if no block" do
+ EnumerableSpecs::Numerous.new(42).min_by.should be_an_instance_of(enumerator_class)
+ end
+
+ it "returns nil if #each yields no objects" do
+ EnumerableSpecs::Empty.new.min_by {|o| o.nonesuch }.should == nil
+ end
+
+
+ it "returns the object for whom the value returned by block is the largest" do
+ EnumerableSpecs::Numerous.new(*%w[3 2 1]).min_by {|obj| obj.to_i }.should == '1'
+ EnumerableSpecs::Numerous.new(*%w[five three]).min_by {|obj| obj.length }.should == 'five'
+ end
+
+ it "returns the object that appears first in #each in case of a tie" do
+ a, b, c = '2', '1', '1'
+ EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj.to_i }.should equal(b)
+ end
+
+ it "uses min.<=>(current) to determine order" do
+ a, b, c = (1..3).map{|n| EnumerableSpecs::ReverseComparable.new(n)}
+
+ # Just using self here to avoid additional complexity
+ EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj }.should == c
+ end
+
+ it "is able to return the maximum for enums that contain nils" do
+ enum = EnumerableSpecs::Numerous.new(nil, nil, true)
+ enum.min_by {|o| o.nil? ? 0 : 1 }.should == nil
+ enum.min_by {|o| o.nil? ? 1 : 0 }.should == true
+ end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.min_by {|e| e.size}.should == [1, 2]
end
end
6 core/enumerable/min_spec.rb
View
@@ -92,4 +92,10 @@
x <=> y
}.should == nil
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.min.should == [1, 2]
+ end
+
end
68 core/enumerable/minmax_by_spec.rb
View
@@ -2,37 +2,41 @@
require File.expand_path('../fixtures/classes', __FILE__)
describe "Enumerable#minmax_by" do
- ruby_version_is '1.8.7' do
- it "returns an enumerator if no block" do
- EnumerableSpecs::Numerous.new(42).minmax_by.should be_an_instance_of(enumerator_class)
- end
-
- it "returns nil if #each yields no objects" do
- EnumerableSpecs::Empty.new.minmax_by {|o| o.nonesuch }.should == [nil, nil]
- end
-
- it "returns the object for whom the value returned by block is the largest" do
- EnumerableSpecs::Numerous.new(*%w[1 2 3]).minmax_by {|obj| obj.to_i }.should == ['1', '3']
- EnumerableSpecs::Numerous.new(*%w[three five]).minmax_by {|obj| obj.length }.should == ['five', 'three']
- end
-
- it "returns the object that appears first in #each in case of a tie" do
- a, b, c, d = '1', '1', '2', '2'
- mm = EnumerableSpecs::Numerous.new(a, b, c, d).minmax_by {|obj| obj.to_i }
- mm[0].should equal(a)
- mm[1].should equal(c)
- end
-
- it "uses min/max.<=>(current) to determine order" do
- a, b, c = (1..3).map{|n| EnumerableSpecs::ReverseComparable.new(n)}
-
- # Just using self here to avoid additional complexity
- EnumerableSpecs::Numerous.new(a, b, c).minmax_by {|obj| obj }.should == [c, a]
- end
-
- it "is able to return the maximum for enums that contain nils" do
- enum = EnumerableSpecs::Numerous.new(nil, nil, true)
- enum.minmax_by {|o| o.nil? ? 0 : 1 }.should == [nil, true]
- end
+ it "returns an enumerator if no block" do
+ EnumerableSpecs::Numerous.new(42).minmax_by.should be_an_instance_of(enumerator_class)
end
+
+ it "returns nil if #each yields no objects" do
+ EnumerableSpecs::Empty.new.minmax_by {|o| o.nonesuch }.should == [nil, nil]
+ end
+
+ it "returns the object for whom the value returned by block is the largest" do
+ EnumerableSpecs::Numerous.new(*%w[1 2 3]).minmax_by {|obj| obj.to_i }.should == ['1', '3']
+ EnumerableSpecs::Numerous.new(*%w[three five]).minmax_by {|obj| obj.length }.should == ['five', 'three']
+ end
+
+ it "returns the object that appears first in #each in case of a tie" do
+ a, b, c, d = '1', '1', '2', '2'
+ mm = EnumerableSpecs::Numerous.new(a, b, c, d).minmax_by {|obj| obj.to_i }
+ mm[0].should equal(a)
+ mm[1].should equal(c)
+ end
+
+ it "uses min/max.<=>(current) to determine order" do
+ a, b, c = (1..3).map{|n| EnumerableSpecs::ReverseComparable.new(n)}
+
+ # Just using self here to avoid additional complexity
+ EnumerableSpecs::Numerous.new(a, b, c).minmax_by {|obj| obj }.should == [c, a]
+ end
+
+ it "is able to return the maximum for enums that contain nils" do
+ enum = EnumerableSpecs::Numerous.new(nil, nil, true)
+ enum.minmax_by {|o| o.nil? ? 0 : 1 }.should == [nil, true]
+ end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.minmax_by {|e| e.size}.should == [[1, 2], [6, 7, 8, 9]]
+ end
+
end
74 core/enumerable/minmax_spec.rb
View
@@ -2,50 +2,54 @@
require File.expand_path('../fixtures/classes', __FILE__)
describe "Enumerable#minmax" do
- ruby_version_is '1.8.7' do
- before :each do
- @enum = EnumerableSpecs::Numerous.new(6, 4, 5, 10, 8)
+ before :each do
+ @enum = EnumerableSpecs::Numerous.new(6, 4, 5, 10, 8)
- @strs = EnumerableSpecs::Numerous.new("333", "2", "60", "55555", "1010", "111")
- end
+ @strs = EnumerableSpecs::Numerous.new("333", "2", "60", "55555", "1010", "111")
+ end
- it "min should return the minimum element" do
- @enum.minmax.should == [4, 10]
- @strs.minmax.should == ["1010", "60" ]
- end
+ it "min should return the minimum element" do
+ @enum.minmax.should == [4, 10]
+ @strs.minmax.should == ["1010", "60" ]
+ end
- it "returns [nil, nil] for an empty Enumerable" do
- EnumerableSpecs::Empty.new.minmax.should == [nil, nil]
- end
+ it "returns [nil, nil] for an empty Enumerable" do
+ EnumerableSpecs::Empty.new.minmax.should == [nil, nil]
+ end
- it "raises an ArgumentError when elements are incomparable" do
- lambda do
- EnumerableSpecs::Numerous.new(11,"22").minmax
- end.should raise_error(ArgumentError)
+ it "raises an ArgumentError when elements are incomparable" do
+ lambda do
+ EnumerableSpecs::Numerous.new(11,"22").minmax
+ end.should raise_error(ArgumentError)
+ lambda do
+ EnumerableSpecs::Numerous.new(11,12,22,33).minmax{|a, b| nil}
+ end.should raise_error(ArgumentError)
+ end
+
+ ruby_version_is ""..."1.9" do
+ it "raises a NoMethodError for elements without #<=>" do
lambda do
- EnumerableSpecs::Numerous.new(11,12,22,33).minmax{|a, b| nil}
- end.should raise_error(ArgumentError)
+ EnumerableSpecs::Numerous.new(Object.new, Object.new).minmax
+ end.should raise_error(NoMethodError)
end
+ end
- ruby_version_is ""..."1.9" do
- it "raises a NoMethodError for elements without #<=>" do
- lambda do
- EnumerableSpecs::Numerous.new(Object.new, Object.new).minmax
- end.should raise_error(NoMethodError)
- end
+ ruby_version_is "1.9" do
+ it "raises a NoMethodError for elements without #<=>" do
+ lambda do
+ EnumerableSpecs::Numerous.new(BasicObject.new, BasicObject.new).minmax
+ end.should raise_error(NoMethodError)
end
+ end
- ruby_version_is "1.9" do
- it "raises a NoMethodError for elements without #<=>" do
- lambda do
- EnumerableSpecs::Numerous.new(BasicObject.new, BasicObject.new).minmax
- end.should raise_error(NoMethodError)
- end
- end
+ it "return the minimun when using a block rule" do
+ @enum.minmax {|a,b| b <=> a }.should == [10, 4]
+ @strs.minmax {|a,b| a.length <=> b.length }.should == ["2", "55555"]
+ end
- it "return the minimun when using a block rule" do
- @enum.minmax {|a,b| b <=> a }.should == [10, 4]
- @strs.minmax {|a,b| a.length <=> b.length }.should == ["2", "55555"]
- end
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.minmax.should == [[1, 2], [6, 7, 8, 9]]
end
+
end
36 core/enumerable/none_spec.rb
View
@@ -12,18 +12,9 @@
e.none?.should be_false
end
- ruby_version_is "" ... "1.9" do
- it "gathers whole arrays as elements when each yields multiple" do
- multi = EnumerableSpecs::YieldsMulti.new
- multi.none? {|e| e == 1 }.should be_true
- end
- end
-
- ruby_version_is "1.9" do
- it "gathers initial args as elements when each yields multiple" do
- multi = EnumerableSpecs::YieldsMulti.new
- multi.none? {|e| e == [1, 2] }.should be_true
- end
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMultiWithFalse.new
+ multi.none?.should be_false
end
end
@@ -51,4 +42,25 @@
it "returns false if the block ever returns true" do
@e.none? {|e| e == 3 ? true : false }.should be_false
end
+
+ ruby_version_is "" ... "1.9" do
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.none? {|e| e == 1 }.should be_true
+ end
+ end
+
+ ruby_version_is "1.9" do
+ it "gathers initial args as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.none? {|e| e == [1, 2] }.should be_true
+ end
+ end
+
+ it "yields multiple arguments when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ yielded = []
+ multi.none? {|e, i| yielded << [e, i] }
+ yielded.should == [[1, 2]]
+ end
end
14 core/enumerable/one_spec.rb
View
@@ -28,6 +28,14 @@
multi.one? {|e| e == 1 }.should be_true
end
end
+
+ it "yields multiple arguments when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ yielded = []
+ multi.one? {|e, i| yielded << [e, i] }
+ yielded.should == [[1, 2], [3, 4]]
+ end
+
end
describe "when not passed a block" do
@@ -42,5 +50,11 @@
it "returns false if all elements evaluate to false" do
[false, nil, false].one?.should be_false
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMultiWithSingleTrue.new
+ multi.one?.should be_false
+ end
+
end
end
6 core/enumerable/partition_spec.rb
View
@@ -17,4 +17,10 @@
EnumerableSpecs::Numerous.new.partition.should be_an_instance_of(enumerator_class)
end
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.partition {|e| e == [3, 4, 5] }.should == [[[3, 4, 5]], [[1, 2], [6, 7, 8, 9]]]
+ end
+
end
6 core/enumerable/reject_spec.rb
View
@@ -22,5 +22,11 @@
EnumerableSpecs::Numerous.new.reject.should be_an_instance_of(enumerator_class)
end
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.reject {|e| e == [3, 4, 5] }.should == [[1, 2], [6, 7, 8, 9]]
+ end
+
end
30 core/enumerable/reverse_each_spec.rb
View
@@ -1,19 +1,25 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
-ruby_version_is '1.8.7' do
- describe "Enumerable#reverse_each" do
+describe "Enumerable#reverse_each" do
- it "traverses enum in reverse order and pass each element to block" do
- a=[]
- EnumerableSpecs::Numerous.new.reverse_each { |i| a << i }
- a.should == [4, 1, 6, 3, 5, 2]
- end
+ it "traverses enum in reverse order and pass each element to block" do
+ a=[]
+ EnumerableSpecs::Numerous.new.reverse_each { |i| a << i }
+ a.should == [4, 1, 6, 3, 5, 2]
+ end
+
+ it 'returns an Enumerator if no block given' do
+ enum = EnumerableSpecs::Numerous.new.reverse_each
+ enum.should be_an_instance_of(enumerator_class)
+ enum.to_a.should == [4, 1, 6, 3, 5, 2]
+ end
- it 'returns an Enumerator if no block given' do
- enum = EnumerableSpecs::Numerous.new.reverse_each
- enum.should be_an_instance_of(enumerator_class)
- enum.to_a.should == [4, 1, 6, 3, 5, 2]
- end
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ yielded = []
+ multi.reverse_each {|e| yielded << e }
+ yielded.should == [[6, 7, 8, 9], [3, 4, 5], [1, 2]]
end
+
end
11 core/enumerable/shared/find_all.rb
View
@@ -22,9 +22,12 @@
end
end
- ruby_version_is "1.8.7" do
- it "returns an enumerator when no block given" do
- @numerous.send(@method).should be_an_instance_of(enumerator_class)
- end
+ it "returns an enumerator when no block given" do
+ @numerous.send(@method).should be_an_instance_of(enumerator_class)
+ end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.send(@method) {|e| e == [3, 4, 5] }.should == [[3, 4, 5]]
end
end
6 core/enumerable/shared/include.rb
View
@@ -25,4 +25,10 @@ class EnumerableSpecIncludeP11; def ==(obj); obj == '11'; end; end
EnumerableSpecs::Numerous.new(2,4,[6,8],10).send(@method, [6, 8]).should == true
EnumerableSpecs::Numerous.new(2,4,[6,8],10).send(@method, [6.0, 8.0]).should == true
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.include?([1,2]).should be_true
+ end
+
end
15 core/enumerable/sort_by_spec.rb
View
@@ -15,11 +15,14 @@
ar.should == [b, a]
end
- ruby_version_is "1.8.7" do
- it "returns an Enumerator when a block is not supplied" do
- a = EnumerableSpecs::Numerous.new("a","b")
- a.sort_by.should be_an_instance_of(enumerator_class)
- a.to_a.should == ["a", "b"]
- end
+ it "returns an Enumerator when a block is not supplied" do
+ a = EnumerableSpecs::Numerous.new("a","b")
+ a.sort_by.should be_an_instance_of(enumerator_class)
+ a.to_a.should == ["a", "b"]
+ end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.sort_by {|e| e.size}.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
end
end
6 core/enumerable/sort_spec.rb
View
@@ -52,4 +52,10 @@
a=EnumerableSpecs::Numerous.new(EnumerableSpecs::Uncomparable.new, EnumerableSpecs::Uncomparable.new)
lambda {a.sort}.should raise_error(ArgumentError)
end
+
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.sort {|a, b| a.first <=> b.first}.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
+ end
+
end
9 core/enumerable/zip_spec.rb
View
@@ -34,8 +34,8 @@
EnumerableSpecs::Numerous.new(1,2,3).zip(convertable).should == [[1,4],[2,5],[3,6]]
convertable.called.should == :to_ary
end
-
- it "converts arguments to arrays using #to_ary" do
+
+ it "converts arguments to enums using #to_enum" do
convertable = EnumerableSpecs::EnumConvertable.new(4..6)
EnumerableSpecs::Numerous.new(1,2,3).zip(convertable).should == [[1,4],[2,5],[3,6]]
convertable.called.should == :to_enum
@@ -43,5 +43,10 @@
end
end
+ it "gathers whole arrays as elements when each yields multiple" do
+ multi = EnumerableSpecs::YieldsMulti.new
+ multi.zip(multi).should == [[[1, 2], [1, 2]], [[3, 4, 5], [3, 4, 5]], [[6, 7, 8, 9], [6, 7, 8, 9]]]
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.