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

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