diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/range/each_spec.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
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
Diffstat (limited to 'spec/ruby/core/range/each_spec.rb')
-rw-r--r-- | spec/ruby/core/range/each_spec.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/spec/ruby/core/range/each_spec.rb b/spec/ruby/core/range/each_spec.rb new file mode 100644 index 0000000000..4520f3cde6 --- /dev/null +++ b/spec/ruby/core/range/each_spec.rb @@ -0,0 +1,66 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +describe "Range#each" do + it "passes each element to the given block by using #succ" do + a = [] + (-5..5).each { |i| a << i } + a.should == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] + + a = [] + ('A'..'D').each { |i| a << i } + a.should == ['A','B','C','D'] + + a = [] + ('A'...'D').each { |i| a << i } + a.should == ['A','B','C'] + + a = [] + (0xfffd...0xffff).each { |i| a << i } + a.should == [0xfffd, 0xfffe] + + y = mock('y') + x = mock('x') + x.should_receive(:<=>).with(y).any_number_of_times.and_return(-1) + x.should_receive(:<=>).with(x).any_number_of_times.and_return(0) + x.should_receive(:succ).any_number_of_times.and_return(y) + y.should_receive(:<=>).with(x).any_number_of_times.and_return(1) + y.should_receive(:<=>).with(y).any_number_of_times.and_return(0) + + a = [] + (x..y).each { |i| a << i } + a.should == [x, y] + end + + it "raises a TypeError if the first element does not respond to #succ" do + lambda { (0.5..2.4).each { |i| i } }.should raise_error(TypeError) + + b = mock('x') + (a = mock('1')).should_receive(:<=>).with(b).and_return(1) + + lambda { (a..b).each { |i| i } }.should raise_error(TypeError) + end + + it "returns self" do + range = 1..10 + range.each{}.should equal(range) + end + + it "returns an enumerator when no block given" do + enum = (1..3).each + enum.should be_an_instance_of(Enumerator) + enum.to_a.should == [1, 2, 3] + end + + it "raises a TypeError if the first element is a Time object" do + t = Time.now + lambda { (t..t+1).each { |i| i } }.should raise_error(TypeError) + end + + it "passes each Symbol element by using #succ" do + (:aa..:ac).each.to_a.should == [:aa, :ab, :ac] + (:aa...:ac).each.to_a.should == [:aa, :ab] + end + + it_behaves_like :enumeratorized_with_origin_size, :each, (1..3) +end |