You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

references_test.go 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package references
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestFindAllIssueReferences(t *testing.T) {
  11. type result struct {
  12. Index int64
  13. Owner string
  14. Name string
  15. Issue string
  16. Action XRefAction
  17. RefLocation *RefSpan
  18. ActionLocation *RefSpan
  19. }
  20. type testFixture struct {
  21. input string
  22. expected []result
  23. }
  24. fixtures := []testFixture{
  25. {
  26. "Simply closes: #29 yes",
  27. []result{
  28. {29, "", "", "29", XRefActionCloses, &RefSpan{Start: 15, End: 18}, &RefSpan{Start: 7, End: 13}},
  29. },
  30. },
  31. {
  32. "#123 no, this is a title.",
  33. []result{},
  34. },
  35. {
  36. " #124 yes, this is a reference.",
  37. []result{
  38. {124, "", "", "124", XRefActionNone, &RefSpan{Start: 0, End: 4}, nil},
  39. },
  40. },
  41. {
  42. "```\nThis is a code block.\n#723 no, it's a code block.```",
  43. []result{},
  44. },
  45. {
  46. "This `#724` no, it's inline code.",
  47. []result{},
  48. },
  49. {
  50. "This user3/repo4#200 yes.",
  51. []result{
  52. {200, "user3", "repo4", "200", XRefActionNone, &RefSpan{Start: 5, End: 20}, nil},
  53. },
  54. },
  55. {
  56. "This [one](#919) no, this is a URL fragment.",
  57. []result{},
  58. },
  59. {
  60. "This [two](/user2/repo1/issues/921) yes.",
  61. []result{
  62. {921, "user2", "repo1", "921", XRefActionNone, nil, nil},
  63. },
  64. },
  65. {
  66. "This [three](/user2/repo1/pulls/922) yes.",
  67. []result{
  68. {922, "user2", "repo1", "922", XRefActionNone, nil, nil},
  69. },
  70. },
  71. {
  72. "This [four](http://gitea.com:3000/user3/repo4/issues/203) yes.",
  73. []result{
  74. {203, "user3", "repo4", "203", XRefActionNone, nil, nil},
  75. },
  76. },
  77. {
  78. "This [five](http://github.com/user3/repo4/issues/204) no.",
  79. []result{},
  80. },
  81. {
  82. "This http://gitea.com:3000/user4/repo5/201 no, bad URL.",
  83. []result{},
  84. },
  85. {
  86. "This http://gitea.com:3000/user4/repo5/pulls/202 yes.",
  87. []result{
  88. {202, "user4", "repo5", "202", XRefActionNone, nil, nil},
  89. },
  90. },
  91. {
  92. "This http://GiTeA.COM:3000/user4/repo6/pulls/205 yes.",
  93. []result{
  94. {205, "user4", "repo6", "205", XRefActionNone, nil, nil},
  95. },
  96. },
  97. {
  98. "Reopens #15 yes",
  99. []result{
  100. {15, "", "", "15", XRefActionReopens, &RefSpan{Start: 8, End: 11}, &RefSpan{Start: 0, End: 7}},
  101. },
  102. },
  103. {
  104. "This closes #20 for you yes",
  105. []result{
  106. {20, "", "", "20", XRefActionCloses, &RefSpan{Start: 12, End: 15}, &RefSpan{Start: 5, End: 11}},
  107. },
  108. },
  109. {
  110. "Do you fix user6/repo6#300 ? yes",
  111. []result{
  112. {300, "user6", "repo6", "300", XRefActionCloses, &RefSpan{Start: 11, End: 26}, &RefSpan{Start: 7, End: 10}},
  113. },
  114. },
  115. {
  116. "For 999 #1235 no keyword, but yes",
  117. []result{
  118. {1235, "", "", "1235", XRefActionNone, &RefSpan{Start: 8, End: 13}, nil},
  119. },
  120. },
  121. {
  122. "Which abc. #9434 same as above",
  123. []result{
  124. {9434, "", "", "9434", XRefActionNone, &RefSpan{Start: 11, End: 16}, nil},
  125. },
  126. },
  127. {
  128. "This closes #600 and reopens #599",
  129. []result{
  130. {600, "", "", "600", XRefActionCloses, &RefSpan{Start: 12, End: 16}, &RefSpan{Start: 5, End: 11}},
  131. {599, "", "", "599", XRefActionReopens, &RefSpan{Start: 29, End: 33}, &RefSpan{Start: 21, End: 28}},
  132. },
  133. },
  134. }
  135. // Save original value for other tests that may rely on it
  136. prevURL := setting.AppURL
  137. setting.AppURL = "https://gitea.com:3000/"
  138. for _, fixture := range fixtures {
  139. expraw := make([]*rawReference, len(fixture.expected))
  140. for i, e := range fixture.expected {
  141. expraw[i] = &rawReference{
  142. index: e.Index,
  143. owner: e.Owner,
  144. name: e.Name,
  145. action: e.Action,
  146. issue: e.Issue,
  147. refLocation: e.RefLocation,
  148. actionLocation: e.ActionLocation,
  149. }
  150. }
  151. expref := rawToIssueReferenceList(expraw)
  152. refs := FindAllIssueReferencesMarkdown(fixture.input)
  153. assert.EqualValues(t, expref, refs, "Failed to parse: {%s}", fixture.input)
  154. rawrefs := findAllIssueReferencesMarkdown(fixture.input)
  155. assert.EqualValues(t, expraw, rawrefs, "Failed to parse: {%s}", fixture.input)
  156. }
  157. // Restore for other tests that may rely on the original value
  158. setting.AppURL = prevURL
  159. type alnumFixture struct {
  160. input string
  161. issue string
  162. refLocation *RefSpan
  163. action XRefAction
  164. actionLocation *RefSpan
  165. }
  166. alnumFixtures := []alnumFixture{
  167. {
  168. "This ref ABC-123 is alphanumeric",
  169. "ABC-123", &RefSpan{Start: 9, End: 16},
  170. XRefActionNone, nil,
  171. },
  172. {
  173. "This closes ABCD-1234 alphanumeric",
  174. "ABCD-1234", &RefSpan{Start: 12, End: 21},
  175. XRefActionCloses, &RefSpan{Start: 5, End: 11},
  176. },
  177. }
  178. for _, fixture := range alnumFixtures {
  179. found, ref := FindRenderizableReferenceAlphanumeric(fixture.input)
  180. if fixture.issue == "" {
  181. assert.False(t, found, "Failed to parse: {%s}", fixture.input)
  182. } else {
  183. assert.True(t, found, "Failed to parse: {%s}", fixture.input)
  184. assert.Equal(t, fixture.issue, ref.Issue, "Failed to parse: {%s}", fixture.input)
  185. assert.Equal(t, fixture.refLocation, ref.RefLocation, "Failed to parse: {%s}", fixture.input)
  186. assert.Equal(t, fixture.action, ref.Action, "Failed to parse: {%s}", fixture.input)
  187. assert.Equal(t, fixture.actionLocation, ref.ActionLocation, "Failed to parse: {%s}", fixture.input)
  188. }
  189. }
  190. }
  191. func TestRegExp_mentionPattern(t *testing.T) {
  192. trueTestCases := []struct {
  193. pat string
  194. exp string
  195. }{
  196. {"@Unknwon", "@Unknwon"},
  197. {"@ANT_123", "@ANT_123"},
  198. {"@xxx-DiN0-z-A..uru..s-xxx", "@xxx-DiN0-z-A..uru..s-xxx"},
  199. {" @lol ", "@lol"},
  200. {" @Te-st", "@Te-st"},
  201. {"(@gitea)", "@gitea"},
  202. {"[@gitea]", "@gitea"},
  203. {"@gitea! this", "@gitea"},
  204. {"@gitea? this", "@gitea"},
  205. {"@gitea. this", "@gitea"},
  206. {"@gitea, this", "@gitea"},
  207. {"@gitea; this", "@gitea"},
  208. {"@gitea!\nthis", "@gitea"},
  209. {"\n@gitea?\nthis", "@gitea"},
  210. {"\t@gitea.\nthis", "@gitea"},
  211. {"@gitea,\nthis", "@gitea"},
  212. {"@gitea;\nthis", "@gitea"},
  213. {"@gitea!", "@gitea"},
  214. {"@gitea?", "@gitea"},
  215. {"@gitea.", "@gitea"},
  216. {"@gitea,", "@gitea"},
  217. {"@gitea;", "@gitea"},
  218. }
  219. falseTestCases := []string{
  220. "@ 0",
  221. "@ ",
  222. "@",
  223. "",
  224. "ABC",
  225. "@.ABC",
  226. "/home/gitea/@gitea",
  227. "\"@gitea\"",
  228. "@@gitea",
  229. "@gitea!this",
  230. "@gitea?this",
  231. "@gitea,this",
  232. "@gitea;this",
  233. }
  234. for _, testCase := range trueTestCases {
  235. found := mentionPattern.FindStringSubmatch(testCase.pat)
  236. assert.Len(t, found, 2)
  237. assert.Equal(t, testCase.exp, found[1])
  238. }
  239. for _, testCase := range falseTestCases {
  240. res := mentionPattern.MatchString(testCase)
  241. assert.False(t, res, "[%s] should be false", testCase)
  242. }
  243. }
  244. func TestRegExp_issueNumericPattern(t *testing.T) {
  245. trueTestCases := []string{
  246. "#1234",
  247. "#0",
  248. "#1234567890987654321",
  249. " #12",
  250. "#12:",
  251. "ref: #12: msg",
  252. }
  253. falseTestCases := []string{
  254. "# 1234",
  255. "# 0",
  256. "# ",
  257. "#",
  258. "#ABC",
  259. "#1A2B",
  260. "",
  261. "ABC",
  262. }
  263. for _, testCase := range trueTestCases {
  264. assert.True(t, issueNumericPattern.MatchString(testCase))
  265. }
  266. for _, testCase := range falseTestCases {
  267. assert.False(t, issueNumericPattern.MatchString(testCase))
  268. }
  269. }
  270. func TestRegExp_issueAlphanumericPattern(t *testing.T) {
  271. trueTestCases := []string{
  272. "ABC-1234",
  273. "A-1",
  274. "RC-80",
  275. "ABCDEFGHIJ-1234567890987654321234567890",
  276. "ABC-123.",
  277. "(ABC-123)",
  278. "[ABC-123]",
  279. "ABC-123:",
  280. }
  281. falseTestCases := []string{
  282. "RC-08",
  283. "PR-0",
  284. "ABCDEFGHIJK-1",
  285. "PR_1",
  286. "",
  287. "#ABC",
  288. "",
  289. "ABC",
  290. "GG-",
  291. "rm-1",
  292. "/home/gitea/ABC-1234",
  293. "MY-STRING-ABC-123",
  294. }
  295. for _, testCase := range trueTestCases {
  296. assert.True(t, issueAlphanumericPattern.MatchString(testCase))
  297. }
  298. for _, testCase := range falseTestCases {
  299. assert.False(t, issueAlphanumericPattern.MatchString(testCase))
  300. }
  301. }