Skip to content

Commit

Permalink
Faker::Alphanumeric.alphanumeric determinism fix (#2996)
Browse files Browse the repository at this point in the history
* Faker::Alphanumeric.alphanumeric determinism fix

* Faker::Alphanumeric.alphanumeric changes from PR feedback
  • Loading branch information
admtnnr committed Sep 6, 2024
1 parent 961cafd commit caa5507
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ def shuffle(list)
list.shuffle(random: Faker::Config.random)
end

def shuffle!(list)
list.shuffle!(random: Faker::Config.random)
end

def rand(max = nil)
if max.nil?
Faker::Config.random.rand
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/alphanumeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def alphanumeric(number: 32, min_alpha: 0, min_numeric: 0)
randoms = Array.new(random_count) { sample(ALPHANUMS) }

combined = alphas + numbers + randoms
combined.shuffle.join
shuffle!(combined).join
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/faker/default/test_alphanum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ def test_alphanumeric_with_min_numeric

assert_operator numbers.compact.size, :>=, 4
end

def test_alphanumeric_with_min_alpha_and_min_numeric
deterministically_verify -> { @tester.alphanumeric(number: 10, min_alpha: 5, min_numeric: 5) } do |alphanum|
assert_equal 10, alphanum.size
assert_equal 5, alphanum.scan(/[[:alpha:]]/).size
assert_equal 5, alphanum.scan(/[[:digit:]]/).size
end
end
end

0 comments on commit caa5507

Please sign in to comment.