markdown_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package markup_test
  2. import (
  3. "bytes"
  4. . "gitote/gitote/pkg/markup"
  5. "gitote/gitote/pkg/setting"
  6. "strings"
  7. "testing"
  8. "github.com/russross/blackfriday"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func Test_IsMarkdownFile(t *testing.T) {
  12. setting.Markdown.FileExtensions = strings.Split(".md,.markdown,.mdown,.mkd", ",")
  13. Convey("Detect Markdown file extension", t, func() {
  14. testCases := []struct {
  15. ext string
  16. match bool
  17. }{
  18. {".md", true},
  19. {".markdown", true},
  20. {".mdown", true},
  21. {".mkd", true},
  22. {".org", false},
  23. {".rst", false},
  24. {".asciidoc", false},
  25. }
  26. for _, tc := range testCases {
  27. So(IsMarkdownFile(tc.ext), ShouldEqual, tc.match)
  28. }
  29. })
  30. }
  31. func Test_Markdown(t *testing.T) {
  32. Convey("Rendering an issue URL", t, func() {
  33. setting.AppURL = "http://localhost:3000/"
  34. htmlFlags := 0
  35. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  36. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  37. renderer := &MarkdownRenderer{
  38. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  39. }
  40. buffer := new(bytes.Buffer)
  41. Convey("To the internal issue tracker", func() {
  42. Convey("It should render valid issue URLs", func() {
  43. testCases := []string{
  44. "http://localhost:3000/user/repo/issues/3333", "<a href=\"http://localhost:3000/user/repo/issues/3333\">#3333</a>",
  45. }
  46. for i := 0; i < len(testCases); i += 2 {
  47. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  48. line, _ := buffer.ReadString(0)
  49. So(line, ShouldEqual, testCases[i+1])
  50. }
  51. })
  52. Convey("It should render but not change non-issue URLs", func() {
  53. testCases := []string{
  54. "http://1111/2222/ssss-issues/3333?param=blah&blahh=333", "<a href=\"http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333\">http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333</a>",
  55. "http://test.com/issues/33333", "<a href=\"http://test.com/issues/33333\">http://test.com/issues/33333</a>",
  56. "http://test.com/issues/3", "<a href=\"http://test.com/issues/3\">http://test.com/issues/3</a>",
  57. "http://issues/333", "<a href=\"http://issues/333\">http://issues/333</a>",
  58. "https://issues/333", "<a href=\"https://issues/333\">https://issues/333</a>",
  59. "http://tissues/0", "<a href=\"http://tissues/0\">http://tissues/0</a>",
  60. }
  61. for i := 0; i < len(testCases); i += 2 {
  62. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  63. line, _ := buffer.ReadString(0)
  64. So(line, ShouldEqual, testCases[i+1])
  65. }
  66. })
  67. })
  68. })
  69. Convey("Rendering a commit URL", t, func() {
  70. setting.AppURL = "http://localhost:3000/"
  71. htmlFlags := 0
  72. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  73. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  74. renderer := &MarkdownRenderer{
  75. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  76. }
  77. buffer := new(bytes.Buffer)
  78. Convey("To the internal issue tracker", func() {
  79. Convey("It should correctly convert URLs", func() {
  80. testCases := []string{
  81. "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae", " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">d8a994ef24</a></code>",
  82. "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">d8a994ef24</a></code>",
  83. "https://external-link.gitote/gitote/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", "<a href=\"https://external-link.gitote/gitote/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">https://external-link.gitote/gitote/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2</a>",
  84. "https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae", "<a href=\"https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae</a>",
  85. }
  86. for i := 0; i < len(testCases); i += 2 {
  87. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  88. line, _ := buffer.ReadString(0)
  89. So(line, ShouldEqual, testCases[i+1])
  90. }
  91. })
  92. })
  93. })
  94. }