Skip to content

Commit 34ce2eb

Browse files
herwinweregon
authored andcommitted
Remove shared/rational folder, move specs inline
This has become obsolete in 2015 when Rational was imported into core Reference: commit 3ceb9ae
1 parent 1ada0bc commit 34ce2eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1090
-1299
lines changed

core/kernel/Rational_spec.rb

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,150 @@
11
require_relative '../../spec_helper'
2-
require_relative '../../shared/rational/Rational'
2+
require_relative '../rational/fixtures/rational'
33

44
describe "Kernel.Rational" do
5-
it_behaves_like :kernel_Rational, :Rational
5+
describe "passed Integer" do
6+
# Guard against the Mathn library
7+
guard -> { !defined?(Math.rsqrt) } do
8+
it "returns a new Rational number with 1 as the denominator" do
9+
Rational(1).should eql(Rational(1, 1))
10+
Rational(-3).should eql(Rational(-3, 1))
11+
Rational(bignum_value).should eql(Rational(bignum_value, 1))
12+
end
13+
end
14+
end
15+
16+
describe "passed two integers" do
17+
it "returns a new Rational number" do
18+
rat = Rational(1, 2)
19+
rat.numerator.should == 1
20+
rat.denominator.should == 2
21+
rat.should be_an_instance_of(Rational)
22+
23+
rat = Rational(-3, -5)
24+
rat.numerator.should == 3
25+
rat.denominator.should == 5
26+
rat.should be_an_instance_of(Rational)
27+
28+
rat = Rational(bignum_value, 3)
29+
rat.numerator.should == bignum_value
30+
rat.denominator.should == 3
31+
rat.should be_an_instance_of(Rational)
32+
end
33+
34+
it "reduces the Rational" do
35+
rat = Rational(2, 4)
36+
rat.numerator.should == 1
37+
rat.denominator.should == 2
38+
39+
rat = Rational(3, 9)
40+
rat.numerator.should == 1
41+
rat.denominator.should == 3
42+
end
43+
end
44+
45+
describe "when passed a String" do
46+
it "converts the String to a Rational using the same method as String#to_r" do
47+
r = Rational(13, 25)
48+
s_r = ".52".to_r
49+
r_s = Rational(".52")
50+
51+
r_s.should == r
52+
r_s.should == s_r
53+
end
54+
55+
it "scales the Rational value of the first argument by the Rational value of the second" do
56+
Rational(".52", ".6").should == Rational(13, 15)
57+
Rational(".52", "1.6").should == Rational(13, 40)
58+
end
59+
60+
it "does not use the same method as Float#to_r" do
61+
r = Rational(3, 5)
62+
f_r = 0.6.to_r
63+
r_s = Rational("0.6")
64+
65+
r_s.should == r
66+
r_s.should_not == f_r
67+
end
68+
end
69+
70+
describe "when passed a Numeric" do
71+
it "calls #to_r to convert the first argument to a Rational" do
72+
num = RationalSpecs::SubNumeric.new(2)
73+
74+
Rational(num).should == Rational(2)
75+
end
76+
end
77+
78+
describe "when passed a Complex" do
79+
it "returns a Rational from the real part if the imaginary part is 0" do
80+
Rational(Complex(1, 0)).should == Rational(1)
81+
end
82+
83+
it "raises a RangeError if the imaginary part is not 0" do
84+
-> { Rational(Complex(1, 2)) }.should raise_error(RangeError)
85+
end
86+
end
87+
88+
it "raises a ZeroDivisionError if the second argument is 0" do
89+
-> { Rational(1, 0) }.should raise_error(ZeroDivisionError, "divided by 0")
90+
-> { Rational(1, 0.0) }.should raise_error(ZeroDivisionError, "divided by 0")
91+
end
92+
93+
it "raises a TypeError if the first argument is nil" do
94+
-> { Rational(nil) }.should raise_error(TypeError)
95+
end
96+
97+
it "raises a TypeError if the second argument is nil" do
98+
-> { Rational(1, nil) }.should raise_error(TypeError)
99+
end
100+
101+
it "raises a TypeError if the first argument is a Symbol" do
102+
-> { Rational(:sym) }.should raise_error(TypeError)
103+
end
104+
105+
it "raises a TypeError if the second argument is a Symbol" do
106+
-> { Rational(1, :sym) }.should raise_error(TypeError)
107+
end
108+
109+
describe "when passed exception: false" do
110+
describe "and [non-Numeric]" do
111+
it "swallows an error" do
112+
Rational(:sym, exception: false).should == nil
113+
Rational("abc", exception: false).should == nil
114+
end
115+
end
116+
117+
describe "and [non-Numeric, Numeric]" do
118+
it "swallows an error" do
119+
Rational(:sym, 1, exception: false).should == nil
120+
Rational("abc", 1, exception: false).should == nil
121+
end
122+
end
123+
124+
describe "and [anything, non-Numeric]" do
125+
it "swallows an error" do
126+
Rational(:sym, :sym, exception: false).should == nil
127+
Rational("abc", :sym, exception: false).should == nil
128+
end
129+
end
130+
131+
describe "and non-Numeric String arguments" do
132+
it "swallows an error" do
133+
Rational("a", "b", exception: false).should == nil
134+
Rational("a", 0, exception: false).should == nil
135+
Rational(0, "b", exception: false).should == nil
136+
end
137+
end
138+
139+
describe "and nil arguments" do
140+
it "swallows an error" do
141+
Rational(nil, exception: false).should == nil
142+
Rational(nil, nil, exception: false).should == nil
143+
end
144+
end
145+
end
146+
147+
it "freezes its result" do
148+
Rational(1).frozen?.should == true
149+
end
6150
end

