Better string input randomization

This commit is contained in:
cryptogopher 2024-01-06 01:05:28 +01:00
parent 5c70736a91
commit d1bfabc105
2 changed files with 6 additions and 3 deletions

View File

@ -18,8 +18,11 @@ class UnitsTest < ApplicationSystemTestCase
within first('tbody > tr') do
assert_selector ':focus'
fill_in 'unit[symbol]', with: SecureRandom.random_symbol(rand(1..16))
fill_in 'unit[name]', with: [nil, SecureRandom.alphanumeric(rand(1..255))].sample
maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 1000] }
fill_in 'unit[symbol]',
with: SecureRandom.random_symbol(rand([1..15, 15..maxlength['unit[symbol]']].sample))
fill_in 'unit[name]',
with: [nil, SecureRandom.alphanumeric(rand(1..maxlength['unit[name]']))].sample
assert_difference ->{ Unit.count }, 1 do
click_on t(:add)
end

View File

@ -16,7 +16,7 @@ class ActiveSupport::TestCase
SecureRandom.class_eval do
def self.random_symbol(n = 10)
# Unicode characters: 32-126, 160-383
choose([*' '..'~', 160.chr, *'¡'..'ſ'], n)
choose([*' '..'~', 160.chr(Encoding::UTF_8), *'¡'..'ſ'], n)
end
end