1
0

markup_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package markup_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. . "gogs.io/gogs/internal/markup"
  6. )
  7. func Test_IsReadmeFile(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. expVal bool
  11. }{
  12. {name: "readme", expVal: true},
  13. {name: "README", expVal: true},
  14. {name: "readme.md", expVal: true},
  15. {name: "readme.markdown", expVal: true},
  16. {name: "readme.mdown", expVal: true},
  17. {name: "readme.mkd", expVal: true},
  18. {name: "readme.org", expVal: true},
  19. {name: "readme.rst", expVal: true},
  20. {name: "readme.asciidoc", expVal: true},
  21. {name: "readme_ZH", expVal: true},
  22. }
  23. for _, test := range tests {
  24. t.Run(test.name, func(t *testing.T) {
  25. assert.Equal(t, test.expVal, IsReadmeFile(test.name))
  26. })
  27. }
  28. }
  29. func Test_FindAllMentions(t *testing.T) {
  30. tests := []struct {
  31. input string
  32. expMatches []string
  33. }{
  34. {input: "@unknwon, what do you think?", expMatches: []string{"unknwon"}},
  35. {input: "@unknwon what do you think?", expMatches: []string{"unknwon"}},
  36. {input: "Hi @unknwon, sounds good to me", expMatches: []string{"unknwon"}},
  37. {input: "cc/ @unknwon @eddycjy", expMatches: []string{"unknwon", "eddycjy"}},
  38. }
  39. for _, test := range tests {
  40. t.Run("", func(t *testing.T) {
  41. assert.Equal(t, test.expMatches, FindAllMentions(test.input))
  42. })
  43. }
  44. }
  45. func Test_RenderIssueIndexPattern(t *testing.T) {
  46. urlPrefix := "/prefix"
  47. t.Run("render to internal issue tracker", func(t *testing.T) {
  48. tests := []struct {
  49. input string
  50. expVal string
  51. }{
  52. {input: "", expVal: ""},
  53. {input: "this is a test", expVal: "this is a test"},
  54. {input: "test 123 123 1234", expVal: "test 123 123 1234"},
  55. {input: "#", expVal: "#"},
  56. {input: "# # #", expVal: "# # #"},
  57. {input: "# 123", expVal: "# 123"},
  58. {input: "#abcd", expVal: "#abcd"},
  59. {input: "##1234", expVal: "##1234"},
  60. {input: "test#1234", expVal: "test#1234"},
  61. {input: "#1234test", expVal: "#1234test"},
  62. {input: " test #1234test", expVal: " test #1234test"},
  63. {input: "#1234 test", expVal: "<a href=\"/prefix/issues/1234\">#1234</a> test"},
  64. {input: "test #1234 issue", expVal: "test <a href=\"/prefix/issues/1234\">#1234</a> issue"},
  65. {input: "test issue #1234", expVal: "test issue <a href=\"/prefix/issues/1234\">#1234</a>"},
  66. {input: "#5 test", expVal: "<a href=\"/prefix/issues/5\">#5</a> test"},
  67. {input: "test #5 issue", expVal: "test <a href=\"/prefix/issues/5\">#5</a> issue"},
  68. {input: "test issue #5", expVal: "test issue <a href=\"/prefix/issues/5\">#5</a>"},
  69. {input: "(#54321 issue)", expVal: "(<a href=\"/prefix/issues/54321\">#54321</a> issue)"},
  70. {input: "test (#54321) issue", expVal: "test (<a href=\"/prefix/issues/54321\">#54321</a>) issue"},
  71. {input: "test (#54321 extra) issue", expVal: "test (<a href=\"/prefix/issues/54321\">#54321</a> extra) issue"},
  72. {input: "test (#54321 issue)", expVal: "test (<a href=\"/prefix/issues/54321\">#54321</a> issue)"},
  73. {input: "test (#54321)", expVal: "test (<a href=\"/prefix/issues/54321\">#54321</a>)"},
  74. {input: "[#54321 issue]", expVal: "[<a href=\"/prefix/issues/54321\">#54321</a> issue]"},
  75. {input: "test [#54321] issue", expVal: "test [<a href=\"/prefix/issues/54321\">#54321</a>] issue"},
  76. {input: "test [#54321 extra] issue", expVal: "test [<a href=\"/prefix/issues/54321\">#54321</a> extra] issue"},
  77. {input: "test [#54321 issue]", expVal: "test [<a href=\"/prefix/issues/54321\">#54321</a> issue]"},
  78. {input: "test [#54321]", expVal: "test [<a href=\"/prefix/issues/54321\">#54321</a>]"},
  79. {input: "#54321 #1243", expVal: "<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>"},
  80. {input: "test #54321 #1243", expVal: "test <a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>"},
  81. {input: "(#54321 #1243)", expVal: "(<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>)"},
  82. {input: "(#54321)(#1243)", expVal: "(<a href=\"/prefix/issues/54321\">#54321</a>)(<a href=\"/prefix/issues/1243\">#1243</a>)"},
  83. {input: "text #54321 test #1243 issue", expVal: "text <a href=\"/prefix/issues/54321\">#54321</a> test <a href=\"/prefix/issues/1243\">#1243</a> issue"},
  84. {input: "#1 (#4321) test", expVal: "<a href=\"/prefix/issues/1\">#1</a> (<a href=\"/prefix/issues/4321\">#4321</a>) test"},
  85. }
  86. for _, test := range tests {
  87. t.Run(test.input, func(t *testing.T) {
  88. assert.Equal(t, test.expVal, string(RenderIssueIndexPattern([]byte(test.input), urlPrefix, nil)))
  89. })
  90. }
  91. })
  92. t.Run("render to external issue tracker", func(t *testing.T) {
  93. t.Run("numeric style", func(t *testing.T) {
  94. metas := map[string]string{
  95. "format": "https://someurl.com/{user}/{repo}/{index}",
  96. "user": "someuser",
  97. "repo": "somerepo",
  98. "style": IssueNameStyleNumeric,
  99. }
  100. tests := []struct {
  101. input string
  102. expVal string
  103. }{
  104. {input: "this is a test", expVal: "this is a test"},
  105. {input: "test 123 123 1234", expVal: "test 123 123 1234"},
  106. {input: "#", expVal: "#"},
  107. {input: "# # #", expVal: "# # #"},
  108. {input: "# 123", expVal: "# 123"},
  109. {input: "#abcd", expVal: "#abcd"},
  110. {input: "#1234 test", expVal: "<a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> test"},
  111. {input: "test #1234 issue", expVal: "test <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> issue"},
  112. {input: "test issue #1234", expVal: "test issue <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a>"},
  113. {input: "#5 test", expVal: "<a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> test"},
  114. {input: "test #5 issue", expVal: "test <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> issue"},
  115. {input: "test issue #5", expVal: "test issue <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a>"},
  116. {input: "(#54321 issue)", expVal: "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)"},
  117. {input: "test (#54321) issue", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>) issue"},
  118. {input: "test (#54321 extra) issue", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> extra) issue"},
  119. {input: "test (#54321 issue)", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)"},
  120. {input: "test (#54321)", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)"},
  121. {input: "#54321 #1243", expVal: "<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>"},
  122. {input: "test #54321 #1243", expVal: "test <a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>"},
  123. {input: "(#54321 #1243)", expVal: "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)"},
  124. {input: "(#54321)(#1243)", expVal: "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)(<a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)"},
  125. {input: "text #54321 test #1243 issue", expVal: "text <a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> test <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a> issue"},
  126. {input: "#1 (#4321) test", expVal: "<a href=\"https://someurl.com/someuser/somerepo/1\">#1</a> (<a href=\"https://someurl.com/someuser/somerepo/4321\">#4321</a>) test"},
  127. }
  128. for _, test := range tests {
  129. t.Run(test.input, func(t *testing.T) {
  130. assert.Equal(t, test.expVal, string(RenderIssueIndexPattern([]byte(test.input), urlPrefix, metas)))
  131. })
  132. }
  133. })
  134. t.Run("alphanumeric style", func(t *testing.T) {
  135. metas := map[string]string{
  136. "format": "https://someurl.com/{user}/{repo}/?b={index}",
  137. "user": "someuser",
  138. "repo": "somerepo",
  139. "style": IssueNameStyleAlphanumeric,
  140. }
  141. tests := []struct {
  142. input string
  143. expVal string
  144. }{
  145. {input: "", expVal: ""},
  146. {input: "this is a test", expVal: "this is a test"},
  147. {input: "test 123 123 1234", expVal: "test 123 123 1234"},
  148. {input: "#", expVal: "#"},
  149. {input: "##1234", expVal: "##1234"},
  150. {input: "# 123", expVal: "# 123"},
  151. {input: "#abcd", expVal: "#abcd"},
  152. {input: "test #123", expVal: "test #123"},
  153. {input: "abc-1234", expVal: "abc-1234"}, // issue prefix must be capital
  154. {input: "ABc-1234", expVal: "ABc-1234"}, // issue prefix must be _all_ capital
  155. {input: "ABCDEFGHIJK-1234", expVal: "ABCDEFGHIJK-1234"}, // the limit is 10 characters in the prefix
  156. {input: "ABC1234", expVal: "ABC1234"}, // dash is required
  157. {input: "test ABC- test", expVal: "test ABC- test"}, // number is required
  158. {input: "test -1234 test", expVal: "test -1234 test"}, // prefix is required
  159. {input: "testABC-123 test", expVal: "testABC-123 test"}, // leading space is required
  160. {input: "test ABC-123test", expVal: "test ABC-123test"}, // trailing space is required
  161. {input: "ABC-0123", expVal: "ABC-0123"}, // no leading zero
  162. {input: "OTT-1234 test", expVal: "<a href=\"https://someurl.com/someuser/somerepo/?b=OTT-1234\">OTT-1234</a> test"},
  163. {input: "test T-12 issue", expVal: "test <a href=\"https://someurl.com/someuser/somerepo/?b=T-12\">T-12</a> issue"},
  164. {input: "test issue ABCDEFGHIJ-1234567890", expVal: "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=ABCDEFGHIJ-1234567890\">ABCDEFGHIJ-1234567890</a>"},
  165. {input: "A-1 test", expVal: "<a href=\"https://someurl.com/someuser/somerepo/?b=A-1\">A-1</a> test"},
  166. {input: "test ZED-1 issue", expVal: "test <a href=\"https://someurl.com/someuser/somerepo/?b=ZED-1\">ZED-1</a> issue"},
  167. {input: "test issue DEED-7154", expVal: "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=DEED-7154\">DEED-7154</a>"},
  168. {input: "(ABG-124 issue)", expVal: "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)"},
  169. {input: "test (ABG-124) issue", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>) issue"},
  170. {input: "test (ABG-124 extra) issue", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> extra) issue"},
  171. {input: "test (ABG-124 issue)", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)"},
  172. {input: "test (ABG-124)", expVal: "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>)"},
  173. {input: "[ABG-124] issue", expVal: "[<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>] issue"},
  174. {input: "test [ABG-124] issue", expVal: "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>] issue"},
  175. {input: "test [ABG-124 extra] issue", expVal: "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> extra] issue"},
  176. {input: "test [ABG-124 issue]", expVal: "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue]"},
  177. {input: "test [ABG-124]", expVal: "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>]"},
  178. {input: "ABG-124 OTT-4321", expVal: "<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>"},
  179. {input: "test ABG-124 OTT-4321", expVal: "test <a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>"},
  180. {input: "(ABG-124 OTT-4321)", expVal: "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>)"},
  181. {input: "(ABG-124)(OTT-4321)", expVal: "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>)(<a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>)"},
  182. {input: "text ABG-124 test OTT-4321 issue", expVal: "text <a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> test <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a> issue"},
  183. {input: "A-1 (RRE-345) test", expVal: "<a href=\"https://someurl.com/someuser/somerepo/?b=A-1\">A-1</a> (<a href=\"https://someurl.com/someuser/somerepo/?b=RRE-345\">RRE-345</a>) test"},
  184. }
  185. for _, test := range tests {
  186. t.Run(test.input, func(t *testing.T) {
  187. assert.Equal(t, test.expVal, string(RenderIssueIndexPattern([]byte(test.input), urlPrefix, metas)))
  188. })
  189. }
  190. })
  191. })
  192. }
  193. func TestRenderSha1CurrentPattern(t *testing.T) {
  194. metas := map[string]string{
  195. "repoLink": "/someuser/somerepo",
  196. }
  197. tests := []struct {
  198. desc string
  199. input string
  200. prefix string
  201. expVal string
  202. }{
  203. {
  204. desc: "Full SHA (40 symbols)",
  205. input: "ad8ced4f57d9068cb2874557245be3c7f341149d",
  206. prefix: metas["repoLink"],
  207. expVal: `<a href="/someuser/somerepo/commit/ad8ced4f57d9068cb2874557245be3c7f341149d"><code>ad8ced4f57</code></a>`,
  208. },
  209. {
  210. desc: "Short SHA (8 symbols)",
  211. input: "ad8ced4f",
  212. prefix: metas["repoLink"],
  213. expVal: `<a href="/someuser/somerepo/commit/ad8ced4f"><code>ad8ced4f</code></a>`,
  214. },
  215. {
  216. desc: "9 digits",
  217. input: "123456789",
  218. prefix: metas["repoLink"],
  219. expVal: "123456789",
  220. },
  221. }
  222. for _, test := range tests {
  223. t.Run(test.desc, func(t *testing.T) {
  224. assert.Equal(t, test.expVal, string(RenderSha1CurrentPattern([]byte(test.input), test.prefix)))
  225. })
  226. }
  227. }