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 23 kB

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