Skip to content

Commit

Permalink
Backport: Enable punctuations ending mentions (#8889) (#8894)
Browse files Browse the repository at this point in the history
* Enable punctuations ending mentions

* Improve tests
  • Loading branch information
guillep2k authored and techknowlogick committed Nov 10, 2019
1 parent b27cac0 commit 8693e54
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion modules/references/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
// TODO: fix invalid linking issue

// 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 = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
// issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
Expand Down
47 changes: 36 additions & 11 deletions modules/references/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,32 +204,57 @@ func TestFindAllIssueReferences(t *testing.T) {
}

func TestRegExp_mentionPattern(t *testing.T) {
trueTestCases := []string{
"@Unknwon",
"@ANT_123",
"@xxx-DiN0-z-A..uru..s-xxx",
" @lol ",
" @Te-st",
"(@gitea)",
"[@gitea]",
trueTestCases := []struct {
pat string
exp string
}{
{"@Unknwon", "@Unknwon"},
{"@ANT_123", "@ANT_123"},
{"@xxx-DiN0-z-A..uru..s-xxx", "@xxx-DiN0-z-A..uru..s-xxx"},
{" @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{
"@ 0",
"@ ",
"@",
"",
"ABC",
"@.ABC",
"/home/gitea/@gitea",
"\"@gitea\"",
"@@gitea",
"@gitea!this",
"@gitea?this",
"@gitea,this",
"@gitea;this",
}

for _, testCase := range trueTestCases {
res := mentionPattern.MatchString(testCase)
assert.True(t, res)
found := mentionPattern.FindStringSubmatch(testCase.pat)
assert.Len(t, found, 2)
assert.Equal(t, testCase.exp, found[1])
}
for _, testCase := range falseTestCases {
res := mentionPattern.MatchString(testCase)
assert.False(t, res)
assert.False(t, res, "[%s] should be false", testCase)
}
}

Expand Down

0 comments on commit 8693e54

Please sign in to comment.