Skip to content

Commit 8c67f4a

Browse files
Lillian ZhangNicolas Laurent
authored andcommitted
Specs for Range#{count, each, equal_value, first, inspect, max, min, size}
1 parent 6fd5262 commit 8c67f4a

File tree

8 files changed

+94
-4
lines changed

8 files changed

+94
-4
lines changed

spec/ruby/core/range/count_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require_relative '../../spec_helper'
2+
3+
describe "Range#count" do
4+
ruby_version_is "2.6" do
5+
it "returns Infinity for endless ranges without arguments or blocks" do
6+
inf = Float::INFINITY
7+
eval("('a'...)").count.should == inf
8+
eval("(7..)").count.should == inf
9+
end
10+
end
11+
12+
ruby_version_is "2.7" do
13+
it "returns Infinity for beginless ranges without arguments or blocks" do
14+
inf = Float::INFINITY
15+
eval("(...'a')").count.should == inf
16+
eval("(...nil)").count.should == inf
17+
eval("(..10.0)").count.should == inf
18+
end
19+
end
20+
end

spec/ruby/core/range/each_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
end
5555
end
5656

57+
ruby_version_is "2.7" do
58+
it "raises a TypeError beginless ranges" do
59+
-> { eval("(..2)").each { |x| x } }.should raise_error(TypeError)
60+
end
61+
end
62+
5763
it "raises a TypeError if the first element does not respond to #succ" do
5864
-> { (0.5..2.4).each { |i| i } }.should raise_error(TypeError)
5965

spec/ruby/core/range/equal_value_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
eval("(1.0..)").should == eval("(1.0..)")
1414
end
1515
end
16+
17+
ruby_version_is "2.7" do
18+
it "returns true if the endpoints are == for beginless ranges" do
19+
eval("(...10)").should == eval("(...10)")
20+
end
21+
end
1622
end

spec/ruby/core/range/first_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@
4646
it "raises a TypeError when passed a String" do
4747
-> { (2..3).first("1") }.should raise_error(TypeError)
4848
end
49+
50+
ruby_version_is "2.7" do
51+
it "raises a RangeError when called on an beginless range" do
52+
-> { eval("(..1)").first }.should raise_error(RangeError)
53+
end
54+
end
4955
end

spec/ruby/core/range/inspect_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
end
2020
end
2121

22+
ruby_version_is '2.7' do
23+
it "works for beginless ranges" do
24+
eval("(..1)").inspect.should == "..1"
25+
eval("(...0.1)").inspect.should == "...0.1"
26+
end
27+
28+
it "works for nil ... nil ranges" do
29+
eval("(..nil)").inspect.should == "nil..nil"
30+
eval("(nil...)").inspect.should == "nil...nil"
31+
end
32+
end
33+
2234
ruby_version_is ''...'2.7' do
2335
it "returns a tainted string if either end is tainted" do
2436
(("a".taint)..."c").inspect.tainted?.should be_true

spec/ruby/core/range/max_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
-> { eval("(1..)").max }.should raise_error(RangeError)
5252
end
5353
end
54+
55+
ruby_version_is "2.7.2" do
56+
it "returns the end point (or one less) for beginless ranges" do
57+
eval("(...1)").max.should == 0
58+
eval("(..1)").max.should == 1
59+
eval("(..1.0)").max.should == 1.0
60+
end
61+
end
5462
end
5563

5664
describe "Range#max given a block" do
@@ -85,4 +93,10 @@
8593
('z'..'l').max {|x,y| x <=> y}.should be_nil
8694
(5...5).max {|x,y| x <=> y}.should be_nil
8795
end
96+
97+
ruby_version_is "2.7" do
98+
it "raises RangeError when called with custom comparison method on an beginless range" do
99+
-> { eval("(..1)").max {|a, b| a} }.should raise_error(RangeError)
100+
end
101+
end
88102
end

spec/ruby/core/range/min_spec.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@
3838
time_end = Time.now + 1.0
3939
(time_start...time_end).min.should equal(time_start)
4040
end
41+
42+
ruby_version_is "2.6" do
43+
it "returns the start point for endless ranges" do
44+
eval("(1..)").min.should == 1
45+
eval("(1.0...)").min.should == 1.0
46+
end
47+
end
48+
49+
ruby_version_is "2.7" do
50+
it "raises RangeError when called on an beginless range" do
51+
-> { eval("(..1)").min }.should raise_error(RangeError)
52+
end
53+
end
4154
end
4255

4356
describe "Range#min given a block" do
@@ -74,9 +87,8 @@
7487
end
7588

7689
ruby_version_is "2.6" do
77-
it "returns the start point for endless ranges" do
78-
eval("(1..)").min.should == 1
79-
eval("(1.0...)").min.should == 1.0
90+
it "raises RangeError when called with custom comparison method on an endless range" do
91+
-> { eval("(1..)").min {|a, b| a} }.should raise_error(RangeError)
8092
end
8193
end
8294
end

spec/ruby/core/range/size_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,24 @@
2525
end
2626

2727
ruby_version_is "2.6" do
28-
it 'returns Float::INFINITY for endless ranges' do
28+
it 'returns Float::INFINITY for endless ranges if the start is numeric' do
2929
eval("(1..)").size.should == Float::INFINITY
3030
eval("(0.5...)").size.should == Float::INFINITY
3131
end
32+
33+
it 'returns nil for endless ranges if the start is not numeric' do
34+
eval("('z'..)").size.should == nil
35+
eval("([]...)").size.should == nil
36+
end
37+
end
38+
39+
ruby_version_is "2.7" do
40+
it 'returns Float::INFINITY for all beginless ranges' do
41+
eval("(..1)").size.should == Float::INFINITY
42+
eval("(...0.5)").size.should == Float::INFINITY
43+
eval("(..nil)").size.should == Float::INFINITY
44+
eval("(...'o')").size.should == Float::INFINITY
45+
end
3246
end
3347

3448
it "returns nil if first and last are not Numeric" do

0 commit comments

Comments
 (0)