-
Notifications
You must be signed in to change notification settings - Fork 5
/
markdown_test.go
220 lines (202 loc) · 7.05 KB
/
markdown_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package fir
import (
"bytes"
"html/template"
"strings"
"testing"
"golang.org/x/net/html"
)
func TestMarkdownTemplate(t *testing.T) {
tmpl := `{{ markdown "./testdata/snippet_input.md" "marker" }}`
expected := `<p>Snippet Content</p>`
md := markdown(readFileOS, existFileOS)
var buf bytes.Buffer
template.Must(template.New("test").Funcs(template.FuncMap{
"markdown": md,
}).Parse(tmpl)).Execute(&buf, nil)
actual := html.UnescapeString(buf.String())
actualNode, err := html.Parse(strings.NewReader(actual))
if err != nil {
t.Fatalf("failed to parse actual HTML: %v", err)
}
expectedNode, err := html.Parse(strings.NewReader(expected))
if err != nil {
t.Fatalf("failed to parse expected HTML: %v", err)
}
if err := areNodesDeepEqual(actualNode, expectedNode); err != nil {
t.Errorf("Expected:\n%s\n\nGot:\n%s\n, err: %v \n", expected, actual, err)
}
}
func TestMarkdown(t *testing.T) {
type markdownTestCase struct {
name string
input string
markers []string
expected string
expectError bool
}
markdownTestCases := []markdownTestCase{
{
name: "Test empty input",
input: "",
expected: "",
},
{
name: "Test basic Markdown conversion",
input: "## Heading",
expected: "<h2 id=\"heading\">Heading</h2>\n",
},
{
name: "Test snippet with single marker",
input: "<!-- start marker -->\nSnippet Content\n<!-- end marker -->",
markers: []string{"marker"},
expected: "<p>Snippet Content</p>",
},
{
name: "Test snippet with multiple markers",
input: "<!-- start marker1 -->\nSnippet Content 1\n<!-- end marker1 -->\nSome Content\n<!-- start marker2 -->\nSnippet Content 2\n<!-- end marker2 -->",
markers: []string{"marker1", "marker2"},
expected: `<p>Snippet Content 1<br />Snippet Content 2</p>`,
},
{
name: "Test basic Markdown conversion",
input: "./testdata/basic_input.md",
expected: "<h2 id=\"heading\">Heading</h2>\n",
expectError: false,
},
{
name: "Test snippet with single marker",
input: "./testdata/snippet_input.md",
markers: []string{"marker"},
expected: "<p>Snippet Content</p>",
expectError: false,
},
{
name: "Test snippet with multiple markers, both",
input: "./testdata/snippet_input.md",
markers: []string{"marker1", "marker2"},
expected: `<p>Snippet Content 1<br />
Snippet Content 2</p>`,
expectError: false,
},
{
name: "Test snippet with multiple markers, single",
input: "./testdata/snippet_input.md",
markers: []string{"marker"},
expected: `<p>Snippet Content</p>`,
expectError: false,
},
// Add more test cases as needed
}
for _, tc := range markdownTestCases {
t.Run(tc.name, func(t *testing.T) {
var inputData []byte
var err error
if existFileOS(tc.input) {
_, inputData, err = readFileOS(tc.input)
if err != nil && !tc.expectError {
t.Errorf("%v: \n Error reading input file: %s: %s", tc.name, tc.input, err)
}
} else {
inputData = []byte(tc.input)
}
md := markdown(readFileOS, existFileOS)
actual := md(string(inputData), tc.markers...)
if tc.expectError {
if actual != "" {
t.Errorf("%v: \n Expected an error, but got a result: %s", tc.name, actual)
}
} else {
actualNode, err := html.Parse(strings.NewReader(string(actual)))
if err != nil {
t.Fatalf("%v: \n failed to parse actual HTML: %v", tc.name, err)
}
expectedNode, err := html.Parse(strings.NewReader(tc.expected))
if err != nil {
t.Fatalf("%v: \n failed to parse expected HTML: %v", tc.name, err)
}
if err := areNodesDeepEqual(actualNode, expectedNode); err != nil {
t.Errorf("%v: \nExpected:\n%s\n\nGot:\n%s\n, err: %v \n", tc.name, tc.expected, actual, err)
}
}
})
}
}
func TestSnippets(t *testing.T) {
testCases := []struct {
description string
in []byte
markers []string
expected []byte
}{
// Test case 1: Single marker with snippet content
{
description: "Single marker with snippet content",
in: []byte("Line 1\n<!-- start marker -->\nSnippet content\n<!-- end marker -->\nLine 4"),
markers: []string{"marker"},
expected: []byte("Snippet content"),
},
// Test case 2: Multiple markers with snippets
{
description: "Multiple markers with snippets",
in: []byte("Line 1\n<!-- start marker1 -->\nSnippet 1\n<!-- end marker1 -->\nLine 4\n<!-- start marker2 -->\nSnippet 2\n<!-- end marker2 -->\nLine 7"),
markers: []string{"marker1", "marker2"},
expected: []byte("Snippet 1\nSnippet 2"),
},
// Test case 3: No markers present, return original input
{
description: "No markers present, return original input",
in: []byte("No markers here"),
markers: []string{"marker"},
expected: []byte("No markers here"),
},
// Test case 4: Empty snippet content between markers, return empty byte slice
{
description: "Empty snippet content between markers, return empty byte slice",
in: []byte("Line 1\n<!-- start marker -->\n\n<!-- end marker -->\nLine 4"),
markers: []string{"marker"},
expected: []byte{},
},
// Test case 5: Start marker with no end marker, return until end of input
{
description: "Start marker with no end marker, return until end of input",
in: []byte("Line 1\n<!-- start marker -->\nSnippet\nLine 4"),
markers: []string{"marker"},
expected: []byte("Snippet\nLine 4"),
},
// Test case 6: Invalid input - empty input byte slice, return original input
{
description: "Invalid input - empty input byte slice, return original input",
in: []byte{},
markers: []string{"marker"},
expected: []byte{},
},
// Test case 7: Invalid input - empty markers slice, return original input
{
description: "Invalid input - empty markers slice, return original input",
in: []byte("Line 1\n<!-- start marker -->\nSnippet\n"),
markers: []string{},
expected: []byte("Line 1\n<!-- start marker -->\nSnippet\n"),
},
// Test case 8: Invalid input - no start marker, return original input
{
description: "Invalid input - no start marker, return original input",
in: []byte("Line 1\nSnippet\n<!-- end marker -->\nLine 4"),
markers: []string{"marker"},
expected: []byte("Line 1\nSnippet\n<!-- end marker -->\nLine 4"),
},
// Test case 9: Invalid input - start and end markers swapped, return original input
{
description: "Invalid input - start and end markers swapped, return original input",
in: []byte("Line 1\n<!-- end marker -->\nSnippet\n<!-- start marker -->\nLine 4"),
markers: []string{"marker"},
expected: []byte("Line 1\n<!-- end marker -->\nSnippet\n<!-- start marker -->\nLine 4"),
},
}
for _, testCase := range testCases {
result := snippets(testCase.in, testCase.markers)
if !bytes.Equal(result, testCase.expected) {
t.Errorf("Test Case: %s\nInput: %s\nMarkers: %v\nExpected: %s\nGot: %s", testCase.description, testCase.in, testCase.markers, testCase.expected, result)
}
}
}