Selaa lähdekoodia

clean up ValidateAIMHandle

Mike 1 vuosi sitten
vanhempi
commit
8a9b0ab90c
2 muutettua tiedostoa jossa 10 lisäystä ja 11 poistoa
  1. 9 11
      state/user.go
  2. 1 0
      state/user_test.go

+ 9 - 11
state/user.go

@@ -70,16 +70,20 @@ var (
 //   - ErrAIMHandleInvalidFormat: if the screen name does not start with a
 //     letter, ends with a space, or contains invalid characters
 func (s DisplayScreenName) ValidateAIMHandle() error {
+	if len(s) > 16 {
+		return ErrAIMHandleLength
+	}
+
 	c := 0
 	for _, r := range s {
-		if unicode.IsLetter(r) || unicode.IsDigit(r) {
+		switch {
+		case unicode.IsLetter(r) || unicode.IsDigit(r):
 			c++
-		}
-		if c == 3 {
-			break
+		case r != ' ':
+			return ErrAIMHandleInvalidFormat
 		}
 	}
-	if c < 3 || len(s) > 16 {
+	if c < 3 {
 		return ErrAIMHandleLength
 	}
 
@@ -89,12 +93,6 @@ func (s DisplayScreenName) ValidateAIMHandle() error {
 		return ErrAIMHandleInvalidFormat
 	}
 
-	for _, ch := range s {
-		if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && ch != ' ' {
-			return ErrAIMHandleInvalidFormat
-		}
-	}
-
 	return nil
 }
 

+ 1 - 0
state/user_test.go

@@ -221,6 +221,7 @@ func TestDisplayScreenName_ValidateAIMHandle(t *testing.T) {
 		{"Too short", "Us", ErrAIMHandleLength},
 		{"Too short due to spaces", "U S", ErrAIMHandleLength},
 		{"Too long", "ThisIsAReallyLongScreenName", ErrAIMHandleLength},
+		{"Too many spaces", "User           123 ", ErrAIMHandleLength},
 		{"Starts with number", "1User", ErrAIMHandleInvalidFormat},
 		{"Ends with space", "User123 ", ErrAIMHandleInvalidFormat},
 		{"Contains invalid character", "User@123", ErrAIMHandleInvalidFormat},