Enable punctuations ending mentions (#8889)

* Enable punctuations ending mentions

* Improve tests
This commit is contained in:
guillep2k 2019-11-09 19:12:05 -03:00 committed by techknowlogick
parent c54145174f
commit bb04fb55d7
2 changed files with 37 additions and 12 deletions

View file

@ -27,7 +27,7 @@ var (
// TODO: fix invalid linking issue // TODO: fix invalid linking issue
// mentionPattern matches all mentions in the form of "@user" // mentionPattern matches all mentions in the form of "@user"
mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_\.]+)(?:\s|$|\)|\])`) mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_][0-9a-zA-Z-_.]+[0-9a-zA-Z-_])(?:\s|[:,;.?!]\s|[:,;.?!]?$|\)|\])`)
// issueNumericPattern matches string that references to a numeric issue, e.g. #1287 // issueNumericPattern matches string that references to a numeric issue, e.g. #1287
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`) issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
// issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234 // issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234

View file

@ -208,14 +208,32 @@ func testFixtures(t *testing.T, fixtures []testFixture, context string) {
} }
func TestRegExp_mentionPattern(t *testing.T) { func TestRegExp_mentionPattern(t *testing.T) {
trueTestCases := []string{ trueTestCases := []struct {
"@Unknwon", pat string
"@ANT_123", exp string
"@xxx-DiN0-z-A..uru..s-xxx", }{
" @lol ", {"@Unknwon", "@Unknwon"},
" @Te-st", {"@ANT_123", "@ANT_123"},
"(@gitea)", {"@xxx-DiN0-z-A..uru..s-xxx", "@xxx-DiN0-z-A..uru..s-xxx"},
"[@gitea]", {" @lol ", "@lol"},
{" @Te-st", "@Te-st"},
{"(@gitea)", "@gitea"},
{"[@gitea]", "@gitea"},
{"@gitea! this", "@gitea"},
{"@gitea? this", "@gitea"},
{"@gitea. this", "@gitea"},
{"@gitea, this", "@gitea"},
{"@gitea; this", "@gitea"},
{"@gitea!\nthis", "@gitea"},
{"\n@gitea?\nthis", "@gitea"},
{"\t@gitea.\nthis", "@gitea"},
{"@gitea,\nthis", "@gitea"},
{"@gitea;\nthis", "@gitea"},
{"@gitea!", "@gitea"},
{"@gitea?", "@gitea"},
{"@gitea.", "@gitea"},
{"@gitea,", "@gitea"},
{"@gitea;", "@gitea"},
} }
falseTestCases := []string{ falseTestCases := []string{
"@ 0", "@ 0",
@ -223,17 +241,24 @@ func TestRegExp_mentionPattern(t *testing.T) {
"@", "@",
"", "",
"ABC", "ABC",
"@.ABC",
"/home/gitea/@gitea", "/home/gitea/@gitea",
"\"@gitea\"", "\"@gitea\"",
"@@gitea",
"@gitea!this",
"@gitea?this",
"@gitea,this",
"@gitea;this",
} }
for _, testCase := range trueTestCases { for _, testCase := range trueTestCases {
res := mentionPattern.MatchString(testCase) found := mentionPattern.FindStringSubmatch(testCase.pat)
assert.True(t, res) assert.Len(t, found, 2)
assert.Equal(t, testCase.exp, found[1])
} }
for _, testCase := range falseTestCases { for _, testCase := range falseTestCases {
res := mentionPattern.MatchString(testCase) res := mentionPattern.MatchString(testCase)
assert.False(t, res) assert.False(t, res, "[%s] should be false", testCase)
} }
} }