core/rational/abs_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require_relative "../../spec_helper"
2-
require_relative '../../shared/rational/abs'
2+
require_relative 'shared/abs'
33

44
describe "Rational#abs" do
55
it_behaves_like :rational_abs, :abs

core/rational/ceil_spec.rb

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
require_relative "../../spec_helper"
2-
require_relative '../../shared/rational/ceil'
32

43
describe "Rational#ceil" do
5-
it_behaves_like :rational_ceil, :ceil
4+
before do
5+
@rational = Rational(2200, 7)
6+
end
7+
8+
describe "with no arguments (precision = 0)" do
9+
it "returns an Integer" do
10+
@rational.ceil.should be_kind_of(Integer)
11+
end
12+
13+
it "returns the truncated value toward positive infinity" do
14+
@rational.ceil.should == 315
15+
Rational(1, 2).ceil.should == 1
16+
Rational(-1, 2).ceil.should == 0
17+
end
18+
end
19+
20+
describe "with a precision < 0" do
21+
it "returns an Integer" do
22+
@rational.ceil(-2).should be_kind_of(Integer)
23+
@rational.ceil(-1).should be_kind_of(Integer)
24+
end
25+
26+
it "moves the truncation point n decimal places left" do
27+
@rational.ceil(-3).should == 1000
28+
@rational.ceil(-2).should == 400
29+
@rational.ceil(-1).should == 320
30+
end
31+
end
32+
33+
describe "with precision > 0" do
34+
it "returns a Rational" do
35+
@rational.ceil(1).should be_kind_of(Rational)
36+
@rational.ceil(2).should be_kind_of(Rational)
37+
end
38+
39+
it "moves the truncation point n decimal places right" do
40+
@rational.ceil(1).should == Rational(3143, 10)
41+
@rational.ceil(2).should == Rational(31429, 100)
42+
@rational.ceil(3).should == Rational(157143, 500)
43+
end
44+
end
645
end

core/rational/comparison_spec.rb

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,93 @@
11
require_relative "../../spec_helper"
2-
require_relative '../../shared/rational/comparison'
2+
require_relative 'fixtures/rational'
33

44
describe "Rational#<=> when passed a Rational object" do
5-
it_behaves_like :rational_cmp_rat, :<=>
5+
it "returns 1 when self is greater than the passed argument" do
6+
(Rational(4, 4) <=> Rational(3, 4)).should equal(1)
7+
(Rational(-3, 4) <=> Rational(-4, 4)).should equal(1)
8+
end
9+
10+
it "returns 0 when self is equal to the passed argument" do
11+
(Rational(4, 4) <=> Rational(4, 4)).should equal(0)
12+
(Rational(-3, 4) <=> Rational(-3, 4)).should equal(0)
13+
end
14+
15+
it "returns -1 when self is less than the passed argument" do
16+
(Rational(3, 4) <=> Rational(4, 4)).should equal(-1)
17+
(Rational(-4, 4) <=> Rational(-3, 4)).should equal(-1)
18+
end
619
end
720

821
describe "Rational#<=> when passed an Integer object" do
9-
it_behaves_like :rational_cmp_int, :<=>
22+
it "returns 1 when self is greater than the passed argument" do
23+
(Rational(4, 4) <=> 0).should equal(1)
24+
(Rational(4, 4) <=> -10).should equal(1)
25+
(Rational(-3, 4) <=> -1).should equal(1)
26+
end
27+
28+
it "returns 0 when self is equal to the passed argument" do
29+
(Rational(4, 4) <=> 1).should equal(0)
30+
(Rational(-8, 4) <=> -2).should equal(0)
31+
end
32+
33+
it "returns -1 when self is less than the passed argument" do
34+
(Rational(3, 4) <=> 1).should equal(-1)
35+
(Rational(-4, 4) <=> 0).should equal(-1)
36+
end
1037
end
1138

1239
describe "Rational#<=> when passed a Float object" do
13-
it_behaves_like :rational_cmp_float, :<=>
40+
it "returns 1 when self is greater than the passed argument" do
41+
(Rational(4, 4) <=> 0.5).should equal(1)
42+
(Rational(4, 4) <=> -1.5).should equal(1)
43+
(Rational(-3, 4) <=> -0.8).should equal(1)
44+
end
45+
46+
it "returns 0 when self is equal to the passed argument" do
47+
(Rational(4, 4) <=> 1.0).should equal(0)
48+
(Rational(-6, 4) <=> -1.5).should equal(0)
49+
end
50+
51+
it "returns -1 when self is less than the passed argument" do
52+
(Rational(3, 4) <=> 1.2).should equal(-1)
53+
(Rational(-4, 4) <=> 0.5).should equal(-1)
54+
end
1455
end
1556

