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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. imgurl := URLJoin(rawtree, "Link.jpg")
  224. urlWiki := URLJoin(AppSubURL, "wiki", "Link")
  225. imgurlWiki := URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  226. favicon := "http://google.com/favicon.ico"
  227. test(
  228. "[[Link]]",
  229. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  230. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  231. test(
  232. "[[Link.jpg]]",
  233. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Link.jpg" title="Link.jpg"/></a></p>`,
  234. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Link.jpg" title="Link.jpg"/></a></p>`)
  235. test(
  236. "[["+favicon+"]]",
  237. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  238. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  239. test(
  240. "[[Name|Link]]",
  241. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  242. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  243. test(
  244. "[[Name|Link.jpg]]",
  245. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Name" title="Name"/></a></p>`,
  246. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Name" title="Name"/></a></p>`)
  247. test(
  248. "[[Name|Link.jpg|alt=AltName]]",
  249. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="AltName"/></a></p>`,
  250. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="AltName"/></a></p>`)
  251. test(
  252. "[[Name|Link.jpg|title=Title]]",
  253. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Title" title="Title"/></a></p>`,
  254. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Title" title="Title"/></a></p>`)
  255. test(
  256. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  257. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  258. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  259. test(
  260. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  261. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  262. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  263. }
  264. func TestRender_Commits(t *testing.T) {
  265. setting.AppURL = AppURL
  266. setting.AppSubURL = AppSubURL
  267. test := func(input, expected string) {
  268. buffer := RenderString(input, setting.AppSubURL, nil)
  269. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  270. }
  271. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  272. var commit = URLJoin(AppSubURL, "commit", sha)
  273. var subtree = URLJoin(commit, "src")
  274. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  275. var src = strings.Replace(subtree, "/commit/", "/src/", -1)
  276. test(sha, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  277. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow">b6dd621</a></p>`)
  278. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow">b6dd6210ea</a></p>`)
  279. test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  280. test(tree, `<p><a href="`+src+`" rel="nofollow">b6dd6210ea/src</a></p>`)
  281. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  282. }
  283. func TestRender_Images(t *testing.T) {
  284. setting.AppURL = AppURL
  285. setting.AppSubURL = AppSubURL
  286. test := func(input, expected string) {
  287. buffer := RenderString(input, setting.AppSubURL, nil)
  288. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  289. }
  290. url := "../../.images/src/02/train.jpg"
  291. title := "Train"
  292. result := URLJoin(AppSubURL, url)
  293. test(
  294. "!["+title+"]("+url+")",
  295. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"></a></p>`)
  296. test(
  297. "[["+title+"|"+url+"]]",
  298. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`" title="`+title+`"/></a></p>`)
  299. }
  300. func TestRender_CrossReferences(t *testing.T) {
  301. setting.AppURL = AppURL
  302. setting.AppSubURL = AppSubURL
  303. test := func(input, expected string) {
  304. buffer := RenderString(input, setting.AppSubURL, nil)
  305. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  306. }
  307. test(
  308. "gogits/gogs#12345",
  309. `<p><a href="`+URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  310. }
  311. func TestRegExp_MentionPattern(t *testing.T) {
  312. trueTestCases := []string{
  313. "@Unknwon",
  314. "@ANT_123",
  315. "@xxx-DiN0-z-A..uru..s-xxx",
  316. " @lol ",
  317. " @Te/st",
  318. }
  319. falseTestCases := []string{
  320. "@ 0",
  321. "@ ",
  322. "@",
  323. "",
  324. "ABC",
  325. }
  326. for _, testCase := range trueTestCases {
  327. res := MentionPattern.MatchString(testCase)
  328. if !res {
  329. println()
  330. println(testCase)
  331. }
  332. assert.True(t, res)
  333. }
  334. for _, testCase := range falseTestCases {
  335. res := MentionPattern.MatchString(testCase)
  336. if res {
  337. println()
  338. println(testCase)
  339. }
  340. assert.False(t, res)
  341. }
  342. }
  343. func TestRegExp_IssueNumericPattern(t *testing.T) {
  344. trueTestCases := []string{
  345. "#1234",
  346. "#0",
  347. "#1234567890987654321",
  348. }
  349. falseTestCases := []string{
  350. "# 1234",
  351. "# 0",
  352. "# ",
  353. "#",
  354. "#ABC",
  355. "#1A2B",
  356. "",
  357. "ABC",
  358. }
  359. for _, testCase := range trueTestCases {
  360. assert.True(t, IssueNumericPattern.MatchString(testCase))
  361. }
  362. for _, testCase := range falseTestCases {
  363. assert.False(t, IssueNumericPattern.MatchString(testCase))
  364. }
  365. }
  366. func TestRegExp_IssueAlphanumericPattern(t *testing.T) {
  367. trueTestCases := []string{
  368. "ABC-1234",
  369. "A-1",
  370. "RC-80",
  371. "ABCDEFGHIJ-1234567890987654321234567890",
  372. }
  373. falseTestCases := []string{
  374. "RC-08",
  375. "PR-0",
  376. "ABCDEFGHIJK-1",
  377. "PR_1",
  378. "",
  379. "#ABC",
  380. "",
  381. "ABC",
  382. "GG-",
  383. "rm-1",
  384. }
  385. for _, testCase := range trueTestCases {
  386. assert.True(t, IssueAlphanumericPattern.MatchString(testCase))
  387. }
  388. for _, testCase := range falseTestCases {
  389. assert.False(t, IssueAlphanumericPattern.MatchString(testCase))
  390. }
  391. }
  392. func TestRegExp_Sha1CurrentPattern(t *testing.T) {
  393. trueTestCases := []string{
  394. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  395. "abcdefabcdefabcdefabcdefabcdefabcdefabcd",
  396. }
  397. falseTestCases := []string{
  398. "test",
  399. "abcdefg",
  400. "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  401. "abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
  402. }
  403. for _, testCase := range trueTestCases {
  404. assert.True(t, Sha1CurrentPattern.MatchString(testCase))
  405. }
  406. for _, testCase := range falseTestCases {
  407. assert.False(t, Sha1CurrentPattern.MatchString(testCase))
  408. }
  409. }
  410. func TestRegExp_ShortLinkPattern(t *testing.T) {
  411. trueTestCases := []string{
  412. "[[stuff]]",
  413. "[[]]",
  414. "[[stuff|title=Difficult name with spaces*!]]",
  415. }
  416. falseTestCases := []string{
  417. "test",
  418. "abcdefg",
  419. "[[]",
  420. "[[",
  421. "[]",
  422. "]]",
  423. "abcdefghijklmnopqrstuvwxyz",
  424. }
  425. for _, testCase := range trueTestCases {
  426. assert.True(t, ShortLinkPattern.MatchString(testCase))
  427. }
  428. for _, testCase := range falseTestCases {
  429. assert.False(t, ShortLinkPattern.MatchString(testCase))
  430. }
  431. }
  432. func TestRegExp_AnySHA1Pattern(t *testing.T) {
  433. testCases := map[string][]string{
  434. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
  435. "https",
  436. "github.com",
  437. "jquery",
  438. "jquery",
  439. "blob",
  440. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  441. "test/unit/event.js",
  442. "L2703",
  443. },
  444. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
  445. "https",
  446. "github.com",
  447. "jquery",
  448. "jquery",
  449. "blob",
  450. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  451. "test/unit/event.js",
  452. "",
  453. },
  454. "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
  455. "https",
  456. "github.com",
  457. "jquery",
  458. "jquery",
  459. "commit",
  460. "0705be475092aede1eddae01319ec931fb9c65fc",
  461. "",
  462. "",
  463. },
  464. "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
  465. "https",
  466. "github.com",
  467. "jquery",
  468. "jquery",
  469. "tree",
  470. "0705be475092aede1eddae01319ec931fb9c65fc",
  471. "src",
  472. "",
  473. },
  474. "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
  475. "https",
  476. "try.gogs.io",
  477. "gogs",
  478. "gogs",
  479. "commit",
  480. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  481. "",
  482. "diff-2",
  483. },
  484. }
  485. for k, v := range testCases {
  486. assert.Equal(t, AnySHA1Pattern.FindStringSubmatch(k)[1:], v)
  487. }
  488. }
  489. func TestRegExp_IssueFullPattern(t *testing.T) {
  490. testCases := map[string][]string{
  491. "https://github.com/gogits/gogs/pull/3244": {
  492. "https",
  493. "github.com/gogits/gogs/pull/",
  494. "3244",
  495. "",
  496. "",
  497. },
  498. "https://github.com/gogits/gogs/issues/3247#issuecomment-231517079": {
  499. "https",
  500. "github.com/gogits/gogs/issues/",
  501. "3247",
  502. "#issuecomment-231517079",
  503. "",
  504. },
  505. "https://try.gogs.io/gogs/gogs/issues/4#issue-685": {
  506. "https",
  507. "try.gogs.io/gogs/gogs/issues/",
  508. "4",
  509. "#issue-685",
  510. "",
  511. },
  512. "https://youtrack.jetbrains.com/issue/JT-36485": {
  513. "https",
  514. "youtrack.jetbrains.com/issue/",
  515. "JT-36485",
  516. "",
  517. "",
  518. },
  519. "https://youtrack.jetbrains.com/issue/JT-36485#comment=27-1508676": {
  520. "https",
  521. "youtrack.jetbrains.com/issue/",
  522. "JT-36485",
  523. "#comment=27-1508676",
  524. "",
  525. },
  526. }
  527. for k, v := range testCases {
  528. assert.Equal(t, IssueFullPattern.FindStringSubmatch(k)[1:], v)
  529. }
  530. }
  531. func TestMisc_IsMarkdownFile(t *testing.T) {
  532. setting.Markdown.FileExtensions = []string{".md", ".markdown", ".mdown", ".mkd"}
  533. trueTestCases := []string{
  534. "test.md",
  535. "wow.MARKDOWN",
  536. "LOL.mDoWn",
  537. }
  538. falseTestCases := []string{
  539. "test",
  540. "abcdefg",
  541. "abcdefghijklmnopqrstuvwxyz",
  542. "test.md.test",
  543. }
  544. for _, testCase := range trueTestCases {
  545. assert.True(t, IsMarkdownFile(testCase))
  546. }
  547. for _, testCase := range falseTestCases {
  548. assert.False(t, IsMarkdownFile(testCase))
  549. }
  550. }
  551. func TestMisc_IsSameDomain(t *testing.T) {
  552. setting.AppURL = AppURL
  553. setting.AppSubURL = AppSubURL
  554. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  555. var commit = URLJoin(AppSubURL, "commit", sha)
  556. assert.True(t, IsSameDomain(commit))
  557. assert.False(t, IsSameDomain("http://google.com/ncr"))
  558. assert.False(t, IsSameDomain("favicon.ico"))
  559. }
  560. // Test cases without ambiguous links
  561. var sameCases = []string{
  562. // dear imgui wiki markdown extract: special wiki syntax
  563. `Wiki! Enjoy :)
  564. - [[Links, Language bindings, Engine bindings|Links]]
  565. - [[Tips]]
  566. Ideas and codes
  567. - Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786
  568. - Node graph editors https://github.com/ocornut/imgui/issues/306
  569. - [[Memory Editor|memory_editor_example]]
  570. - [[Plot var helper|plot_var_example]]`,
  571. // wine-staging wiki home extract: tables, special wiki syntax, images
  572. `## What is Wine Staging?
  573. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  574. ## Quick Links
  575. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  576. | [[images/icon-install.png]] | [[Installation]] |
  577. |--------------------------------|----------------------------------------------------------|
  578. | [[images/icon-usage.png]] | [[Usage]] |
  579. `,
  580. // libgdx wiki page: inline images with special syntax
  581. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  582. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  583. [[images/1.png]]
  584. 2. Perform a test run by hitting the Run! button.
  585. [[images/2.png]]`,
  586. }
  587. func testAnswers(baseURLContent, baseURLImages string) []string {
  588. return []string{
  589. `<p>Wiki! Enjoy :)</p>
  590. <ul>
  591. <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  592. <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
  593. </ul>
  594. <p>Ideas and codes</p>
  595. <ul>
  596. <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>
  597. <li>Node graph editors<a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">#306</a></li>
  598. <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
  599. <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
  600. </ul>
  601. `,
  602. `<h2>What is Wine Staging?</h2>
  603. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  604. <h2>Quick Links</h2>
  605. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  606. <table>
  607. <thead>
  608. <tr>
  609. <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>
  610. <th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
  611. </tr>
  612. </thead>
  613. <tbody>
  614. <tr>
  615. <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>
  616. <td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
  617. </tr>
  618. </tbody>
  619. </table>
  620. `,
  621. `<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>
  622. <ol>
  623. <li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
  624. <a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" alt="images/1.png" title="1.png"/></a></li>
  625. <li>Perform a test run by hitting the Run! button.
  626. <a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" alt="images/2.png" title="2.png"/></a></li>
  627. </ol>
  628. `,
  629. }
  630. }
  631. func TestTotal_RenderString(t *testing.T) {
  632. answers := testAnswers(URLJoin(AppSubURL, "src", "master/"), URLJoin(AppSubURL, "raw", "master/"))
  633. for i := 0; i < len(sameCases); i++ {
  634. line := RenderString(sameCases[i], URLJoin(AppSubURL, "src", "master/"), nil)
  635. assert.Equal(t, answers[i], line)
  636. }
  637. testCases := []string{}
  638. for i := 0; i < len(testCases); i += 2 {
  639. line := RenderString(testCases[i], AppSubURL, nil)
  640. assert.Equal(t, testCases[i+1], line)
  641. }
  642. }
  643. func TestTotal_RenderWiki(t *testing.T) {
  644. answers := testAnswers(URLJoin(AppSubURL, "wiki/"), URLJoin(AppSubURL, "wiki", "raw/"))
  645. for i := 0; i < len(sameCases); i++ {
  646. line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil)
  647. assert.Equal(t, answers[i], line)
  648. }
  649. testCases := []string{
  650. // Guard wiki sidebar: special syntax
  651. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  652. // rendered
  653. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  654. `,
  655. // special syntax
  656. `[[Name|Link]]`,
  657. // rendered
  658. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  659. `,
  660. }
  661. for i := 0; i < len(testCases); i += 2 {
  662. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  663. assert.Equal(t, testCases[i+1], line)
  664. }
  665. }