code_block_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 TestCodeBlockParser(t *testing.T) {
  8. tests := []struct {
  9. text string
  10. codeBlock *CodeBlockParser
  11. }{
  12. {
  13. text: "```Hello world!```",
  14. codeBlock: nil,
  15. },
  16. {
  17. text: "```\nHello\n```",
  18. codeBlock: &CodeBlockParser{
  19. Language: "",
  20. Content: "Hello",
  21. },
  22. },
  23. {
  24. text: "```\nHello world!\n```",
  25. codeBlock: &CodeBlockParser{
  26. Language: "",
  27. Content: "Hello world!",
  28. },
  29. },
  30. {
  31. text: "```java\nHello \n world!\n```",
  32. codeBlock: &CodeBlockParser{
  33. Language: "java",
  34. Content: "Hello \n world!",
  35. },
  36. },
  37. {
  38. text: "```java\nHello \n world!\n```111",
  39. codeBlock: nil,
  40. },
  41. {
  42. text: "```java\nHello \n world!\n``` 111",
  43. codeBlock: nil,
  44. },
  45. {
  46. text: "```java\nHello \n world!\n```\n123123",
  47. codeBlock: &CodeBlockParser{
  48. Language: "java",
  49. Content: "Hello \n world!",
  50. },
  51. },
  52. }
  53. for _, test := range tests {
  54. tokens := tokenizer.Tokenize(test.text)
  55. codeBlock := NewCodeBlockParser()
  56. require.Equal(t, test.codeBlock, codeBlock.Match(tokens))
  57. }
  58. }