Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringUtils.create_random_alpha_string の生成する文字の上限を廃止 #21

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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