summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorguillep2k <18600385+guillep2k@users.noreply.github.com>2019-11-09 22:24:59 -0300
committertechknowlogick <techknowlogick@gitea.io>2019-11-09 20:24:59 -0500
commit8693e544269e45e8415cb5c9ce350ed07a1e3bf6 (patch)
treeaa7130ef8721b8c542ae778fe94f792c375b8edb /modules
parentb27cac021f6f51c7605b29713f3293cdbcdc85f7 (diff)
downloadgitea-8693e544269e45e8415cb5c9ce350ed07a1e3bf6.tar.gz
gitea-8693e544269e45e8415cb5c9ce350ed07a1e3bf6.zip
Backport: Enable punctuations ending mentions (#8889) (#8894)
* Enable punctuations ending mentions * Improve tests
Diffstat (limited to 'modules')
-rw-r--r--modules/references/references.go2
-rw-r--r--modules/references/references_test.go47
2 files changed, 37 insertions, 12 deletions
diff --git a/modules/references/references.go b/modules/references/references.go
index 9c74d0d081..2e36eecec5 100644
--- a/modules/references/references.go
+++ b/modules/references/references.go
@@ -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
diff --git a/modules/references/references_test.go b/modules/references/references_test.go
index f8153ffe36..23403b42ba 100644
--- a/modules/references/references_test.go
+++ b/modules/references/references_test.go
@@ -204,14 +204,32 @@ 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",
@@ -219,17 +237,24 @@ func TestRegExp_mentionPattern(t *testing.T) {
"@",
"",
"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)
}
}