]> source.dussan.org Git - gitea.git/commitdiff
Enable punctuations ending mentions (#8889)
authorguillep2k <18600385+guillep2k@users.noreply.github.com>
Sat, 9 Nov 2019 22:12:05 +0000 (19:12 -0300)
committertechknowlogick <techknowlogick@gitea.io>
Sat, 9 Nov 2019 22:12:05 +0000 (17:12 -0500)
* Enable punctuations ending mentions

* Improve tests

modules/references/references.go
modules/references/references_test.go

index 58a8da28957b91f1d81c2852f2c32a1368b5244d..af0fe1aa0df35ea46454acdf8b050258630a4872 100644 (file)
@@ -27,7 +27,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
index 52e9b4ff524e8d5b3650a2e14ccc16b516fae2a8..d46c5e85d72a7237c4de61b8d052aaea41de12a8 100644 (file)
@@ -208,14 +208,32 @@ func testFixtures(t *testing.T, fixtures []testFixture, context string) {
 }
 
 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",
@@ -223,17 +241,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)
        }
 }