|
1 | 1 | require_relative '../../spec_helper' |
2 | | -require_relative '../../shared/rational/Rational' |
| 2 | +require_relative '../rational/fixtures/rational' |
3 | 3 |
|
4 | 4 | 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 |
6 | 150 | end |
0 commit comments