markup_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. . "github.com/gogits/gogs/modules/markup"
  10. "github.com/gogits/gogs/modules/setting"
  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. setting.AppSubUrlDepth = 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 multiple issue mentions in the same line", func() {
  112. testCases := []string{
  113. "#54321 #1243", "<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>",
  114. "test #54321 #1243", "test <a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>",
  115. "(#54321 #1243)", "(<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>)",
  116. "(#54321)(#1243)", "(<a href=\"/prefix/issues/54321\">#54321</a>)(<a href=\"/prefix/issues/1243\">#1243</a>)",
  117. "text #54321 test #1243 issue", "text <a href=\"/prefix/issues/54321\">#54321</a> test <a href=\"/prefix/issues/1243\">#1243</a> issue",
  118. "#1 (#4321) test", "<a href=\"/prefix/issues/1\">#1</a> (<a href=\"/prefix/issues/4321\">#4321</a>) test",
  119. }
  120. for i := 0; i < len(testCases); i += 2 {
  121. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  122. }
  123. })
  124. })
  125. Convey("To an external issue tracker with numeric style", func() {
  126. metas = make(map[string]string)
  127. metas["format"] = "https://someurl.com/{user}/{repo}/{index}"
  128. metas["user"] = "someuser"
  129. metas["repo"] = "somerepo"
  130. metas["style"] = ISSUE_NAME_STYLE_NUMERIC
  131. Convey("should not render anything when there are no mentions", func() {
  132. testCases := []string{
  133. "this is a test",
  134. "test 123 123 1234",
  135. "#",
  136. "# # #",
  137. "# 123",
  138. "#abcd",
  139. }
  140. for i := 0; i < len(testCases); i++ {
  141. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  142. }
  143. })
  144. Convey("It should render freestanding issue mentions", func() {
  145. testCases := []string{
  146. "#1234 test", "<a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> test",
  147. "test #1234 issue", "test <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> issue",
  148. "test issue #1234", "test issue <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a>",
  149. "#5 test", "<a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> test",
  150. "test #5 issue", "test <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> issue",
  151. "test issue #5", "test issue <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a>",
  152. }
  153. for i := 0; i < len(testCases); i += 2 {
  154. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  155. }
  156. })
  157. Convey("It should not render issue mention without leading space", func() {
  158. input := []byte("test#54321 issue")
  159. expected := "test#54321 issue"
  160. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  161. })
  162. Convey("It should not render issue mention without trailing space", func() {
  163. input := []byte("test #54321issue")
  164. expected := "test #54321issue"
  165. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  166. })
  167. Convey("It should render issue mention in parentheses", func() {
  168. testCases := []string{
  169. "(#54321 issue)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)",
  170. "test (#54321) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>) issue",
  171. "test (#54321 extra) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> extra) issue",
  172. "test (#54321 issue)", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)",
  173. "test (#54321)", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)",
  174. }
  175. for i := 0; i < len(testCases); i += 2 {
  176. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  177. }
  178. })
  179. Convey("It should render multiple issue mentions in the same line", func() {
  180. testCases := []string{
  181. "#54321 #1243", "<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>",
  182. "test #54321 #1243", "test <a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>",
  183. "(#54321 #1243)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)",
  184. "(#54321)(#1243)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)(<a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)",
  185. "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",
  186. "#1 (#4321) test", "<a href=\"https://someurl.com/someuser/somerepo/1\">#1</a> (<a href=\"https://someurl.com/someuser/somerepo/4321\">#4321</a>) test",
  187. }
  188. for i := 0; i < len(testCases); i += 2 {
  189. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  190. }
  191. })
  192. })
  193. Convey("To an external issue tracker with alphanumeric style", func() {
  194. metas = make(map[string]string)
  195. metas["format"] = "https://someurl.com/{user}/{repo}/?b={index}"
  196. metas["user"] = "someuser"
  197. metas["repo"] = "somerepo"
  198. metas["style"] = ISSUE_NAME_STYLE_ALPHANUMERIC
  199. Convey("It should not render anything when there are no mentions", func() {
  200. testCases := []string{
  201. "",
  202. "this is a test",
  203. "test 123 123 1234",
  204. "#",
  205. "##1234",
  206. "# 123",
  207. "#abcd",
  208. "test #123",
  209. "abc-1234", // issue prefix must be capital
  210. "ABc-1234", // issue prefix must be _all_ capital
  211. "ABCDEFGHIJK-1234", // the limit is 10 characters in the prefix
  212. "ABC1234", // dash is required
  213. "test ABC- test", // number is required
  214. "test -1234 test", // prefix is required
  215. "testABC-123 test", // leading space is required
  216. "test ABC-123test", // trailing space is required
  217. "ABC-0123", // no leading zero
  218. }
  219. for i := 0; i < len(testCases); i += 2 {
  220. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  221. }
  222. })
  223. Convey("It should render freestanding issue mention", func() {
  224. testCases := []string{
  225. "OTT-1234 test", "<a href=\"https://someurl.com/someuser/somerepo/?b=OTT-1234\">OTT-1234</a> test",
  226. "test T-12 issue", "test <a href=\"https://someurl.com/someuser/somerepo/?b=T-12\">T-12</a> issue",
  227. "test issue ABCDEFGHIJ-1234567890", "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=ABCDEFGHIJ-1234567890\">ABCDEFGHIJ-1234567890</a>",
  228. "A-1 test", "<a href=\"https://someurl.com/someuser/somerepo/?b=A-1\">A-1</a> test",
  229. "test ZED-1 issue", "test <a href=\"https://someurl.com/someuser/somerepo/?b=ZED-1\">ZED-1</a> issue",
  230. "test issue DEED-7154", "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=DEED-7154\">DEED-7154</a>",
  231. }
  232. for i := 0; i < len(testCases); i += 2 {
  233. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  234. }
  235. })
  236. Convey("It should render issue mention in parentheses", func() {
  237. testCases := []string{
  238. "(ABG-124 issue)", "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)",
  239. "test (ABG-124) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>) issue",
  240. "test (ABG-124 extra) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> extra) issue",
  241. "test (ABG-124 issue)", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)",
  242. "test (ABG-124)", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</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 multiple issue mentions in the same line", func() {
  249. testCases := []string{
  250. "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>",
  251. "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>",
  252. "(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>)",
  253. "(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>)",
  254. "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",
  255. "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",
  256. }
  257. for i := 0; i < len(testCases); i += 2 {
  258. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  259. }
  260. })
  261. })
  262. })
  263. }