markup_test.go 15 KB

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