Skip to content

Commit

Permalink
Specs for Range#{count, each, equal_value, first, inspect, max, min, …
Browse files Browse the repository at this point in the history
…size}
  • Loading branch information
Lillian Zhang authored and Nicolas Laurent committed Nov 30, 2020
1 parent 6fd5262 commit 8c67f4a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 4 deletions.
20 changes: 20 additions & 0 deletions spec/ruby/core/range/count_spec.rb
@@ -0,0 +1,20 @@
require_relative '../../spec_helper'

describe "Range#count" do
ruby_version_is "2.6" do
it "returns Infinity for endless ranges without arguments or blocks" do
inf = Float::INFINITY
eval("('a'...)").count.should == inf
eval("(7..)").count.should == inf
end
end

ruby_version_is "2.7" do
it "returns Infinity for beginless ranges without arguments or blocks" do
inf = Float::INFINITY
eval("(...'a')").count.should == inf
eval("(...nil)").count.should == inf
eval("(..10.0)").count.should == inf
end
end
end
6 changes: 6 additions & 0 deletions spec/ruby/core/range/each_spec.rb
Expand Up @@ -54,6 +54,12 @@
end
end

ruby_version_is "2.7" do
it "raises a TypeError beginless ranges" do
-> { eval("(..2)").each { |x| x } }.should raise_error(TypeError)
end
end

it "raises a TypeError if the first element does not respond to #succ" do
-> { (0.5..2.4).each { |i| i } }.should raise_error(TypeError)

Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/range/equal_value_spec.rb
Expand Up @@ -13,4 +13,10 @@
eval("(1.0..)").should == eval("(1.0..)")
end
end

ruby_version_is "2.7" do
it "returns true if the endpoints are == for beginless ranges" do
eval("(...10)").should == eval("(...10)")
end
end
end
6 changes: 6 additions & 0 deletions spec/ruby/core/range/first_spec.rb
Expand Up @@ -46,4 +46,10 @@
it "raises a TypeError when passed a String" do
-> { (2..3).first("1") }.should raise_error(TypeError)
end

ruby_version_is "2.7" do
it "raises a RangeError when called on an beginless range" do
-> { eval("(..1)").first }.should raise_error(RangeError)
end
end
end
12 changes: 12 additions & 0 deletions spec/ruby/core/range/inspect_spec.rb
Expand Up @@ -19,6 +19,18 @@
end
end

ruby_version_is '2.7' do
it "works for beginless ranges" do
eval("(..1)").inspect.should == "..1"
eval("(...0.1)").inspect.should == "...0.1"
end

it "works for nil ... nil ranges" do
eval("(..nil)").inspect.should == "nil..nil"
eval("(nil...)").inspect.should == "nil...nil"
end
end

ruby_version_is ''...'2.7' do
it "returns a tainted string if either end is tainted" do
(("a".taint)..."c").inspect.tainted?.should be_true
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/core/range/max_spec.rb
Expand Up @@ -51,6 +51,14 @@
-> { eval("(1..)").max }.should raise_error(RangeError)
end
end

ruby_version_is "2.7.2" do
it "returns the end point (or one less) for beginless ranges" do
eval("(...1)").max.should == 0
eval("(..1)").max.should == 1
eval("(..1.0)").max.should == 1.0
end
end
end

describe "Range#max given a block" do
Expand Down Expand Up @@ -85,4 +93,10 @@
('z'..'l').max {|x,y| x <=> y}.should be_nil
(5...5).max {|x,y| x <=> y}.should be_nil
end

ruby_version_is "2.7" do
it "raises RangeError when called with custom comparison method on an beginless range" do
-> { eval("(..1)").max {|a, b| a} }.should raise_error(RangeError)
end
end
end
18 changes: 15 additions & 3 deletions spec/ruby/core/range/min_spec.rb
Expand Up @@ -38,6 +38,19 @@
time_end = Time.now + 1.0
(time_start...time_end).min.should equal(time_start)
end

ruby_version_is "2.6" do
it "returns the start point for endless ranges" do
eval("(1..)").min.should == 1
eval("(1.0...)").min.should == 1.0
end
end

ruby_version_is "2.7" do
it "raises RangeError when called on an beginless range" do
-> { eval("(..1)").min }.should raise_error(RangeError)
end
end
end

describe "Range#min given a block" do
Expand Down Expand Up @@ -74,9 +87,8 @@
end

ruby_version_is "2.6" do
it "returns the start point for endless ranges" do
eval("(1..)").min.should == 1
eval("(1.0...)").min.should == 1.0
it "raises RangeError when called with custom comparison method on an endless range" do
-> { eval("(1..)").min {|a, b| a} }.should raise_error(RangeError)
end
end
end
16 changes: 15 additions & 1 deletion spec/ruby/core/range/size_spec.rb
Expand Up @@ -25,10 +25,24 @@
end

ruby_version_is "2.6" do
it 'returns Float::INFINITY for endless ranges' do
it 'returns Float::INFINITY for endless ranges if the start is numeric' do
eval("(1..)").size.should == Float::INFINITY
eval("(0.5...)").size.should == Float::INFINITY
end

it 'returns nil for endless ranges if the start is not numeric' do
eval("('z'..)").size.should == nil
eval("([]...)").size.should == nil
end
end

ruby_version_is "2.7" do
it 'returns Float::INFINITY for all beginless ranges' do
eval("(..1)").size.should == Float::INFINITY
eval("(...0.5)").size.should == Float::INFINITY
eval("(..nil)").size.should == Float::INFINITY
eval("(...'o')").size.should == Float::INFINITY
end
end

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

0 comments on commit 8c67f4a

Please sign in to comment.