diff options
Diffstat (limited to 'spec/ruby/core/enumerable/shared')
| -rw-r--r-- | spec/ruby/core/enumerable/shared/collect.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/enumerable/shared/collect_concat.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/enumerable/shared/find.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/enumerable/shared/find_all.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/enumerable/shared/include.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/enumerable/shared/inject.rb | 47 | ||||
| -rw-r--r-- | spec/ruby/core/enumerable/shared/take.rb | 8 | ||||
| -rw-r--r-- | spec/ruby/core/enumerable/shared/value_packing.rb | 26 |
8 files changed, 77 insertions, 18 deletions
diff --git a/spec/ruby/core/enumerable/shared/collect.rb b/spec/ruby/core/enumerable/shared/collect.rb index 6df1a616eb..4696d32454 100644 --- a/spec/ruby/core/enumerable/shared/collect.rb +++ b/spec/ruby/core/enumerable/shared/collect.rb @@ -30,7 +30,7 @@ describe :enumerable_collect, shared: true do it "returns an enumerator when no block given" do enum = EnumerableSpecs::Numerous.new.send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each { |i| -i }.should == [-2, -5, -3, -6, -1, -4] end @@ -86,7 +86,7 @@ describe :enumerable_collect, shared: true do -> do { 1 => 'a', 2 => 'b' }.map(&m) - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "calls the each method on sub-classes" do diff --git a/spec/ruby/core/enumerable/shared/collect_concat.rb b/spec/ruby/core/enumerable/shared/collect_concat.rb index ddd431baeb..1694e3fdce 100644 --- a/spec/ruby/core/enumerable/shared/collect_concat.rb +++ b/spec/ruby/core/enumerable/shared/collect_concat.rb @@ -41,12 +41,12 @@ describe :enumerable_collect_concat, shared: true do obj = mock("to_ary defined") obj.should_receive(:to_ary).and_return("array") - -> { [1, obj, 3].send(@method) { |i| i } }.should raise_error(TypeError) + -> { [1, obj, 3].send(@method) { |i| i } }.should.raise(TypeError) end it "returns an enumerator when no block given" do enum = EnumerableSpecs::Numerous.new(1, 2).send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each{ |i| [i] * i }.should == [1, 2, 2] end diff --git a/spec/ruby/core/enumerable/shared/find.rb b/spec/ruby/core/enumerable/shared/find.rb index 61d63ba3d5..cdff640415 100644 --- a/spec/ruby/core/enumerable/shared/find.rb +++ b/spec/ruby/core/enumerable/shared/find.rb @@ -59,7 +59,7 @@ describe :enumerable_find, shared: true do end it "returns an enumerator when no block given" do - @numerous.send(@method).should be_an_instance_of(Enumerator) + @numerous.send(@method).should.instance_of?(Enumerator) end it "passes the ifnone proc to the enumerator" do diff --git a/spec/ruby/core/enumerable/shared/find_all.rb b/spec/ruby/core/enumerable/shared/find_all.rb index 1bbe71f372..27f01de6e0 100644 --- a/spec/ruby/core/enumerable/shared/find_all.rb +++ b/spec/ruby/core/enumerable/shared/find_all.rb @@ -14,7 +14,7 @@ describe :enumerable_find_all, shared: true do end it "returns an enumerator when no block given" do - @numerous.send(@method).should be_an_instance_of(Enumerator) + @numerous.send(@method).should.instance_of?(Enumerator) end it "passes through the values yielded by #each_with_index" do diff --git a/spec/ruby/core/enumerable/shared/include.rb b/spec/ruby/core/enumerable/shared/include.rb index 569f350fd5..ea250f032b 100644 --- a/spec/ruby/core/enumerable/shared/include.rb +++ b/spec/ruby/core/enumerable/shared/include.rb @@ -28,7 +28,7 @@ describe :enumerable_include, shared: true do it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method, [1,2]).should be_true + multi.send(@method, [1,2]).should == true end end diff --git a/spec/ruby/core/enumerable/shared/inject.rb b/spec/ruby/core/enumerable/shared/inject.rb index 693d34d675..7da4f0ca99 100644 --- a/spec/ruby/core/enumerable/shared/inject.rb +++ b/spec/ruby/core/enumerable/shared/inject.rb @@ -16,6 +16,23 @@ describe :enumerable_inject, shared: true do it "can take two argument" do EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, :-).should == 4 + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, "-").should == 4 + + [1, 2, 3].send(@method, 10, :-).should == 4 + [1, 2, 3].send(@method, 10, "-").should == 4 + end + + it "converts non-Symbol method name argument to String with #to_str if two arguments" do + name = Object.new + def name.to_str; "-"; end + + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, name).should == 4 + [1, 2, 3].send(@method, 10, name).should == 4 + end + + it "raises TypeError when the second argument is not Symbol or String and it cannot be converted to String if two arguments" do + -> { EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [1, 2, 3].send(@method, 10, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) end it "ignores the block if two arguments" do @@ -39,6 +56,25 @@ describe :enumerable_inject, shared: true do it "can take a symbol argument" do EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, :-).should == 4 + [10, 1, 2, 3].send(@method, :-).should == 4 + end + + it "can take a String argument" do + EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, "-").should == 4 + [10, 1, 2, 3].send(@method, "-").should == 4 + end + + it "converts non-Symbol method name argument to String with #to_str" do + name = Object.new + def name.to_str; "-"; end + + EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, name).should == 4 + [10, 1, 2, 3].send(@method, name).should == 4 + end + + it "raises TypeError when passed not Symbol or String method name argument and it cannot be converted to String" do + -> { EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [10, 1, 2, 3].send(@method, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) end it "without argument takes a block with an accumulator (with first element as initial value) and the current element. Value of block becomes new accumulator" do @@ -67,7 +103,7 @@ describe :enumerable_inject, shared: true do it "without inject arguments(legacy rubycon)" do # no inject argument - EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| 999 } .should == 2 + EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| 999 }.should == 2 EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| acc }.should == 2 EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| x }.should == 2 @@ -77,7 +113,6 @@ describe :enumerable_inject, shared: true do EnumerableSpecs::EachDefiner.new('a','b','c').send(@method) {|result, i| i+result}.should == "cba" EnumerableSpecs::EachDefiner.new(3, 4, 5).send(@method) {|result, i| result*i}.should == 60 EnumerableSpecs::EachDefiner.new([1], 2, 'a','b').send(@method){|r,i| r<<i}.should == [1, 2, 'a', 'b'] - end it "returns nil when fails(legacy rubycon)" do @@ -100,10 +135,8 @@ describe :enumerable_inject, shared: true do actual.sort_by(&:to_s).should == expected.sort_by(&:to_s) end - ruby_bug '#18635', ''...'3.2' do - it "raises an ArgumentError when no parameters or block is given" do - -> { [1,2].send(@method) }.should raise_error(ArgumentError) - -> { {one: 1, two: 2}.send(@method) }.should raise_error(ArgumentError) - end + it "raises an ArgumentError when no parameters or block is given" do + -> { [1,2].send(@method) }.should.raise(ArgumentError) + -> { {one: 1, two: 2}.send(@method) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/shared/take.rb b/spec/ruby/core/enumerable/shared/take.rb index ce2ace20fa..a6da06325f 100644 --- a/spec/ruby/core/enumerable/shared/take.rb +++ b/spec/ruby/core/enumerable/shared/take.rb @@ -25,7 +25,7 @@ describe :enumerable_take, shared: true do end it "raises an ArgumentError when count is negative" do - -> { @enum.send(@method, -1) }.should raise_error(ArgumentError) + -> { @enum.send(@method, -1) }.should.raise(ArgumentError) end it "returns the entire array when count > length" do @@ -40,11 +40,11 @@ describe :enumerable_take, shared: true do end it "raises a TypeError if the passed argument is not numeric" do - -> { @enum.send(@method, nil) }.should raise_error(TypeError) - -> { @enum.send(@method, "a") }.should raise_error(TypeError) + -> { @enum.send(@method, nil) }.should.raise(TypeError) + -> { @enum.send(@method, "a") }.should.raise(TypeError) obj = mock("nonnumeric") - -> { @enum.send(@method, obj) }.should raise_error(TypeError) + -> { @enum.send(@method, obj) }.should.raise(TypeError) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/shared/value_packing.rb b/spec/ruby/core/enumerable/shared/value_packing.rb new file mode 100644 index 0000000000..ff77f45cdf --- /dev/null +++ b/spec/ruby/core/enumerable/shared/value_packing.rb @@ -0,0 +1,26 @@ +# This is the behavior of rb_enum_values_pack() in CRuby +describe :enumerable_value_packing, shared: true do + # @take must be set to a Proc that returns the take-result whose #each + # yields packed values (e.g. -> e { e.take(1) } or -> e { e.lazy.take(1) }). + + it "yields a single nil for a zero-argument source yield" do + e = Enumerator.new { |y| y.yield } + args = nil + @take.call(e).each { |*a| args = a } + args.should == [nil] + end + + it "yields the value for a single-argument source yield" do + e = Enumerator.new { |y| y.yield :v } + args = nil + @take.call(e).each { |*a| args = a } + args.should == [:v] + end + + it "yields a packed Array for a multi-argument source yield" do + e = Enumerator.new { |y| y.yield 1, 2 } + args = nil + @take.call(e).each { |*a| args = a } + args.should == [[1, 2]] + end +end |
