-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GR-59866] Add Range#reverse_each method
PullRequest: truffleruby/4413
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
require_relative '../../spec_helper' | ||
|
||
ruby_version_is "3.3" do | ||
describe "Range#reverse_each" do | ||
it "traverses the Range in reverse order and pass each element to block" do | ||
a = [] | ||
(1..3).reverse_each { |i| a << i } | ||
a.should == [3, 2, 1] | ||
|
||
a = [] | ||
(1...3).reverse_each { |i| a << i } | ||
a.should == [2, 1] | ||
end | ||
|
||
it "returns self" do | ||
r = (1..3) | ||
r.reverse_each { |x| }.should equal(r) | ||
end | ||
|
||
it "returns an Enumerator if no block given" do | ||
enum = (1..3).reverse_each | ||
enum.should be_an_instance_of(Enumerator) | ||
enum.to_a.should == [3, 2, 1] | ||
end | ||
|
||
it "raises a TypeError for endless Ranges of Integers" do | ||
-> { | ||
(1..).reverse_each.take(3) | ||
}.should raise_error(TypeError, "can't iterate from NilClass") | ||
end | ||
|
||
it "raises a TypeError for endless Ranges of non-Integers" do | ||
-> { | ||
("a"..).reverse_each.take(3) | ||
}.should raise_error(TypeError, "can't iterate from NilClass") | ||
end | ||
|
||
context "Integer boundaries" do | ||
it "supports beginningless Ranges" do | ||
(..5).reverse_each.take(3).should == [5, 4, 3] | ||
end | ||
end | ||
|
||
context "non-Integer boundaries" do | ||
it "uses #succ to iterate a Range of non-Integer elements" do | ||
y = mock('y') | ||
x = mock('x') | ||
|
||
x.should_receive(:succ).any_number_of_times.and_return(y) | ||
x.should_receive(:<=>).with(y).any_number_of_times.and_return(-1) | ||
x.should_receive(:<=>).with(x).any_number_of_times.and_return(0) | ||
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 "uses #succ to iterate a Range of Strings" do | ||
a = [] | ||
('A'..'D').reverse_each { |i| a << i } | ||
a.should == ['D','C','B','A'] | ||
end | ||
|
||
it "uses #succ to iterate a Range of Symbols" do | ||
a = [] | ||
(:A..:D).reverse_each { |i| a << i } | ||
a.should == [:D, :C, :B, :A] | ||
end | ||
|
||
it "raises a TypeError when `begin` value does not respond to #succ" do | ||
-> { (Time.now..Time.now).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Time/) | ||
-> { (//..//).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Regexp/) | ||
-> { ([]..[]).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Array/) | ||
end | ||
|
||
it "does not support beginningless Ranges" do | ||
-> { | ||
(..'a').reverse_each { |x| x } | ||
}.should raise_error(TypeError, /can't iterate from NilClass/) | ||
end | ||
end | ||
|
||
context "when no block is given" do | ||
describe "returned Enumerator size" do | ||
it "returns the Range size when Range size is finite" do | ||
(1..3).reverse_each.size.should == 3 | ||
end | ||
|
||
it "returns Infinity when Range size is infinite" do | ||
(..3).reverse_each.size.should == Float::INFINITY | ||
end | ||
|
||
it "returns nil when Range size is unknown" do | ||
('a'..'z').reverse_each.size.should == nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters