From 7583954c83ccb0138ce6b715281a15469c70a4d3 Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Fri, 8 Nov 2019 21:46:35 -0300 Subject: [PATCH 1/2] Enable punctuations ending mentions --- modules/references/references.go | 2 +- modules/references/references_test.go | 46 ++++++++++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/modules/references/references.go b/modules/references/references.go index 58a8da28957b..af0fe1aa0df3 100644 --- a/modules/references/references.go +++ b/modules/references/references.go @@ -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 diff --git a/modules/references/references_test.go b/modules/references/references_test.go index 52e9b4ff524e..8afe50c91ae8 100644 --- a/modules/references/references_test.go +++ b/modules/references/references_test.go @@ -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,23 @@ func TestRegExp_mentionPattern(t *testing.T) { "@", "", "ABC", + "@.ABC", "/home/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], "[%s] should be [%s]", found[1], testCase.exp) } for _, testCase := range falseTestCases { res := mentionPattern.MatchString(testCase) - assert.False(t, res) + assert.False(t, res, "[%s] should be false", testCase) } } From 8d9bf0e7f9e79cf7c13e706f3ca21e53d88c650a Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Sat, 9 Nov 2019 13:17:09 -0300 Subject: [PATCH 2/2] Improve tests --- modules/references/references_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/references/references_test.go b/modules/references/references_test.go index 8afe50c91ae8..d46c5e85d72a 100644 --- a/modules/references/references_test.go +++ b/modules/references/references_test.go @@ -218,7 +218,7 @@ func TestRegExp_mentionPattern(t *testing.T) { {" @lol ", "@lol"}, {" @Te-st", "@Te-st"}, {"(@gitea)", "@gitea"}, - {"(@gitea)", "@gitea"}, + {"[@gitea]", "@gitea"}, {"@gitea! this", "@gitea"}, {"@gitea? this", "@gitea"}, {"@gitea. this", "@gitea"}, @@ -244,6 +244,7 @@ func TestRegExp_mentionPattern(t *testing.T) { "@.ABC", "/home/gitea/@gitea", "\"@gitea\"", + "@@gitea", "@gitea!this", "@gitea?this", "@gitea,this", @@ -253,7 +254,7 @@ func TestRegExp_mentionPattern(t *testing.T) { for _, testCase := range trueTestCases { found := mentionPattern.FindStringSubmatch(testCase.pat) assert.Len(t, found, 2) - assert.Equal(t, testCase.exp, found[1], "[%s] should be [%s]", found[1], testCase.exp) + assert.Equal(t, testCase.exp, found[1]) } for _, testCase := range falseTestCases { res := mentionPattern.MatchString(testCase)