1657
describe "Rational#<=> when passed an Object that responds to #coerce" do
17-
it_behaves_like :rational_cmp_coerce, :<=>
18-
it_behaves_like :rational_cmp_coerce_exception, :<=>
58+
it "calls #coerce on the passed argument with self" do
59+
rational = Rational(3, 4)
60+
61+
obj = mock("Object")
62+
obj.should_receive(:coerce).with(rational).and_return([1, 2])
63+
64+
rational <=> obj
65+
end
66+
67+
it "calls #<=> on the coerced Rational with the coerced Object" do
68+
rational = Rational(3, 4)
69+
70+
coerced_rational = mock("Coerced Rational")
71+
coerced_rational.should_receive(:<=>).and_return(:result)
72+
73+
coerced_obj = mock("Coerced Object")
74+
75+
obj = mock("Object")
76+
obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
77+
78+
(rational <=> obj).should == :result
79+
end
80+
81+
it "does not rescue exception raised in other#coerce" do
82+
b = mock("numeric with failed #coerce")
83+
b.should_receive(:coerce).and_raise(RationalSpecs::CoerceError)
84+
85+
-> { Rational(3, 4) <=> b }.should raise_error(RationalSpecs::CoerceError)
86+
end
1987
end
2088

2189
describe "Rational#<=> when passed a non-Numeric Object that doesn't respond to #coerce" do
22-
it_behaves_like :rational_cmp_other, :<=>
90+
it "returns nil" do
91+
(Rational <=> mock("Object")).should be_nil
92+
end
2393
end

core/rational/denominator_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
require_relative "../../spec_helper"
2-
require_relative '../../shared/rational/denominator'
32

43
describe "Rational#denominator" do
5-
it_behaves_like :rational_denominator, :denominator
4+
it "returns the denominator" do
5+
Rational(3, 4).denominator.should equal(4)
6+
Rational(3, -4).denominator.should equal(4)
7+
8+
Rational(1, bignum_value).denominator.should == bignum_value
9+
end
10+
11+
it "returns 1 if no denominator was given" do
12+
Rational(80).denominator.should == 1
13+
end
614
end

core/rational/div_spec.rb

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,54 @@
11
require_relative "../../spec_helper"
2-
require_relative '../../shared/rational/div'
32

43
describe "Rational#div" do
5-
it_behaves_like :rational_div, :div
4+
it "returns an Integer" do
5+
Rational(229, 21).div(82).should be_kind_of(Integer)
6+
end
7+
8+
it "raises an ArgumentError if passed more than one argument" do
9+
-> { Rational(3, 4).div(2,3) }.should raise_error(ArgumentError)
10+
end
11+
12+
# See http://redmine.ruby-lang.org/issues/show/1648
13+
it "raises a TypeError if passed a non-numeric argument" do
14+
-> { Rational(3, 4).div([]) }.should raise_error(TypeError)
15+
end
616
end
717

818
describe "Rational#div passed a Rational" do
9-
it_behaves_like :rational_div_rat, :div
19+
it "performs integer division and returns the result" do
20+
Rational(2, 3).div(Rational(2, 3)).should == 1
21+
Rational(-2, 9).div(Rational(-9, 2)).should == 0
22+
end
23+
24+
it "raises a ZeroDivisionError when the argument has a numerator of 0" do
25+
-> { Rational(3, 4).div(Rational(0, 3)) }.should raise_error(ZeroDivisionError)
26+
end
27+
28+
it "raises a ZeroDivisionError when the argument has a numerator of 0.0" do
29+
-> { Rational(3, 4).div(Rational(0.0, 3)) }.should raise_error(ZeroDivisionError)
30+
end
1031
end
1132

1233
describe "Rational#div passed an Integer" do
13-
it_behaves_like :rational_div_int, :div
34+
it "performs integer division and returns the result" do
35+
Rational(2, 1).div(1).should == 2
36+
Rational(25, 5).div(-50).should == -1
37+
end
38+
39+
it "raises a ZeroDivisionError when the argument is 0" do
40+
-> { Rational(3, 4).div(0) }.should raise_error(ZeroDivisionError)
41+
end
1442
end
1543

1644
describe "Rational#div passed a Float" do
17-
it_behaves_like :rational_div_float, :div
45+
it "performs integer division and returns the result" do
46+
Rational(2, 3).div(30.333).should == 0
47+
Rational(2, 9).div(Rational(-8.6)).should == -1
48+
Rational(3.12).div(0.5).should == 6
49+
end
50+
51+
it "raises a ZeroDivisionError when the argument is 0.0" do
52+
-> { Rational(3, 4).div(0.0) }.should raise_error(ZeroDivisionError)
53+
end
1854
end

0 commit comments

Comments
 (0)