italic_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 TestItalicParser(t *testing.T) {
  8. tests := []struct {
  9. text string
  10. italic *ItalicParser
  11. }{
  12. {
  13. text: "*Hello world!",
  14. italic: nil,
  15. },
  16. {
  17. text: "*Hello*",
  18. italic: &ItalicParser{
  19. ContentTokens: []*tokenizer.Token{
  20. {
  21. Type: tokenizer.Text,
  22. Value: "Hello",
  23. },
  24. },
  25. },
  26. },
  27. {
  28. text: "* Hello *",
  29. italic: &ItalicParser{
  30. ContentTokens: []*tokenizer.Token{
  31. {
  32. Type: tokenizer.Space,
  33. Value: " ",
  34. },
  35. {
  36. Type: tokenizer.Text,
  37. Value: "Hello",
  38. },
  39. {
  40. Type: tokenizer.Space,
  41. Value: " ",
  42. },
  43. },
  44. },
  45. },
  46. {
  47. text: "** Hello * *",
  48. italic: nil,
  49. },
  50. {
  51. text: "*1* Hello * *",
  52. italic: &ItalicParser{
  53. ContentTokens: []*tokenizer.Token{
  54. {
  55. Type: tokenizer.Text,
  56. Value: "1",
  57. },
  58. },
  59. },
  60. },
  61. {
  62. text: `* \n * Hello * *`,
  63. italic: &ItalicParser{
  64. ContentTokens: []*tokenizer.Token{
  65. {
  66. Type: tokenizer.Space,
  67. Value: " ",
  68. },
  69. {
  70. Type: tokenizer.Text,
  71. Value: `\n`,
  72. },
  73. {
  74. Type: tokenizer.Space,
  75. Value: " ",
  76. },
  77. },
  78. },
  79. },
  80. {
  81. text: "* \n * Hello * *",
  82. italic: nil,
  83. },
  84. }
  85. for _, test := range tests {
  86. tokens := tokenizer.Tokenize(test.text)
  87. italic := NewItalicParser()
  88. require.Equal(t, test.italic, italic.Match(tokens))
  89. }
  90. }