Skip to content

Commit 571f339

Browse files
committed
[Bug #21106] Fix tests for custom random object
When a positive integer limit is given, `rand` method of a RNG object is expected to return a value between 0 and the limit (exclusive). Fix shuffle_spec.rb like as the similar code in sample_spec.rb, and add tests for greater values. TODO: - Return a value that is equal to or greater than the limit given to the RNG object. - Extract common code about RNG objects to a shared file.
1 parent 8dd0d63 commit 571f339

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

spec/ruby/core/array/sample_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@
114114

115115
-> { [1, 2].sample(random: random) }.should raise_error(RangeError)
116116
end
117+
118+
it "raises a RangeError if the value is greater than the Array size" do
119+
random = mock("array_sample_random")
120+
random.should_receive(:rand).and_return(3)
121+
122+
-> { [1, 2].sample(random: random) }.should raise_error(RangeError)
123+
end
117124
end
118125
end
119126

spec/ruby/core/array/shuffle_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,18 @@
6969
-> { [1, 2].shuffle(random: random) }.should raise_error(RangeError)
7070
end
7171

72-
it "raises a RangeError if the value is equal to one" do
72+
it "raises a RangeError if the value is equal to the Array size" do
7373
value = mock("array_shuffle_random_value")
74-
value.should_receive(:to_int).at_least(1).times.and_return(1)
74+
value.should_receive(:to_int).at_least(1).times.and_return(2)
75+
random = mock("array_shuffle_random")
76+
random.should_receive(:rand).at_least(1).times.and_return(value)
77+
78+
-> { [1, 2].shuffle(random: random) }.should raise_error(RangeError)
79+
end
80+
81+
it "raises a RangeError if the value is greater than the Array size" do
82+
value = mock("array_shuffle_random_value")
83+
value.should_receive(:to_int).at_least(1).times.and_return(3)
7584
random = mock("array_shuffle_random")
7685
random.should_receive(:rand).at_least(1).times.and_return(value)
7786

0 commit comments

Comments
 (0)