You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown_test.go 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // Copyright 2017 The Gitea 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 markdown_test
  5. import (
  6. "fmt"
  7. "strconv"
  8. "testing"
  9. "strings"
  10. . "code.gitea.io/gitea/modules/markdown"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. const AppURL = "http://localhost:3000/"
  15. const Repo = "gogits/gogs"
  16. const AppSubURL = AppURL + Repo + "/"
  17. var numericMetas = map[string]string{
  18. "format": "https://someurl.com/{user}/{repo}/{index}",
  19. "user": "someUser",
  20. "repo": "someRepo",
  21. "style": IssueNameStyleNumeric,
  22. }
  23. var alphanumericMetas = map[string]string{
  24. "format": "https://someurl.com/{user}/{repo}/{index}",
  25. "user": "someUser",
  26. "repo": "someRepo",
  27. "style": IssueNameStyleAlphanumeric,
  28. }
  29. // numericLink an HTML to a numeric-style issue
  30. func numericIssueLink(baseURL string, index int) string {
  31. return link(URLJoin(baseURL, strconv.Itoa(index)), fmt.Sprintf("#%d", index))
  32. }
  33. // alphanumLink an HTML link to an alphanumeric-style issue
  34. func alphanumIssueLink(baseURL string, name string) string {
  35. return link(URLJoin(baseURL, name), name)
  36. }
  37. // urlContentsLink an HTML link whose contents is the target URL
  38. func urlContentsLink(href string) string {
  39. return link(href, href)
  40. }
  41. // link an HTML link
  42. func link(href, contents string) string {
  43. return fmt.Sprintf("<a href=\"%s\">%s</a>", href, contents)
  44. }
  45. func testRenderIssueIndexPattern(t *testing.T, input, expected string, metas map[string]string) {
  46. assert.Equal(t, expected,
  47. string(RenderIssueIndexPattern([]byte(input), AppSubURL, metas)))
  48. }
  49. func TestURLJoin(t *testing.T) {
  50. type test struct {
  51. Expected string
  52. Base string
  53. Elements []string
  54. }
  55. newTest := func(expected, base string, elements ...string) test {
  56. return test{Expected: expected, Base: base, Elements: elements}
  57. }
  58. for _, test := range []test{
  59. newTest("https://try.gitea.io/a/b/c",
  60. "https://try.gitea.io", "a/b", "c"),
  61. newTest("https://try.gitea.io/a/b/c",
  62. "https://try.gitea.io/", "/a/b/", "/c/"),
  63. newTest("https://try.gitea.io/a/c",
  64. "https://try.gitea.io/", "/a/./b/", "../c/"),
  65. newTest("a/b/c",
  66. "a", "b/c/"),
  67. newTest("a/b/d",
  68. "a/", "b/c/", "/../d/"),
  69. } {
  70. assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
  71. }
  72. }
  73. func TestRender_IssueIndexPattern(t *testing.T) {
  74. // numeric: render inputs without valid mentions
  75. test := func(s string) {
  76. testRenderIssueIndexPattern(t, s, s, nil)
  77. testRenderIssueIndexPattern(t, s, s, numericMetas)
  78. }
  79. // should not render anything when there are no mentions
  80. test("")
  81. test("this is a test")
  82. test("test 123 123 1234")
  83. test("#")
  84. test("# # #")
  85. test("# 123")
  86. test("#abcd")
  87. test("##1234")
  88. test("test#1234")
  89. test("#1234test")
  90. test(" test #1234test")
  91. // should not render issue mention without leading space
  92. test("test#54321 issue")
  93. // should not render issue mention without trailing space
  94. test("test #54321issue")
  95. }
  96. func TestRender_IssueIndexPattern2(t *testing.T) {
  97. setting.AppURL = AppURL
  98. setting.AppSubURL = AppSubURL
  99. // numeric: render inputs with valid mentions
  100. test := func(s, expectedFmt string, indices ...int) {
  101. links := make([]interface{}, len(indices))
  102. for i, index := range indices {
  103. links[i] = numericIssueLink(URLJoin(setting.AppSubURL, "issues"), index)
  104. }
  105. expectedNil := fmt.Sprintf(expectedFmt, links...)
  106. testRenderIssueIndexPattern(t, s, expectedNil, nil)
  107. for i, index := range indices {
  108. links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", index)
  109. }
  110. expectedNum := fmt.Sprintf(expectedFmt, links...)
  111. testRenderIssueIndexPattern(t, s, expectedNum, numericMetas)
  112. }
  113. // should render freestanding mentions
  114. test("#1234 test", "%s test", 1234)
  115. test("test #8 issue", "test %s issue", 8)
  116. test("test issue #1234", "test issue %s", 1234)
  117. // should render mentions in parentheses
  118. test("(#54321 issue)", "(%s issue)", 54321)
  119. test("test (#9801 extra) issue", "test (%s extra) issue", 9801)
  120. test("test (#1)", "test (%s)", 1)
  121. // should render multiple issue mentions in the same line
  122. test("#54321 #1243", "%s %s", 54321, 1243)
  123. test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243)
  124. test("(#4)(#5)", "(%s)(%s)", 4, 5)
  125. test("#1 (#4321) test", "%s (%s) test", 1, 4321)
  126. }
  127. func TestRender_IssueIndexPattern3(t *testing.T) {
  128. setting.AppURL = AppURL
  129. setting.AppSubURL = AppSubURL
  130. // alphanumeric: render inputs without valid mentions
  131. test := func(s string) {
  132. testRenderIssueIndexPattern(t, s, s, alphanumericMetas)
  133. }
  134. test("")
  135. test("this is a test")
  136. test("test 123 123 1234")
  137. test("#")
  138. test("##1234")
  139. test("# 123")
  140. test("#abcd")
  141. test("test #123")
  142. test("abc-1234") // issue prefix must be capital
  143. test("ABc-1234") // issue prefix must be _all_ capital
  144. test("ABCDEFGHIJK-1234") // the limit is 10 characters in the prefix
  145. test("ABC1234") // dash is required
  146. test("test ABC- test") // number is required
  147. test("test -1234 test") // prefix is required
  148. test("testABC-123 test") // leading space is required
  149. test("test ABC-123test") // trailing space is required
  150. test("ABC-0123") // no leading zero
  151. }
  152. func TestRender_IssueIndexPattern4(t *testing.T) {
  153. setting.AppURL = AppURL
  154. setting.AppSubURL = AppSubURL
  155. // alphanumeric: render inputs with valid mentions
  156. test := func(s, expectedFmt string, names ...string) {
  157. links := make([]interface{}, len(names))
  158. for i, name := range names {
  159. links[i] = alphanumIssueLink("https://someurl.com/someUser/someRepo/", name)
  160. }
  161. expected := fmt.Sprintf(expectedFmt, links...)
  162. testRenderIssueIndexPattern(t, s, expected, alphanumericMetas)
  163. }
  164. test("OTT-1234 test", "%s test", "OTT-1234")
  165. test("test T-12 issue", "test %s issue", "T-12")
  166. test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890")
  167. }
  168. func TestRender_AutoLink(t *testing.T) {
  169. setting.AppURL = AppURL
  170. setting.AppSubURL = AppSubURL
  171. test := func(input, expected string) {
  172. buffer := RenderSpecialLink([]byte(input), setting.AppSubURL, nil, false)
  173. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  174. buffer = RenderSpecialLink([]byte(input), setting.AppSubURL, nil, true)
  175. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  176. }
  177. // render valid issue URLs
  178. test(URLJoin(setting.AppSubURL, "issues", "3333"),
  179. numericIssueLink(URLJoin(setting.AppSubURL, "issues"), 3333))
  180. // render external issue URLs
  181. tmp := "http://1111/2222/ssss-issues/3333?param=blah&blahh=333"
  182. test(tmp, "<a href=\""+tmp+"\">#3333 <i class='comment icon'></i></a>")
  183. test("http://test.com/issues/33333", numericIssueLink("http://test.com/issues", 33333))
  184. test("https://issues/333", numericIssueLink("https://issues", 333))
  185. // render valid commit URLs
  186. tmp = URLJoin(AppSubURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae")
  187. test(tmp, "<a href=\""+tmp+"\">d8a994ef24</a>")
  188. tmp += "#diff-2"
  189. test(tmp, "<a href=\""+tmp+"\">d8a994ef24 (diff-2)</a>")
  190. // render other commit URLs
  191. tmp = "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2"
  192. test(tmp, "<a href=\""+tmp+"\">d8a994ef24 (diff-2)</a>")
  193. }
  194. func TestRender_StandardLinks(t *testing.T) {
  195. setting.AppURL = AppURL
  196. setting.AppSubURL = AppSubURL
  197. test := func(input, expected, expectedWiki string) {
  198. buffer := RenderString(input, setting.AppSubURL, nil)
  199. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  200. bufferWiki := RenderWiki([]byte(input), setting.AppSubURL, nil)
  201. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(bufferWiki))
  202. }
  203. googleRendered := `<p><a href="https://google.com/" rel="nofollow">https://google.com/</a></p>`
  204. test("<https://google.com/>", googleRendered, googleRendered)
  205. lnk := URLJoin(AppSubURL, "WikiPage")
  206. lnkWiki := URLJoin(AppSubURL, "wiki", "WikiPage")
  207. test("[WikiPage](WikiPage)",
  208. `<p><a href="`+lnk+`" rel="nofollow">WikiPage</a></p>`,
  209. `<p><a href="`+lnkWiki+`" rel="nofollow">WikiPage</a></p>`)
  210. }
  211. func TestRender_ShortLinks(t *testing.T) {
  212. setting.AppURL = AppURL
  213. setting.AppSubURL = AppSubURL
  214. tree := URLJoin(AppSubURL, "src", "master")
  215. test := func(input, expected, expectedWiki string) {
  216. buffer := RenderString(input, tree, nil)
  217. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  218. buffer = RenderWiki([]byte(input), setting.AppSubURL, nil)
  219. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  220. }
  221. rawtree := URLJoin(AppSubURL, "raw", "master")
  222. url := URLJoin(tree, "Link")
  223. otherUrl := URLJoin(tree, "OtherLink")
  224. imgurl := URLJoin(rawtree, "Link.jpg")
  225. urlWiki := URLJoin(AppSubURL, "wiki", "Link")
  226. otherUrlWiki := URLJoin(AppSubURL, "wiki", "OtherLink")
  227. imgurlWiki := URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  228. favicon := "http://google.com/favicon.ico"
  229. test(
  230. "[[Link]]",
  231. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  232. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  233. test(
  234. "[[Link.jpg]]",
  235. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Link.jpg" title="Link.jpg"/></a></p>`,
  236. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Link.jpg" title="Link.jpg"/></a></p>`)
  237. test(
  238. "[["+favicon+"]]",
  239. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  240. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  241. test(
  242. "[[Name|Link]]",
  243. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  244. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  245. test(
  246. "[[Name|Link.jpg]]",
  247. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Name" title="Name"/></a></p>`,
  248. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Name" title="Name"/></a></p>`)
  249. test(
  250. "[[Name|Link.jpg|alt=AltName]]",
  251. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="AltName"/></a></p>`,
  252. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="AltName"/></a></p>`)
  253. test(
  254. "[[Name|Link.jpg|title=Title]]",
  255. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Title" title="Title"/></a></p>`,
  256. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Title" title="Title"/></a></p>`)
  257. test(
  258. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  259. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  260. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  261. test(
  262. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  263. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  264. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  265. test(
  266. "[[Link]] [[OtherLink]]",
  267. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherUrl+`" rel="nofollow">OtherLink</a></p>`,
  268. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherUrlWiki+`" rel="nofollow">OtherLink</a></p>`)
  269. }
  270. func TestRender_Commits(t *testing.T) {
  271. setting.AppURL = AppURL
  272. setting.AppSubURL = AppSubURL
  273. test := func(input, expected string) {
  274. buffer := RenderString(input, setting.AppSubURL, nil)
  275. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  276. }
  277. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  278. var commit = URLJoin(AppSubURL, "commit", sha)
  279. var subtree = URLJoin(commit, "src")
  280. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  281. var src = strings.Replace(subtree, "/commit/", "/src/", -1)
  282. test(sha, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  283. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow">b6dd621</a></p>`)
  284. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow">b6dd6210ea</a></p>`)
  285. test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  286. test(tree, `<p><a href="`+src+`" rel="nofollow">b6dd6210ea/src</a></p>`)
  287. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  288. }
  289. func TestRender_Images(t *testing.T) {
  290. setting.AppURL = AppURL
  291. setting.AppSubURL = AppSubURL
  292. test := func(input, expected string) {
  293. buffer := RenderString(input, setting.AppSubURL, nil)
  294. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  295. }
  296. url := "../../.images/src/02/train.jpg"
  297. title := "Train"
  298. result := URLJoin(AppSubURL, url)
  299. test(
  300. "!["+title+"]("+url+")",
  301. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"></a></p>`)
  302. test(
  303. "[["+title+"|"+url+"]]",
  304. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`" title="`+title+`"/></a></p>`)
  305. }
  306. func TestRender_CrossReferences(t *testing.T) {
  307. setting.AppURL = AppURL
  308. setting.AppSubURL = AppSubURL
  309. test := func(input, expected string) {
  310. buffer := RenderString(input, setting.AppSubURL, nil)
  311. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  312. }
  313. test(
  314. "gogits/gogs#12345",
  315. `<p><a href="`+URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  316. }
  317. func TestRegExp_MentionPattern(t *testing.T) {
  318. trueTestCases := []string{
  319. "@Unknwon",
  320. "@ANT_123",
  321. "@xxx-DiN0-z-A..uru..s-xxx",
  322. " @lol ",
  323. " @Te/st",
  324. }
  325. falseTestCases := []string{
  326. "@ 0",
  327. "@ ",
  328. "@",
  329. "",
  330. "ABC",
  331. }
  332. for _, testCase := range trueTestCases {
  333. res := MentionPattern.MatchString(testCase)
  334. if !res {
  335. println()
  336. println(testCase)
  337. }
  338. assert.True(t, res)
  339. }
  340. for _, testCase := range falseTestCases {
  341. res := MentionPattern.MatchString(testCase)
  342. if res {
  343. println()
  344. println(testCase)
  345. }
  346. assert.False(t, res)
  347. }
  348. }
  349. func TestRegExp_IssueNumericPattern(t *testing.T) {
  350. trueTestCases := []string{
  351. "#1234",
  352. "#0",
  353. "#1234567890987654321",
  354. }
  355. falseTestCases := []string{
  356. "# 1234",
  357. "# 0",
  358. "# ",
  359. "#",
  360. "#ABC",
  361. "#1A2B",
  362. "",
  363. "ABC",
  364. }
  365. for _, testCase := range trueTestCases {
  366. assert.True(t, IssueNumericPattern.MatchString(testCase))
  367. }
  368. for _, testCase := range falseTestCases {
  369. assert.False(t, IssueNumericPattern.MatchString(testCase))
  370. }
  371. }
  372. func TestRegExp_IssueAlphanumericPattern(t *testing.T) {
  373. trueTestCases := []string{
  374. "ABC-1234",
  375. "A-1",
  376. "RC-80",
  377. "ABCDEFGHIJ-1234567890987654321234567890",
  378. }
  379. falseTestCases := []string{
  380. "RC-08",
  381. "PR-0",
  382. "ABCDEFGHIJK-1",
  383. "PR_1",
  384. "",
  385. "#ABC",
  386. "",
  387. "ABC",
  388. "GG-",
  389. "rm-1",
  390. }
  391. for _, testCase := range trueTestCases {
  392. assert.True(t, IssueAlphanumericPattern.MatchString(testCase))
  393. }
  394. for _, testCase := range falseTestCases {
  395. assert.False(t, IssueAlphanumericPattern.MatchString(testCase))
  396. }
  397. }
  398. func TestRegExp_Sha1CurrentPattern(t *testing.T) {
  399. trueTestCases := []string{
  400. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  401. "abcdefabcdefabcdefabcdefabcdefabcdefabcd",
  402. }
  403. falseTestCases := []string{
  404. "test",
  405. "abcdefg",
  406. "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  407. "abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
  408. }
  409. for _, testCase := range trueTestCases {
  410. assert.True(t, Sha1CurrentPattern.MatchString(testCase))
  411. }
  412. for _, testCase := range falseTestCases {
  413. assert.False(t, Sha1CurrentPattern.MatchString(testCase))
  414. }
  415. }
  416. func TestRegExp_ShortLinkPattern(t *testing.T) {
  417. trueTestCases := []string{
  418. "[[stuff]]",
  419. "[[]]",
  420. "[[stuff|title=Difficult name with spaces*!]]",
  421. }
  422. falseTestCases := []string{
  423. "test",
  424. "abcdefg",
  425. "[[]",
  426. "[[",
  427. "[]",
  428. "]]",
  429. "abcdefghijklmnopqrstuvwxyz",
  430. }
  431. for _, testCase := range trueTestCases {
  432. assert.True(t, ShortLinkPattern.MatchString(testCase))
  433. }
  434. for _, testCase := range falseTestCases {
  435. assert.False(t, ShortLinkPattern.MatchString(testCase))
  436. }
  437. }
  438. func TestRegExp_AnySHA1Pattern(t *testing.T) {
  439. testCases := map[string][]string{
  440. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
  441. "https",
  442. "github.com",
  443. "jquery",
  444. "jquery",
  445. "blob",
  446. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  447. "test/unit/event.js",
  448. "L2703",
  449. },
  450. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
  451. "https",
  452. "github.com",
  453. "jquery",
  454. "jquery",
  455. "blob",
  456. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  457. "test/unit/event.js",
  458. "",
  459. },
  460. "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
  461. "https",
  462. "github.com",
  463. "jquery",
  464. "jquery",
  465. "commit",
  466. "0705be475092aede1eddae01319ec931fb9c65fc",
  467. "",
  468. "",
  469. },
  470. "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
  471. "https",
  472. "github.com",
  473. "jquery",
  474. "jquery",
  475. "tree",
  476. "0705be475092aede1eddae01319ec931fb9c65fc",
  477. "src",
  478. "",
  479. },
  480. "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
  481. "https",
  482. "try.gogs.io",
  483. "gogs",
  484. "gogs",
  485. "commit",
  486. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  487. "",
  488. "diff-2",
  489. },
  490. }
  491. for k, v := range testCases {
  492. assert.Equal(t, AnySHA1Pattern.FindStringSubmatch(k)[1:], v)
  493. }
  494. }
  495. func TestRegExp_IssueFullPattern(t *testing.T) {
  496. testCases := map[string][]string{
  497. "https://github.com/gogits/gogs/pull/3244": {
  498. "https",
  499. "github.com/gogits/gogs/pull/",
  500. "3244",
  501. "",
  502. "",
  503. },
  504. "https://github.com/gogits/gogs/issues/3247#issuecomment-231517079": {
  505. "https",
  506. "github.com/gogits/gogs/issues/",
  507. "3247",
  508. "#issuecomment-231517079",
  509. "",
  510. },
  511. "https://try.gogs.io/gogs/gogs/issues/4#issue-685": {
  512. "https",
  513. "try.gogs.io/gogs/gogs/issues/",
  514. "4",
  515. "#issue-685",
  516. "",
  517. },
  518. "https://youtrack.jetbrains.com/issue/JT-36485": {
  519. "https",
  520. "youtrack.jetbrains.com/issue/",
  521. "JT-36485",
  522. "",
  523. "",
  524. },
  525. "https://youtrack.jetbrains.com/issue/JT-36485#comment=27-1508676": {
  526. "https",
  527. "youtrack.jetbrains.com/issue/",
  528. "JT-36485",
  529. "#comment=27-1508676",
  530. "",
  531. },
  532. }
  533. for k, v := range testCases {
  534. assert.Equal(t, IssueFullPattern.FindStringSubmatch(k)[1:], v)
  535. }
  536. }
  537. func TestMisc_IsMarkdownFile(t *testing.T) {
  538. setting.Markdown.FileExtensions = []string{".md", ".markdown", ".mdown", ".mkd"}
  539. trueTestCases := []string{
  540. "test.md",
  541. "wow.MARKDOWN",
  542. "LOL.mDoWn",
  543. }
  544. falseTestCases := []string{
  545. "test",
  546. "abcdefg",
  547. "abcdefghijklmnopqrstuvwxyz",
  548. "test.md.test",
  549. }
  550. for _, testCase := range trueTestCases {
  551. assert.True(t, IsMarkdownFile(testCase))
  552. }
  553. for _, testCase := range falseTestCases {
  554. assert.False(t, IsMarkdownFile(testCase))
  555. }
  556. }
  557. func TestMisc_IsSameDomain(t *testing.T) {
  558. setting.AppURL = AppURL
  559. setting.AppSubURL = AppSubURL
  560. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  561. var commit = URLJoin(AppSubURL, "commit", sha)
  562. assert.True(t, IsSameDomain(commit))
  563. assert.False(t, IsSameDomain("http://google.com/ncr"))
  564. assert.False(t, IsSameDomain("favicon.ico"))
  565. }
  566. // Test cases without ambiguous links
  567. var sameCases = []string{
  568. // dear imgui wiki markdown extract: special wiki syntax
  569. `Wiki! Enjoy :)
  570. - [[Links, Language bindings, Engine bindings|Links]]
  571. - [[Tips]]
  572. Ideas and codes
  573. - Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786
  574. - Node graph editors https://github.com/ocornut/imgui/issues/306
  575. - [[Memory Editor|memory_editor_example]]
  576. - [[Plot var helper|plot_var_example]]`,
  577. // wine-staging wiki home extract: tables, special wiki syntax, images
  578. `## What is Wine Staging?
  579. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  580. ## Quick Links
  581. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  582. | [[images/icon-install.png]] | [[Installation]] |
  583. |--------------------------------|----------------------------------------------------------|
  584. | [[images/icon-usage.png]] | [[Usage]] |
  585. `,
  586. // libgdx wiki page: inline images with special syntax
  587. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  588. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  589. [[images/1.png]]
  590. 2. Perform a test run by hitting the Run! button.
  591. [[images/2.png]]`,
  592. }
  593. func testAnswers(baseURLContent, baseURLImages string) []string {
  594. return []string{
  595. `<p>Wiki! Enjoy :)</p>
  596. <ul>
  597. <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  598. <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
  599. </ul>
  600. <p>Ideas and codes</p>
  601. <ul>
  602. <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>)<a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">#786</a></li>
  603. <li>Node graph editors<a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">#306</a></li>
  604. <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
  605. <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
  606. </ul>
  607. `,
  608. `<h2>What is Wine Staging?</h2>
  609. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  610. <h2>Quick Links</h2>
  611. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  612. <table>
  613. <thead>
  614. <tr>
  615. <th><a href="` + baseURLImages + `/images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-install.png" alt="images/icon-install.png" title="icon-install.png"/></a></th>
  616. <th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
  617. </tr>
  618. </thead>
  619. <tbody>
  620. <tr>
  621. <td><a href="` + baseURLImages + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-usage.png" alt="images/icon-usage.png" title="icon-usage.png"/></a></td>
  622. <td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
  623. </tr>
  624. </tbody>
  625. </table>
  626. `,
  627. `<p><a href="http://www.excelsiorjet.com/" rel="nofollow">Excelsior JET</a> allows you to create native executables for Windows, Linux and Mac OS X.</p>
  628. <ol>
  629. <li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
  630. <a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" alt="images/1.png" title="1.png"/></a></li>
  631. <li>Perform a test run by hitting the Run! button.
  632. <a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" alt="images/2.png" title="2.png"/></a></li>
  633. </ol>
  634. `,
  635. }
  636. }
  637. func TestTotal_RenderString(t *testing.T) {
  638. answers := testAnswers(URLJoin(AppSubURL, "src", "master/"), URLJoin(AppSubURL, "raw", "master/"))
  639. for i := 0; i < len(sameCases); i++ {
  640. line := RenderString(sameCases[i], URLJoin(AppSubURL, "src", "master/"), nil)
  641. assert.Equal(t, answers[i], line)
  642. }
  643. testCases := []string{}
  644. for i := 0; i < len(testCases); i += 2 {
  645. line := RenderString(testCases[i], AppSubURL, nil)
  646. assert.Equal(t, testCases[i+1], line)
  647. }
  648. }
  649. func TestTotal_RenderWiki(t *testing.T) {
  650. answers := testAnswers(URLJoin(AppSubURL, "wiki/"), URLJoin(AppSubURL, "wiki", "raw/"))
  651. for i := 0; i < len(sameCases); i++ {
  652. line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil)
  653. assert.Equal(t, answers[i], line)
  654. }
  655. testCases := []string{
  656. // Guard wiki sidebar: special syntax
  657. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  658. // rendered
  659. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  660. `,
  661. // special syntax
  662. `[[Name|Link]]`,
  663. // rendered
  664. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  665. `,
  666. }
  667. for i := 0; i < len(testCases); i += 2 {
  668. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  669. assert.Equal(t, testCases[i+1], line)
  670. }
  671. }