diff options
Diffstat (limited to 'spec/ruby/core/random')
| -rw-r--r-- | spec/ruby/core/random/fixtures/classes.rb | 15 | ||||
| -rw-r--r-- | spec/ruby/core/random/new_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/random/rand_spec.rb | 19 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/urandom.rb | 2 |
4 files changed, 30 insertions, 8 deletions
diff --git a/spec/ruby/core/random/fixtures/classes.rb b/spec/ruby/core/random/fixtures/classes.rb new file mode 100644 index 0000000000..9c8d113e24 --- /dev/null +++ b/spec/ruby/core/random/fixtures/classes.rb @@ -0,0 +1,15 @@ +module RandomSpecs + CustomRangeInteger = Struct.new(:value) do + def to_int; value; end + def <=>(other); to_int <=> other.to_int; end + def -(other); self.class.new(to_int - other.to_int); end + def +(other); self.class.new(to_int + other.to_int); end + end + + CustomRangeFloat = Struct.new(:value) do + def to_f; value; end + def <=>(other); to_f <=> other.to_f; end + def -(other); to_f - other.to_f; end + def +(other); self.class.new(to_f + other.to_f); end + end +end diff --git a/spec/ruby/core/random/new_spec.rb b/spec/ruby/core/random/new_spec.rb index 8160f44d79..07d7b439ca 100644 --- a/spec/ruby/core/random/new_spec.rb +++ b/spec/ruby/core/random/new_spec.rb @@ -30,7 +30,7 @@ describe "Random.new" do end it "raises a RangeError if passed a Complex (with imaginary part) seed value as an argument" do - lambda do + -> do Random.new(Complex(20,2)) end.should raise_error(RangeError) end diff --git a/spec/ruby/core/random/rand_spec.rb b/spec/ruby/core/random/rand_spec.rb index 395e89dc75..b585aa9737 100644 --- a/spec/ruby/core/random/rand_spec.rb +++ b/spec/ruby/core/random/rand_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe "Random.rand" do it "returns a Float if no max argument is passed" do @@ -83,13 +84,13 @@ describe "Random#rand with Fixnum" do end it "raises an ArgumentError when the argument is 0" do - lambda do + -> do Random.new.rand(0) end.should raise_error(ArgumentError) end it "raises an ArgumentError when the argument is negative" do - lambda do + -> do Random.new.rand(-12) end.should raise_error(ArgumentError) end @@ -122,7 +123,7 @@ describe "Random#rand with Bignum" do end it "raises an ArgumentError when the argument is negative" do - lambda do + -> do Random.new.rand(-bignum_value) end.should raise_error(ArgumentError) end @@ -154,7 +155,7 @@ describe "Random#rand with Float" do end it "raises an ArgumentError when the argument is negative" do - lambda do + -> do Random.new.rand(-1.234567) end.should raise_error(ArgumentError) end @@ -165,6 +166,12 @@ describe "Random#rand with Range" do Random.new.rand(20..43).should be_an_instance_of(Fixnum) end + it "supports custom object types" do + rand(RandomSpecs::CustomRangeInteger.new(1)..RandomSpecs::CustomRangeInteger.new(42)).should be_an_instance_of(RandomSpecs::CustomRangeInteger) + rand(RandomSpecs::CustomRangeFloat.new(1.0)..RandomSpecs::CustomRangeFloat.new(42.0)).should be_an_instance_of(RandomSpecs::CustomRangeFloat) + rand(Time.now..Time.now).should be_an_instance_of(Time) + end + it "returns an object that is a member of the Range" do prng = Random.new r = 20..30 @@ -203,13 +210,13 @@ describe "Random#rand with Range" do end it "raises an ArgumentError when the startpoint lacks #+ and #- methods" do - lambda do + -> do Random.new.rand(Object.new..67) end.should raise_error(ArgumentError) end it "raises an ArgumentError when the endpoint lacks #+ and #- methods" do - lambda do + -> do Random.new.rand(68..Object.new) end.should raise_error(ArgumentError) end diff --git a/spec/ruby/core/random/shared/urandom.rb b/spec/ruby/core/random/shared/urandom.rb index 316167b63f..159716075c 100644 --- a/spec/ruby/core/random/shared/urandom.rb +++ b/spec/ruby/core/random/shared/urandom.rb @@ -8,7 +8,7 @@ describe :random_urandom, shared: true do end it "raises an ArgumentError on a negative size" do - lambda { + -> { Random.send(@method, -1) }.should raise_error(ArgumentError) end |
