heading_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package parser
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/usememos/memos/plugin/gomark/parser/tokenizer"
  6. )
  7. func TestHeadingParser(t *testing.T) {
  8. tests := []struct {
  9. text string
  10. heading *HeadingParser
  11. }{
  12. {
  13. text: "*Hello world",
  14. heading: nil,
  15. },
  16. {
  17. text: "## Hello World",
  18. heading: &HeadingParser{
  19. Level: 2,
  20. ContentTokens: []*tokenizer.Token{
  21. {
  22. Type: tokenizer.Text,
  23. Value: "Hello",
  24. },
  25. {
  26. Type: tokenizer.Space,
  27. Value: " ",
  28. },
  29. {
  30. Type: tokenizer.Text,
  31. Value: "World",
  32. },
  33. },
  34. },
  35. },
  36. {
  37. text: "# # Hello World",
  38. heading: &HeadingParser{
  39. Level: 1,
  40. ContentTokens: []*tokenizer.Token{
  41. {
  42. Type: tokenizer.Hash,
  43. Value: "#",
  44. },
  45. {
  46. Type: tokenizer.Space,
  47. Value: " ",
  48. },
  49. {
  50. Type: tokenizer.Text,
  51. Value: "Hello",
  52. },
  53. {
  54. Type: tokenizer.Space,
  55. Value: " ",
  56. },
  57. {
  58. Type: tokenizer.Text,
  59. Value: "World",
  60. },
  61. },
  62. },
  63. },
  64. {
  65. text: " # 123123 Hello World",
  66. heading: nil,
  67. },
  68. {
  69. text: `# 123
  70. Hello World`,
  71. heading: &HeadingParser{
  72. Level: 1,
  73. ContentTokens: []*tokenizer.Token{
  74. {
  75. Type: tokenizer.Text,
  76. Value: "123",
  77. },
  78. {
  79. Type: tokenizer.Space,
  80. Value: " ",
  81. },
  82. },
  83. },
  84. },
  85. }
  86. for _, test := range tests {
  87. tokens := tokenizer.Tokenize(test.text)
  88. heading := NewHeadingParser()
  89. require.Equal(t, test.heading, heading.Match(tokens))
  90. }
  91. }