diff options
Diffstat (limited to 'spec/ruby/core/array/shuffle_spec.rb')
| -rw-r--r-- | spec/ruby/core/array/shuffle_spec.rb | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/spec/ruby/core/array/shuffle_spec.rb b/spec/ruby/core/array/shuffle_spec.rb index 4f793acf19..b84394bcb5 100644 --- a/spec/ruby/core/array/shuffle_spec.rb +++ b/spec/ruby/core/array/shuffle_spec.rb @@ -25,12 +25,6 @@ describe "Array#shuffle" do ArraySpecs::MyArray[1, 2, 3].shuffle.should be_an_instance_of(Array) end - it "attempts coercion via #to_hash" do - obj = mock('hash') - obj.should_receive(:to_hash).once.and_return({}) - [2, 3].shuffle(obj) - end - it "calls #rand on the Object passed by the :random key in the arguments Hash" do obj = mock("array_shuffle_random") obj.should_receive(:rand).at_least(1).times.and_return(0.5) @@ -43,7 +37,7 @@ describe "Array#shuffle" do it "raises a NoMethodError if an object passed for the RNG does not define #rand" do obj = BasicObject.new - lambda { [1, 2].shuffle(random: obj) }.should raise_error(NoMethodError) + -> { [1, 2].shuffle(random: obj) }.should raise_error(NoMethodError) end it "accepts a Float for the value returned by #rand" do @@ -53,6 +47,10 @@ describe "Array#shuffle" do [1, 2].shuffle(random: random).should be_an_instance_of(Array) end + it "accepts a Random class for the value for random: argument" do + [1, 2].shuffle(random: Random).should be_an_instance_of(Array) + end + it "calls #to_int on the Object returned by #rand" do value = mock("array_shuffle_random_value") value.should_receive(:to_int).at_least(1).times.and_return(0) @@ -68,16 +66,25 @@ describe "Array#shuffle" do random = mock("array_shuffle_random") random.should_receive(:rand).and_return(value) - lambda { [1, 2].shuffle(random: random) }.should raise_error(RangeError) + -> { [1, 2].shuffle(random: random) }.should raise_error(RangeError) + end + + it "raises a RangeError if the value is equal to the Array size" do + value = mock("array_shuffle_random_value") + value.should_receive(:to_int).at_least(1).times.and_return(2) + random = mock("array_shuffle_random") + random.should_receive(:rand).at_least(1).times.and_return(value) + + -> { [1, 2].shuffle(random: random) }.should raise_error(RangeError) end - it "raises a RangeError if the value is equal to one" do + it "raises a RangeError if the value is greater than the Array size" do value = mock("array_shuffle_random_value") - value.should_receive(:to_int).at_least(1).times.and_return(1) + value.should_receive(:to_int).at_least(1).times.and_return(3) random = mock("array_shuffle_random") random.should_receive(:rand).at_least(1).times.and_return(value) - lambda { [1, 2].shuffle(random: random) }.should raise_error(RangeError) + -> { [1, 2].shuffle(random: random) }.should raise_error(RangeError) end end @@ -95,8 +102,18 @@ describe "Array#shuffle!" do a.should equal(original) end - it "raises a #{frozen_error_class} on a frozen array" do - lambda { ArraySpecs.frozen_array.shuffle! }.should raise_error(frozen_error_class) - lambda { ArraySpecs.empty_frozen_array.shuffle! }.should raise_error(frozen_error_class) + it "raises a FrozenError on a frozen array" do + -> { ArraySpecs.frozen_array.shuffle! }.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.shuffle! }.should raise_error(FrozenError) + end + + it "matches CRuby with random:" do + %w[a b c].shuffle(random: Random.new(1)).should == %w[a c b] + (0..10).to_a.shuffle(random: Random.new(10)).should == [2, 6, 8, 5, 7, 10, 3, 1, 0, 4, 9] + end + + it "matches CRuby with srand" do + srand(123) + %w[a b c d e f g h i j k].shuffle.should == %w[a e f h i j d b g k c] end end |
