Skip to content

Commit bb04fb5

Browse files
guillep2ktechknowlogick
authored andcommitted
Enable punctuations ending mentions (#8889)
* Enable punctuations ending mentions * Improve tests
1 parent c541451 commit bb04fb5

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

modules/references/references.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727
// TODO: fix invalid linking issue
2828

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

modules/references/references_test.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -208,32 +208,57 @@ func testFixtures(t *testing.T, fixtures []testFixture, context string) {
208208
}
209209

210210
func TestRegExp_mentionPattern(t *testing.T) {
211-
trueTestCases := []string{
212-
"@Unknwon",
213-
"@ANT_123",
214-
"@xxx-DiN0-z-A..uru..s-xxx",
215-
" @lol ",
216-
" @Te-st",
217-
"(@gitea)",
218-
"[@gitea]",
211+
trueTestCases := []struct {
212+
pat string
213+
exp string
214+
}{
215+
{"@Unknwon", "@Unknwon"},
216+
{"@ANT_123", "@ANT_123"},
217+
{"@xxx-DiN0-z-A..uru..s-xxx", "@xxx-DiN0-z-A..uru..s-xxx"},
218+
{" @lol ", "@lol"},
219+
{" @Te-st", "@Te-st"},
220+
{"(@gitea)", "@gitea"},
221+
{"[@gitea]", "@gitea"},
222+
{"@gitea! this", "@gitea"},
223+
{"@gitea? this", "@gitea"},
224+
{"@gitea. this", "@gitea"},
225+
{"@gitea, this", "@gitea"},
226+
{"@gitea; this", "@gitea"},
227+
{"@gitea!\nthis", "@gitea"},
228+
{"\n@gitea?\nthis", "@gitea"},
229+
{"\t@gitea.\nthis", "@gitea"},
230+
{"@gitea,\nthis", "@gitea"},
231+
{"@gitea;\nthis", "@gitea"},
232+
{"@gitea!", "@gitea"},
233+
{"@gitea?", "@gitea"},
234+
{"@gitea.", "@gitea"},
235+
{"@gitea,", "@gitea"},
236+
{"@gitea;", "@gitea"},
219237
}
220238
falseTestCases := []string{
221239
"@ 0",
222240
"@ ",
223241
"@",
224242
"",
225243
"ABC",
244+
"@.ABC",
226245
"/home/gitea/@gitea",
227246
"\"@gitea\"",
247+
"@@gitea",
248+
"@gitea!this",
249+
"@gitea?this",
250+
"@gitea,this",
251+
"@gitea;this",
228252
}
229253

230254
for _, testCase := range trueTestCases {
231-
res := mentionPattern.MatchString(testCase)
232-
assert.True(t, res)
255+
found := mentionPattern.FindStringSubmatch(testCase.pat)
256+
assert.Len(t, found, 2)
257+
assert.Equal(t, testCase.exp, found[1])
233258
}
234259
for _, testCase := range falseTestCases {
235260
res := mentionPattern.MatchString(testCase)
236-
assert.False(t, res)
261+
assert.False(t, res, "[%s] should be false", testCase)
237262
}
238263
}
239264

0 commit comments

Comments
 (0)