gitea源码

mdstripper_test.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package mdstripper
  4. import (
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestMarkdownStripper(t *testing.T) {
  10. type testItem struct {
  11. markdown string
  12. expectedText []string
  13. expectedLinks []string
  14. }
  15. list := []testItem{
  16. {
  17. `
  18. ## This is a title
  19. This is [one](link) to paradise.
  20. This **is emphasized**.
  21. This: should coalesce.
  22. ` + "```" + `
  23. This is a code block.
  24. This should not appear in the output at all.
  25. ` + "```" + `
  26. * Bullet 1
  27. * Bullet 2
  28. A HIDDEN ` + "`" + `GHOST` + "`" + ` IN THIS LINE.
  29. `,
  30. []string{
  31. "This is a title",
  32. "This is",
  33. "to paradise.",
  34. "This",
  35. "is emphasized",
  36. ".",
  37. "This: should coalesce.",
  38. "Bullet 1",
  39. "Bullet 2",
  40. "A HIDDEN",
  41. "IN THIS LINE.",
  42. },
  43. []string{
  44. "link",
  45. },
  46. },
  47. {
  48. "Simply closes: #29 yes",
  49. []string{
  50. "Simply closes: #29 yes",
  51. },
  52. []string{},
  53. },
  54. {
  55. "Simply closes: !29 yes",
  56. []string{
  57. "Simply closes: !29 yes",
  58. },
  59. []string{},
  60. },
  61. }
  62. for _, test := range list {
  63. text, links := StripMarkdown([]byte(test.markdown))
  64. rawlines := strings.Split(text, "\n")
  65. lines := make([]string, 0, len(rawlines))
  66. for _, line := range rawlines {
  67. line := strings.TrimSpace(line)
  68. if line != "" {
  69. lines = append(lines, line)
  70. }
  71. }
  72. assert.Equal(t, test.expectedText, lines)
  73. assert.Equal(t, test.expectedLinks, links)
  74. }
  75. }