Debian Codegraph @

extension/ast_test.go

raw symbols SCIP
1 package extension
2
3 import (
4 "bytes"
5 "testing"
6
7 "github.com/yuin/goldmark"
8 "github.com/yuin/goldmark/renderer/html"
9 "github.com/yuin/goldmark/testutil"
10 "github.com/yuin/goldmark/text"
11 )
12
13 func TestASTBlockNodeText(t *testing.T) {
14 var cases = []struct {
15 Name string
16 Source string
17 T1 string
18 T2 string
19 C bool
20 }{
21 {
22 Name: "DefinitionList",
23 Source: `c1
24 : c2
25 c3
26
27 a
28
29 c4
30 : c5
31 c6`,
32 T1: `c1c2
33 c3`,
34 T2: `c4c5
35 c6`,
36 },
37 {
38 Name: "Table",
39 Source: `| h1 | h2 |
40 | -- | -- |
41 | c1 | c2 |
42
43 a
44
45
46 | h3 | h4 |
47 | -- | -- |
48 | c3 | c4 |`,
49
50 T1: `h1h2c1c2`,
51 T2: `h3h4c3c4`,
52 },
53 }
54
55 for _, cs := range cases {
56 t.Run(cs.Name, func(t *testing.T) {
57 s := []byte(cs.Source)
58 md := goldmark.New(
59 goldmark.WithRendererOptions(
60 html.WithUnsafe(),
61 ),
62 goldmark.WithExtensions(
63 DefinitionList,
64 Table,
65 ),
66 )
67 n := md.Parser().Parse(text.NewReader(s))
68 c1 := n.FirstChild()
69 c2 := c1.NextSibling().NextSibling()
70 if cs.C {
71 c1 = c1.FirstChild()
72 c2 = c2.FirstChild()
73 }
74 if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
75
76 t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
77
78 }
79 if !bytes.Equal(c2.Text(s), []byte(cs.T2)) { // nolint: staticcheck
80
81 t.Errorf("%s(EOF) unmatch: %s", cs.Name, testutil.DiffPretty(c2.Text(s), []byte(cs.T2))) // nolint: staticcheck
82
83 }
84 })
85 }
86
87 }
88
89 func TestASTInlineNodeText(t *testing.T) {
90 var cases = []struct {
91 Name string
92 Source string
93 T1 string
94 }{
95 {
96 Name: "Strikethrough",
97 Source: `~c1 *c2*~`,
98 T1: `c1 c2`,
99 },
100 }
101
102 for _, cs := range cases {
103 t.Run(cs.Name, func(t *testing.T) {
104 s := []byte(cs.Source)
105 md := goldmark.New(
106 goldmark.WithRendererOptions(
107 html.WithUnsafe(),
108 ),
109 goldmark.WithExtensions(
110 Strikethrough,
111 ),
112 )
113 n := md.Parser().Parse(text.NewReader(s))
114 c1 := n.FirstChild().FirstChild()
115 if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
116
117 t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
118
119 }
120 })
121 }
122
123 }