Skip to content
Snippets Groups Projects
Commit 8cf8bdd7 authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Merge branch 'nicknames' into 'release'

added nickname validity checking

See merge request !32
parents 9a25e2d3 40a20755
Branches
Tags
2 merge requests!32added nickname validity checking,!24Release Updates for Channels
package nicknames
import (
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
)
const (
MinNicknameLength = 3
MaxNicknameLength = 24
)
var ErrNicknameTooShort = errors.Errorf("nicknames must be at least "+
"%d characters in length", MinNicknameLength)
var ErrNicknameTooLong = errors.Errorf("nicknames must be %d "+
"characters in length or less", MaxNicknameLength)
// IsValid checks if a nickname is valid.
//
// Rules:
// - A nickname must not be longer than 24 characters.
// - A nickname must not be shorter than 1 character.
// - A nickname may be blank, this will be treated by the system as
// no nickname
//
// TODO: Add character filtering.
func IsValid(nick string) error {
if nick == "" {
jww.INFO.Printf("empty nickname passed, treating like no " +
"nickname")
return nil
}
runeNick := []rune(nick)
if len(runeNick) < MinNicknameLength {
return errors.WithStack(ErrNicknameTooShort)
}
if len(runeNick) > MaxNicknameLength {
return errors.WithStack(ErrNicknameTooLong)
}
return nil
}
package nicknames
import (
"github.com/pkg/errors"
"testing"
)
func TestIsNicknameValid(t *testing.T) {
// test that behavior for an empty nickname is correct
if err := IsValid(""); err != nil {
t.Errorf("Empty nickname should be valid, received: %+v", err)
}
nicknameSource := "Sodium, atomic number 11, was first isolated by Humphry " +
"Davy in 1807. A chemical component of salt, he named it Na in honor " +
"of the saltiest region on earth, North America."
// test that behavior for too short nicknames is correct
for i := 1; i < MinNicknameLength; i++ {
nick := nicknameSource[:i]
if err := IsValid(nick); err != nil &&
!errors.Is(err, ErrNicknameTooShort) {
t.Errorf("Wrong error returned from nicknames.IsValid() "+
"with too short input of length %d: %+v", i, err)
} else if err == nil {
t.Errorf("No error returned from nicknames.IsValid() "+
"with too short input of length %d", i)
}
}
// test that behavior for too long nicknames is correct
for i := MaxNicknameLength + 1; i < MaxNicknameLength*5; i++ {
nick := nicknameSource[:i]
if err := IsValid(nick); err != nil &&
!errors.Is(err, ErrNicknameTooLong) {
t.Errorf("Wrong error returned from nicknames.IsValid() "+
"with too long input of length %d: %+v", i, err)
} else if err == nil {
t.Errorf("No error returned from nicknames.IsValid() "+
"with too long input of length %d", i)
}
}
// test that behavior for valid nicknames is correct
for i := MinNicknameLength; i <= MaxNicknameLength; i++ {
nick := nicknameSource[:i]
if err := IsValid(nick); err != nil {
t.Errorf("Error returned from nicknames.IsValid() "+
"with valid nickname of input of length %d: %+v", i, err)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment