markup_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright 2017 The Gogs 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 markup_test
  5. import (
  6. "strings"
  7. "testing"
  8. . "github.com/smartystreets/goconvey/convey"
  9. "gogs.io/gogs/internal/conf"
  10. . "gogs.io/gogs/internal/markup"
  11. )
  12. func Test_IsReadmeFile(t *testing.T) {
  13. Convey("Detect README file extension", t, func() {
  14. testCases := []struct {
  15. ext string
  16. match bool
  17. }{
  18. {"readme", true},
  19. {"README", true},
  20. {"readme.md", true},
  21. {"readme.markdown", true},
  22. {"readme.mdown", true},
  23. {"readme.mkd", true},
  24. {"readme.org", true},
  25. {"readme.rst", true},
  26. {"readme.asciidoc", true},
  27. {"readme_ZH", true},
  28. }
  29. for _, tc := range testCases {
  30. So(IsReadmeFile(tc.ext), ShouldEqual, tc.match)
  31. }
  32. })
  33. }
  34. func Test_FindAllMentions(t *testing.T) {
  35. Convey("Find all mention patterns", t, func() {
  36. testCases := []struct {
  37. content string
  38. matches string
  39. }{
  40. {"@Unknwon, what do you think?", "Unknwon"},
  41. {"@Unknwon what do you think?", "Unknwon"},
  42. {"Hi @Unknwon, sounds good to me", "Unknwon"},
  43. {"cc/ @Unknwon @User", "Unknwon,User"},
  44. }
  45. for _, tc := range testCases {
  46. So(strings.Join(FindAllMentions(tc.content), ","), ShouldEqual, tc.matches)
  47. }
  48. })
  49. }
  50. func Test_RenderIssueIndexPattern(t *testing.T) {
  51. Convey("Rendering an issue reference", t, func() {
  52. var (
  53. urlPrefix = "/prefix"
  54. metas map[string]string = nil
  55. )
  56. conf.Server.SubpathDepth = 0
  57. Convey("To the internal issue tracker", func() {
  58. Convey("It should not render anything when there are no mentions", func() {
  59. testCases := []string{
  60. "",
  61. "this is a test",
  62. "test 123 123 1234",
  63. "#",
  64. "# # #",
  65. "# 123",
  66. "#abcd",
  67. "##1234",
  68. "test#1234",
  69. "#1234test",
  70. " test #1234test",
  71. }
  72. for i := 0; i < len(testCases); i++ {
  73. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  74. }
  75. })
  76. Convey("It should render freestanding mentions", func() {
  77. testCases := []string{
  78. "#1234 test", "<a href=\"/prefix/issues/1234\">#1234</a> test",
  79. "test #1234 issue", "test <a href=\"/prefix/issues/1234\">#1234</a> issue",
  80. "test issue #1234", "test issue <a href=\"/prefix/issues/1234\">#1234</a>",
  81. "#5 test", "<a href=\"/prefix/issues/5\">#5</a> test",
  82. "test #5 issue", "test <a href=\"/prefix/issues/5\">#5</a> issue",
  83. "test issue #5", "test issue <a href=\"/prefix/issues/5\">#5</a>",
  84. }
  85. for i := 0; i < len(testCases); i += 2 {
  86. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  87. }
  88. })
  89. Convey("It should not render issue mention without leading space", func() {
  90. input := []byte("test#54321 issue")
  91. expected := "test#54321 issue"
  92. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  93. })
  94. Convey("It should not render issue mention without trailing space", func() {
  95. input := []byte("test #54321issue")
  96. expected := "test #54321issue"
  97. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  98. })
  99. Convey("It should render issue mention in parentheses", func() {
  100. testCases := []string{
  101. "(#54321 issue)", "(<a href=\"/prefix/issues/54321\">#54321</a> issue)",
  102. "test (#54321) issue", "test (<a href=\"/prefix/issues/54321\">#54321</a>) issue",
  103. "test (#54321 extra) issue", "test (<a href=\"/prefix/issues/54321\">#54321</a> extra) issue",
  104. "test (#54321 issue)", "test (<a href=\"/prefix/issues/54321\">#54321</a> issue)",
  105. "test (#54321)", "test (<a href=\"/prefix/issues/54321\">#54321</a>)",
  106. }
  107. for i := 0; i < len(testCases); i += 2 {
  108. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  109. }
  110. })
  111. Convey("It should render issue mention in square brackets", func() {
  112. testCases := []string{
  113. "[#54321 issue]", "[<a href=\"/prefix/issues/54321\">#54321</a> issue]",
  114. "test [#54321] issue", "test [<a href=\"/prefix/issues/54321\">#54321</a>] issue",
  115. "test [#54321 extra] issue", "test [<a href=\"/prefix/issues/54321\">#54321</a> extra] issue",
  116. "test [#54321 issue]", "test [<a href=\"/prefix/issues/54321\">#54321</a> issue]",
  117. "test [#54321]", "test [<a href=\"/prefix/issues/54321\">#54321</a>]",
  118. }
  119. for i := 0; i < len(testCases); i += 2 {
  120. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  121. }
  122. })
  123. Convey("It should render multiple issue mentions in the same line", func() {
  124. testCases := []string{
  125. "#54321 #1243", "<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>",
  126. "test #54321 #1243", "test <a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>",
  127. "(#54321 #1243)", "(<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>)",
  128. "(#54321)(#1243)", "(<a href=\"/prefix/issues/54321\">#54321</a>)(<a href=\"/prefix/issues/1243\">#1243</a>)",
  129. "text #54321 test #1243 issue", "text <a href=\"/prefix/issues/54321\">#54321</a> test <a href=\"/prefix/issues/1243\">#1243</a> issue",
  130. "#1 (#4321) test", "<a href=\"/prefix/issues/1\">#1</a> (<a href=\"/prefix/issues/4321\">#4321</a>) test",
  131. }
  132. for i := 0; i < len(testCases); i += 2 {
  133. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  134. }
  135. })
  136. })
  137. Convey("To an external issue tracker with numeric style", func() {
  138. metas = make(map[string]string)
  139. metas["format"] = "https://someurl.com/{user}/{repo}/{index}"
  140. metas["user"] = "someuser"
  141. metas["repo"] = "somerepo"
  142. metas["style"] = ISSUE_NAME_STYLE_NUMERIC
  143. Convey("should not render anything when there are no mentions", func() {
  144. testCases := []string{
  145. "this is a test",
  146. "test 123 123 1234",
  147. "#",
  148. "# # #",
  149. "# 123",
  150. "#abcd",
  151. }
  152. for i := 0; i < len(testCases); i++ {
  153. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  154. }
  155. })
  156. Convey("It should render freestanding issue mentions", func() {
  157. testCases := []string{
  158. "#1234 test", "<a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> test",
  159. "test #1234 issue", "test <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> issue",
  160. "test issue #1234", "test issue <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a>",
  161. "#5 test", "<a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> test",
  162. "test #5 issue", "test <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> issue",
  163. "test issue #5", "test issue <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a>",
  164. }
  165. for i := 0; i < len(testCases); i += 2 {
  166. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  167. }
  168. })
  169. Convey("It should not render issue mention without leading space", func() {
  170. input := []byte("test#54321 issue")
  171. expected := "test#54321 issue"
  172. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  173. })
  174. Convey("It should not render issue mention without trailing space", func() {
  175. input := []byte("test #54321issue")
  176. expected := "test #54321issue"
  177. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  178. })
  179. Convey("It should render issue mention in parentheses", func() {
  180. testCases := []string{
  181. "(#54321 issue)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)",
  182. "test (#54321) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>) issue",
  183. "test (#54321 extra) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> extra) issue",
  184. "test (#54321 issue)", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)",
  185. "test (#54321)", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)",
  186. }
  187. for i := 0; i < len(testCases); i += 2 {
  188. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  189. }
  190. })
  191. Convey("It should render multiple issue mentions in the same line", func() {
  192. testCases := []string{
  193. "#54321 #1243", "<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>",
  194. "test #54321 #1243", "test <a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>",
  195. "(#54321 #1243)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)",
  196. "(#54321)(#1243)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)(<a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)",
  197. "text #54321 test #1243 issue", "text <a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> test <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a> issue",
  198. "#1 (#4321) test", "<a href=\"https://someurl.com/someuser/somerepo/1\">#1</a> (<a href=\"https://someurl.com/someuser/somerepo/4321\">#4321</a>) test",
  199. }
  200. for i := 0; i < len(testCases); i += 2 {
  201. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  202. }
  203. })
  204. })
  205. Convey("To an external issue tracker with alphanumeric style", func() {
  206. metas = make(map[string]string)
  207. metas["format"] = "https://someurl.com/{user}/{repo}/?b={index}"
  208. metas["user"] = "someuser"
  209. metas["repo"] = "somerepo"
  210. metas["style"] = ISSUE_NAME_STYLE_ALPHANUMERIC
  211. Convey("It should not render anything when there are no mentions", func() {
  212. testCases := []string{
  213. "",
  214. "this is a test",
  215. "test 123 123 1234",
  216. "#",
  217. "##1234",
  218. "# 123",
  219. "#abcd",
  220. "test #123",
  221. "abc-1234", // issue prefix must be capital
  222. "ABc-1234", // issue prefix must be _all_ capital
  223. "ABCDEFGHIJK-1234", // the limit is 10 characters in the prefix
  224. "ABC1234", // dash is required
  225. "test ABC- test", // number is required
  226. "test -1234 test", // prefix is required
  227. "testABC-123 test", // leading space is required
  228. "test ABC-123test", // trailing space is required
  229. "ABC-0123", // no leading zero
  230. }
  231. for i := 0; i < len(testCases); i += 2 {
  232. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  233. }
  234. })
  235. Convey("It should render freestanding issue mention", func() {
  236. testCases := []string{
  237. "OTT-1234 test", "<a href=\"https://someurl.com/someuser/somerepo/?b=OTT-1234\">OTT-1234</a> test",
  238. "test T-12 issue", "test <a href=\"https://someurl.com/someuser/somerepo/?b=T-12\">T-12</a> issue",
  239. "test issue ABCDEFGHIJ-1234567890", "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=ABCDEFGHIJ-1234567890\">ABCDEFGHIJ-1234567890</a>",
  240. "A-1 test", "<a href=\"https://someurl.com/someuser/somerepo/?b=A-1\">A-1</a> test",
  241. "test ZED-1 issue", "test <a href=\"https://someurl.com/someuser/somerepo/?b=ZED-1\">ZED-1</a> issue",
  242. "test issue DEED-7154", "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=DEED-7154\">DEED-7154</a>",
  243. }
  244. for i := 0; i < len(testCases); i += 2 {
  245. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  246. }
  247. })
  248. Convey("It should render issue mention in parentheses", func() {
  249. testCases := []string{
  250. "(ABG-124 issue)", "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)",
  251. "test (ABG-124) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>) issue",
  252. "test (ABG-124 extra) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> extra) issue",
  253. "test (ABG-124 issue)", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)",
  254. "test (ABG-124)", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>)",
  255. }
  256. for i := 0; i < len(testCases); i += 2 {
  257. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  258. }
  259. })
  260. Convey("It should render issue mention in square brackets", func() {
  261. testCases := []string{
  262. "[ABG-124] issue", "[<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>] issue",
  263. "test [ABG-124] issue", "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>] issue",
  264. "test [ABG-124 extra] issue", "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> extra] issue",
  265. "test [ABG-124 issue]", "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue]",
  266. "test [ABG-124]", "test [<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>]",
  267. }
  268. for i := 0; i < len(testCases); i += 2 {
  269. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  270. }
  271. })
  272. Convey("It should render multiple issue mentions in the same line", func() {
  273. testCases := []string{
  274. "ABG-124 OTT-4321", "<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>",
  275. "test ABG-124 OTT-4321", "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>",
  276. "(ABG-124 OTT-4321)", "(<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>)",
  277. "(ABG-124)(OTT-4321)", "(<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>)",
  278. "text ABG-124 test OTT-4321 issue", "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",
  279. "A-1 (RRE-345) test", "<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",
  280. }
  281. for i := 0; i < len(testCases); i += 2 {
  282. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  283. }
  284. })
  285. })
  286. })
  287. }