|
1 | 1 | package ast
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "reflect"
|
5 | 6 | "testing"
|
| 7 | + |
| 8 | + "github.com/yuin/goldmark/text" |
6 | 9 | )
|
7 | 10 |
|
8 | 11 | func TestRemoveChildren(t *testing.T) {
|
@@ -73,3 +76,48 @@ func node(n Node, children ...Node) Node {
|
73 | 76 | }
|
74 | 77 | return n
|
75 | 78 | }
|
| 79 | + |
| 80 | +func TestBaseBlock_Text(t *testing.T) { |
| 81 | + source := []byte(`# Heading |
| 82 | +
|
| 83 | + code block here |
| 84 | + and also here |
| 85 | +
|
| 86 | +A paragraph |
| 87 | +
|
| 88 | +` + "```" + `somelang |
| 89 | +fenced code block |
| 90 | +` + "```" + ` |
| 91 | +
|
| 92 | +The end`) |
| 93 | + |
| 94 | + t.Run("fetch text from code block", func(t *testing.T) { |
| 95 | + block := NewCodeBlock() |
| 96 | + block.lines = text.NewSegments() |
| 97 | + block.lines.Append(text.Segment{Start: 15, Stop: 31}) |
| 98 | + block.lines.Append(text.Segment{Start: 32, Stop: 46}) |
| 99 | + |
| 100 | + expected := []byte("code block here\nand also here\n") |
| 101 | + if !bytes.Equal(expected, block.Text(source)) { |
| 102 | + t.Errorf("Expected: %q, got: %q", string(expected), string(block.Text(source))) |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("fetch text from fenced code block", func(t *testing.T) { |
| 107 | + block := NewFencedCodeBlock(&Text{ |
| 108 | + Segment: text.Segment{Start: 63, Stop: 71}, |
| 109 | + }) |
| 110 | + block.lines = text.NewSegments() |
| 111 | + block.lines.Append(text.Segment{Start: 72, Stop: 90}) |
| 112 | + |
| 113 | + expectedLang := []byte("somelang") |
| 114 | + if !bytes.Equal(expectedLang, block.Language(source)) { |
| 115 | + t.Errorf("Expected: %q, got: %q", string(expectedLang), string(block.Language(source))) |
| 116 | + } |
| 117 | + |
| 118 | + expected := []byte("fenced code block\n") |
| 119 | + if !bytes.Equal(expected, block.Text(source)) { |
| 120 | + t.Errorf("Expected: %q, got: %q", string(expected), string(block.Text(source))) |
| 121 | + } |
| 122 | + }) |
| 123 | +} |
0 commit comments