From c7b7b3af99c903ea45aece1d579dbc0d273dede5 Mon Sep 17 00:00:00 2001 From: Koichi Hirachi Date: Sat, 13 Apr 2024 01:29:59 +0900 Subject: [PATCH] fix test --- main.go | 4 ++++ main_test.go | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 3e3bbfb..25dfbc9 100644 --- a/main.go +++ b/main.go @@ -301,6 +301,10 @@ func ScreenName2user(logger *zap.Logger, sess *discordgo.Session, guildID string var usernameRe = regexp.MustCompile(`(^.{2,32})#(\d{4}$)`) func DiscordUserParse(usernameRaw string) (username, discriminator string, err error) { + // name shoud be 2-32 characters + if utf8string.NewString(usernameRaw).RuneCount() < 2 || utf8string.NewString(usernameRaw).RuneCount() > 32 { + return "", "", fmt.Errorf("username length invalid") + } if !usernameRe.MatchString(usernameRaw) { // for new type username return usernameRaw, "", nil diff --git a/main_test.go b/main_test.go index 4be428f..90b5c47 100644 --- a/main_test.go +++ b/main_test.go @@ -122,10 +122,6 @@ func TestDiscordUserParse(t *testing.T) { descriminator: "1234", wantErr: false, }, - "missmatch pattern": { - input: "onlyname", - wantErr: false, - }, "misspattern": { input: `a`, wantErr: true, @@ -143,6 +139,12 @@ func TestDiscordUserParse(t *testing.T) { assert.Equal(t, v.descriminator, d) }) } + t.Run("new type username pattern", func(t *testing.T) { + u, d, err := DiscordUserParse("username") + assert.Empty(t, err) + assert.Equal(t, "username", u) + assert.Empty(t, d) + }) } func TestCreateMsg(t *testing.T) {