尝试解决正则表达式登录问题。以下是规则:
以下是必须通过的测试用例:
Test.describe("Basic tests") do
Test.assert_equals(validate_usr('asddsa'), true)
Test.assert_equals(validate_usr('a'), false)
Test.assert_equals(validate_usr('Hass'), false)
Test.assert_equals(validate_usr('Hasd_12assssssasasasasasaasasasasas'), false)
Test.assert_equals(validate_usr(''), false)
Test.assert_equals(validate_usr('____'), true)
Test.assert_equals(validate_usr('012'), false)
Test.assert_equals(validate_usr('p1pp1'), true)
Test.assert_equals(validate_usr('asd43 34'), false)
Test.assert_equals(validate_usr('asd43_34'), true)
end
这是我的代码:
def validate_usr(username)
if (username.length > 3 && username == username.downcase)
return true
elsif
username.include?(" ") == true
return false
else
return false
end
end
遇到涉及空间的测试用例9的问题。它返回true
。
答案 0 :(得分:2)
Test case 9 returns true
because it satisfies username.length > 3 && username == username.downcase
. Note that " ".downcase == " "
here.
Move that return false if username.include?(" ")
up and it should work. I honestly suggest using Regex
though.
def validate_usr(username)
/\A[a-z0-9_]{4,16}\z/ === username
end