Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #195 from kachick/improve-shared-enumerator-with_o…

…bject

Improve specs for Enumerator#{each_}with_object
  • Loading branch information...
commit 80ff3153ff3bbe1ce98936f05e716f239fe857ef 2 parents 59c9466 + fe773b8
@brixen brixen authored
Showing with 33 additions and 10 deletions.
  1. +33 −10 shared/enumerator/with_object.rb
View
43 shared/enumerator/with_object.rb
@@ -1,19 +1,42 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe :enum_with_object, :shared => true do
- it "returns an enumerator when not given a block" do
- [].to_enum.send(@method, '').should be_an_instance_of(enumerator_class)
+ before :each do
+ @enum = [:a, :b].to_enum
+ @memo = ''
+ @block_params = @enum.send(@method, @memo).to_a
end
- it "returns the given object when given a block" do
- object = [].to_enum.send(@method, 'wadus') {|i, o| o = o + o}
- object.should == 'wadus'
+ it "receives an argument" do
+ @enum.method(@method).arity.should == 1
end
- it "iterates over the array adding the given object" do
- expected = ''
- %w|wadus wadus|.to_enum.send(@method, ' ') {|e, o| expected += e + o}
+ context "with block" do
+ it "returns the given object" do
+ ret = @enum.send(@method, @memo) do |elm, memo|
+ # nothing
+ end
+ ret.should equal(@memo)
+ end
- expected.should == 'wadus wadus '
+ context "the block parameter" do
+ it "passes each element to first parameter" do
+ @block_params[0][0].should equal(:a)
+ @block_params[1][0].should equal(:b)
+ end
+
+ it "passes the given object to last parameter" do
+ @block_params[0][1].should equal(@memo)
+ @block_params[1][1].should equal(@memo)
+ end
+ end
+ end
+
+ context "without block" do
+ it "returns new Enumerator" do
+ ret = @enum.send(@method, @memo)
+ ret.should be_an_instance_of(enumerator_class)
+ ret.should_not equal(@enum)
+ end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.