focalboard/server/services/notify/notifymentions/mentions_test.go

53 lines
1.5 KiB
Go
Raw Normal View History

2021-09-13 21:36:36 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package notifymentions
import (
"reflect"
"testing"
"github.com/mattermost/focalboard/server/model"
mm_model "github.com/mattermost/mattermost-server/v6/model"
)
func Test_extractMentions(t *testing.T) {
tests := []struct {
name string
block *model.Block
want map[string]struct{}
}{
{name: "empty", block: makeBlock(""), want: makeMap()},
{name: "zero mentions", block: makeBlock("This is some text."), want: makeMap()},
{name: "one mention", block: makeBlock("Hello @user1"), want: makeMap("user1")},
{name: "multiple mentions", block: makeBlock("Hello @user1, @user2 and @user3"), want: makeMap("user1", "user2", "user3")},
{name: "include period", block: makeBlock("Hello @user1."), want: makeMap("user1.")},
{name: "include underscore", block: makeBlock("Hello @user1_"), want: makeMap("user1_")},
{name: "don't include comma", block: makeBlock("Hello @user1,"), want: makeMap("user1")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := extractMentions(tt.block); !reflect.DeepEqual(got, tt.want) {
t.Errorf("extractMentions() = %v, want %v", got, tt.want)
}
})
}
}
func makeBlock(text string) *model.Block {
return &model.Block{
ID: mm_model.NewId(),
2021-10-27 03:11:19 +02:00
Type: model.TypeComment,
2021-09-13 21:36:36 +02:00
Title: text,
}
}
func makeMap(mentions ...string) map[string]struct{} {
m := make(map[string]struct{})
for _, mention := range mentions {
m[mention] = struct{}{}
}
return m
}