Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

references_test.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 := []string{
  193. "@Unknwon",
  194. "@ANT_123",
  195. "@xxx-DiN0-z-A..uru..s-xxx",
  196. " @lol ",
  197. " @Te-st",
  198. "(@gitea)",
  199. "[@gitea]",
  200. }
  201. falseTestCases := []string{
  202. "@ 0",
  203. "@ ",
  204. "@",
  205. "",
  206. "ABC",
  207. "/home/gitea/@gitea",
  208. "\"@gitea\"",
  209. }
  210. for _, testCase := range trueTestCases {
  211. res := mentionPattern.MatchString(testCase)
  212. assert.True(t, res)
  213. }
  214. for _, testCase := range falseTestCases {
  215. res := mentionPattern.MatchString(testCase)
  216. assert.False(t, res)
  217. }
  218. }
  219. func TestRegExp_issueNumericPattern(t *testing.T) {
  220. trueTestCases := []string{
  221. "#1234",
  222. "#0",
  223. "#1234567890987654321",
  224. " #12",
  225. "#12:",
  226. "ref: #12: msg",
  227. }
  228. falseTestCases := []string{
  229. "# 1234",
  230. "# 0",
  231. "# ",
  232. "#",
  233. "#ABC",
  234. "#1A2B",
  235. "",
  236. "ABC",
  237. }
  238. for _, testCase := range trueTestCases {
  239. assert.True(t, issueNumericPattern.MatchString(testCase))
  240. }
  241. for _, testCase := range falseTestCases {
  242. assert.False(t, issueNumericPattern.MatchString(testCase))
  243. }
  244. }
  245. func TestRegExp_issueAlphanumericPattern(t *testing.T) {
  246. trueTestCases := []string{
  247. "ABC-1234",
  248. "A-1",
  249. "RC-80",
  250. "ABCDEFGHIJ-1234567890987654321234567890",
  251. "ABC-123.",
  252. "(ABC-123)",
  253. "[ABC-123]",
  254. "ABC-123:",
  255. }
  256. falseTestCases := []string{
  257. "RC-08",
  258. "PR-0",
  259. "ABCDEFGHIJK-1",
  260. "PR_1",
  261. "",
  262. "#ABC",
  263. "",
  264. "ABC",
  265. "GG-",
  266. "rm-1",
  267. "/home/gitea/ABC-1234",
  268. "MY-STRING-ABC-123",
  269. }
  270. for _, testCase := range trueTestCases {
  271. assert.True(t, issueAlphanumericPattern.MatchString(testCase))
  272. }
  273. for _, testCase := range falseTestCases {
  274. assert.False(t, issueAlphanumericPattern.MatchString(testCase))
  275. }
  276. }