github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/at_mentions_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestPossibleAtMentions(t *testing.T) {
    13  	fixture := []struct {
    14  		message  string
    15  		expected []string
    16  	}{
    17  		{
    18  			"",
    19  			[]string{},
    20  		},
    21  		{
    22  			"@user",
    23  			[]string{"user"},
    24  		},
    25  		{
    26  			"@user-with_special.chars @multiple.-_chars",
    27  			[]string{"user-with_special.chars", "multiple.-_chars"},
    28  		},
    29  		{
    30  			"@repeated @user @repeated",
    31  			[]string{"repeated", "user"},
    32  		},
    33  		{
    34  			"@user1 @user2 @user3",
    35  			[]string{"user1", "user2", "user3"},
    36  		},
    37  		{
    38  			"@李",
    39  			[]string{},
    40  		},
    41  		{
    42  			"@withfinaldot. @withfinaldash- @withfinalunderscore_",
    43  			[]string{
    44  				"withfinaldot.",
    45  				"withfinaldash-",
    46  				"withfinalunderscore_",
    47  			},
    48  		},
    49  	}
    50  
    51  	for _, data := range fixture {
    52  		actual := PossibleAtMentions(data.message)
    53  		require.ElementsMatch(t, actual, data.expected)
    54  	}
    55  }
    56  
    57  func TestTrimUsernameSpecialChar(t *testing.T) {
    58  	fixture := []struct {
    59  		word           string
    60  		expectedString string
    61  		expectedBool   bool
    62  	}{
    63  		{"user...", "user..", true},
    64  		{"user..", "user.", true},
    65  		{"user.", "user", true},
    66  		{"user--", "user-", true},
    67  		{"user-", "user", true},
    68  		{"user_.-", "user_.", true},
    69  		{"user_.", "user_", true},
    70  		{"user_", "user", true},
    71  		{"user", "user", false},
    72  		{"user.with-inner_chars", "user.with.inner.chars", false},
    73  	}
    74  
    75  	for _, data := range fixture {
    76  		actualString, actualBool := TrimUsernameSpecialChar(data.word)
    77  		require.Equal(t, actualBool, data.expectedBool)
    78  		if actualBool {
    79  			require.Equal(t, actualString, data.expectedString)
    80  		} else {
    81  			require.Equal(t, actualString, data.word)
    82  		}
    83  	}
    84  }