Skip to content

Commit caa5507

Browse files
authored
Faker::Alphanumeric.alphanumeric determinism fix (#2996)
* Faker::Alphanumeric.alphanumeric determinism fix * Faker::Alphanumeric.alphanumeric changes from PR feedback
1 parent 961cafd commit caa5507

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

lib/faker.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ def shuffle(list)
249249
list.shuffle(random: Faker::Config.random)
250250
end
251251

252+
def shuffle!(list)
253+
list.shuffle!(random: Faker::Config.random)
254+
end
255+
252256
def rand(max = nil)
253257
if max.nil?
254258
Faker::Config.random.rand

lib/faker/default/alphanumeric.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def alphanumeric(number: 32, min_alpha: 0, min_numeric: 0)
6060
randoms = Array.new(random_count) { sample(ALPHANUMS) }
6161

6262
combined = alphas + numbers + randoms
63-
combined.shuffle.join
63+
shuffle!(combined).join
6464
end
6565
end
6666
end

test/faker/default/test_alphanum.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ def test_alphanumeric_with_min_numeric
4848

4949
assert_operator numbers.compact.size, :>=, 4
5050
end
51+
52+
def test_alphanumeric_with_min_alpha_and_min_numeric
53+
deterministically_verify -> { @tester.alphanumeric(number: 10, min_alpha: 5, min_numeric: 5) } do |alphanum|
54+
assert_equal 10, alphanum.size
55+
assert_equal 5, alphanum.scan(/[[:alpha:]]/).size
56+
assert_equal 5, alphanum.scan(/[[:digit:]]/).size
57+
end
58+
end
5159
end

0 commit comments

Comments
 (0)