markup_test.go 13 KB

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