1require_relative '../../spec_helper' 2require_relative 'fixtures/classes' 3 4describe "Kernel.rand" do 5 it "is a private method" do 6 Kernel.should have_private_instance_method(:rand) 7 end 8 9 it "returns a float if no argument is passed" do 10 rand.should be_kind_of(Float) 11 end 12 13 it "returns an integer for an integer argument" do 14 rand(77).should be_kind_of(Integer) 15 end 16 17 it "returns an integer for a float argument greater than 1" do 18 rand(1.3).should be_kind_of(Integer) 19 end 20 21 it "returns a float for an argument between -1 and 1" do 22 rand(-0.999).should be_kind_of(Float) 23 rand(-0.01).should be_kind_of(Float) 24 rand(0).should be_kind_of(Float) 25 rand(0.01).should be_kind_of(Float) 26 rand(0.999).should be_kind_of(Float) 27 end 28 29 it "ignores the sign of the argument" do 30 [0, 1, 2, 3].should include(rand(-4)) 31 end 32 33 it "never returns a value greater or equal to 1.0 with no arguments" do 34 1000.times do 35 (0...1.0).should include(rand) 36 end 37 end 38 39 it "never returns a value greater or equal to any passed in max argument" do 40 1000.times do 41 (0...100).to_a.should include(rand(100)) 42 end 43 end 44 45 it "calls to_int on its argument" do 46 l = mock('limit') 47 l.should_receive(:to_int).and_return 7 48 49 rand l 50 end 51 52 context "given an exclusive range" do 53 it "returns an Integer between the two Integers" do 54 1000.times do 55 x = rand(4...6) 56 x.should be_kind_of(Integer) 57 (4...6).should include(x) 58 end 59 end 60 61 it "returns a Float between the given Integer and Float" do 62 1000.times do 63 x = rand(4...6.5) 64 x.should be_kind_of(Float) 65 (4...6.5).should include(x) 66 end 67 end 68 69 it "returns a Float between the given Float and Integer" do 70 1000.times do 71 x = rand(3.5...6) 72 x.should be_kind_of(Float) 73 (3.5...6).should include(x) 74 end 75 end 76 77 it "returns a Float between the two given Floats" do 78 1000.times do 79 x = rand(3.5...6.5) 80 x.should be_kind_of(Float) 81 (3.5...6.5).should include(x) 82 end 83 end 84 end 85 86 context "given an inclusive range" do 87 it "returns an Integer between the two Integers" do 88 1000.times do 89 x = rand(4..6) 90 x.should be_kind_of(Integer) 91 (4..6).should include(x) 92 end 93 end 94 95 it "returns a Float between the given Integer and Float" do 96 1000.times do 97 x = rand(4..6.5) 98 x.should be_kind_of(Float) 99 (4..6.5).should include(x) 100 end 101 end 102 103 it "returns a Float between the given Float and Integer" do 104 1000.times do 105 x = rand(3.5..6) 106 x.should be_kind_of(Float) 107 (3.5..6).should include(x) 108 end 109 end 110 111 it "returns a Float between the two given Floats" do 112 1000.times do 113 x = rand(3.5..6.5) 114 x.should be_kind_of(Float) 115 (3.5..6.5).should include(x) 116 end 117 end 118 end 119 120 it "returns a numeric for an range argument where max is < 1" do 121 rand(0.25..0.75).should be_kind_of(Numeric) 122 end 123 124 it "returns nil when range is backwards" do 125 rand(1..0).should be_nil 126 end 127 128 it "returns the range start/end when Float range is 0" do 129 rand(1.0..1.0).should eql(1.0) 130 end 131 132 it "returns the range start/end when Integer range is 0" do 133 rand(42..42).should eql(42) 134 end 135end 136 137describe "Kernel#rand" do 138 it "needs to be reviewed for spec completeness" 139end 140