Skip to content

Commit

Permalink
Merge pull request #21 from TakuyaNoguchi/remove_character_restrictio…
Browse files Browse the repository at this point in the history
…n_of_create_random_alpha_string

StringUtils.create_random_alpha_string の生成する文字の上限を廃止
  • Loading branch information
TakuyaNoguchi authored May 19, 2022
2 parents d889794 + 5618542 commit 36ef776
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bizside_test_app/test/lib/bizside/string_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,21 @@ def test_rand_string
assert s =~ /^(?=.*?[a-z])(?=.*?\d).*+$/iu
end

def test_create_random_alpha_string_case_sensitive_true
s = StringUtils.create_random_alpha_string(10, true)
assert s =~ /\A[A-Za-z]{10}\z/
end

def test_create_random_alpha_string_case_sensitive_false
s = StringUtils.create_random_alpha_string(10, false)
assert s =~ /\A[a-z]{10}\z/
end

def test_create_random_alpha_string_more_than_26_chars
s = StringUtils.create_random_alpha_string(53, true)
assert s =~ /\A[A-Za-z]{53}\z/

s = StringUtils.create_random_alpha_string(27, false)
assert s =~ /\A[a-z]{27}\z/
end
end
4 changes: 3 additions & 1 deletion lib/bizside/string_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class StringUtils
def self.create_random_alpha_string length, case_sensitive = false
chars = ('a'..'z').to_a
chars += ('A'..'Z').to_a if case_sensitive
chars.sample(length).join
chars_length = chars.length

Array.new(length) { chars[rand(chars_length)] }.join
end

def self.create_random_string(number)
Expand Down

0 comments on commit 36ef776

Please sign in to comment.