Skip to content

Commit ddef3c0

Browse files
peterzhu2118eregon
authored andcommitted
Add shared spec integer_ceil_precision
This spec can be shared between Integer#ceil and Float#ceil and improves coverage of cases where precision is passed in.
1 parent ce08f14 commit ddef3c0

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

core/float/ceil_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
require_relative '../../spec_helper'
2+
require_relative '../integer/shared/integer_ceil_precision'
23

34
describe "Float#ceil" do
5+
context "with precision" do
6+
it_behaves_like :integer_ceil_precision, :Float
7+
end
8+
49
it "returns the smallest Integer greater than or equal to self" do
510
-1.2.ceil.should eql( -1)
611
-1.0.ceil.should eql( -1)

core/integer/ceil_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
require_relative '../../spec_helper'
22
require_relative 'shared/to_i'
33
require_relative 'shared/integer_rounding'
4+
require_relative 'shared/integer_ceil_precision'
45

56
describe "Integer#ceil" do
67
it_behaves_like :integer_to_i, :ceil
78
it_behaves_like :integer_rounding_positive_precision, :ceil
89

10+
context "with precision" do
11+
it_behaves_like :integer_ceil_precision, :Integer
12+
end
13+
914
context "precision argument specified as part of the ceil method is negative" do
1015
it "returns the smallest integer greater than self with at least precision.abs trailing zeros" do
1116
18.ceil(-1).should eql(20)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
describe :integer_ceil_precision, shared: true do
2+
context "precision is zero" do
3+
it "returns integer self" do
4+
send(@method, 0).ceil(0).should.eql?(0)
5+
send(@method, 123).ceil(0).should.eql?(123)
6+
send(@method, -123).ceil(0).should.eql?(-123)
7+
end
8+
end
9+
10+
context "precision is positive" do
11+
it "returns self" do
12+
send(@method, 0).ceil(1).should.eql?(send(@method, 0))
13+
send(@method, 0).ceil(10).should.eql?(send(@method, 0))
14+
15+
send(@method, 123).ceil(10).should.eql?(send(@method, 123))
16+
send(@method, -123).ceil(10).should.eql?(send(@method, -123))
17+
end
18+
end
19+
20+
context "precision is negative" do
21+
it "always returns 0 when self is 0" do
22+
send(@method, 0).ceil(-1).should.eql?(0)
23+
send(@method, 0).ceil(-10).should.eql?(0)
24+
end
25+
26+
it "returns largest integer less than self with at least precision.abs trailing zeros" do
27+
send(@method, 123).ceil(-1).should.eql?(130)
28+
send(@method, 123).ceil(-2).should.eql?(200)
29+
send(@method, 123).ceil(-3).should.eql?(1000)
30+
31+
send(@method, -123).ceil(-1).should.eql?(-120)
32+
send(@method, -123).ceil(-2).should.eql?(-100)
33+
send(@method, -123).ceil(-3).should.eql?(0)
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)