-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_test.go
More file actions
267 lines (228 loc) · 10.3 KB
/
Copy pathfix_test.go
File metadata and controls
267 lines (228 loc) · 10.3 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package markdownflavor
import (
"testing"
"github.com/jeduden/mdsmith/pkg/goldmark/ast"
extast "github.com/jeduden/mdsmith/pkg/goldmark/extension/ast"
"github.com/jeduden/mdsmith/pkg/goldmark/text"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/pkg/markdown/flavor/ext"
)
// fixWith parses src into a *lint.File, applies the configured rule's
// Fix, and returns the result as a string for compact assertion.
func fixWith(t *testing.T, flavor, src string) string {
t.Helper()
r := &Rule{}
require.NoError(t, r.ApplySettings(map[string]any{"flavor": flavor}))
return string(r.Fix(mkFile(t, src)))
}
// --- heading IDs --------------------------------------------------------
func TestRuleFixHeadingIDRemovesAttributeBlock(t *testing.T) {
got := fixWith(t, "commonmark", "# Heading {#top}\n\nBody text.\n")
assert.Equal(t, "# Heading\n\nBody text.\n", got)
}
func TestRuleFixHeadingIDPreservesTrailingNewline(t *testing.T) {
// A heading at end-of-file with no trailing newline must stay that way.
got := fixWith(t, "commonmark", "# Heading {#top}")
assert.Equal(t, "# Heading", got)
}
func TestRuleFixHeadingIDMultiple(t *testing.T) {
src := "# A {#a}\n\n## B {#b}\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t, "# A\n\n## B\n", got)
}
func TestRuleFixHeadingIDGoldmarkAccepts(t *testing.T) {
// goldmark supports heading IDs, so Fix must not strip them.
src := "# Heading {#top}\n"
got := fixWith(t, "goldmark", src)
assert.Equal(t, src, got)
}
// --- strikethrough ------------------------------------------------------
func TestRuleFixStrikethroughRemovesMarkers(t *testing.T) {
got := fixWith(t, "commonmark", "Text ~~crossed out~~ here.\n")
assert.Equal(t, "Text crossed out here.\n", got)
}
func TestRuleFixStrikethroughGFMAccepts(t *testing.T) {
src := "Text ~~crossed out~~ here.\n"
got := fixWith(t, "gfm", src)
assert.Equal(t, src, got)
}
// --- task lists ---------------------------------------------------------
func TestRuleFixTaskListRemovesMarker(t *testing.T) {
src := "- [x] done\n- [ ] todo\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t, "- done\n- todo\n", got)
}
func TestRuleFixTaskListPreservesBullet(t *testing.T) {
// The plan calls out `*` and `+` bullets explicitly.
src := "* [X] one\n+ [ ] two\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t, "* one\n+ two\n", got)
}
func TestRuleFixTaskListGFMAccepts(t *testing.T) {
src := "- [x] done\n"
got := fixWith(t, "gfm", src)
assert.Equal(t, src, got)
}
// --- superscript --------------------------------------------------------
func TestRuleFixSuperscriptRemovesCarets(t *testing.T) {
got := fixWith(t, "commonmark", "E = mc^2^ is famous.\n")
assert.Equal(t, "E = mc2 is famous.\n", got)
}
// --- subscript ----------------------------------------------------------
func TestRuleFixSubscriptRemovesTildes(t *testing.T) {
got := fixWith(t, "commonmark", "H~2~O is water.\n")
assert.Equal(t, "H2O is water.\n", got)
}
// --- bare URLs ----------------------------------------------------------
func TestRuleFixBareURLWrapsInAngleBrackets(t *testing.T) {
got := fixWith(t, "commonmark",
"Visit https://example.com for details.\n")
assert.Equal(t,
"Visit <https://example.com> for details.\n", got)
}
func TestRuleFixBareURLGFMAccepts(t *testing.T) {
src := "Visit https://example.com for details.\n"
got := fixWith(t, "gfm", src)
assert.Equal(t, src, got)
}
// --- combined -----------------------------------------------------------
// TestRuleFixMultipleFeaturesOnOneLine covers the ascending single-
// pass build in applyEdits: two byte-range edits on the same line
// must compose into a contiguous output without one corrupting the
// other's spans.
func TestRuleFixMultipleFeaturesOnOneLine(t *testing.T) {
got := fixWith(t, "commonmark",
"# Heading {#top}\n\nText ~~old~~ at https://example.com.\n")
assert.Equal(t,
"# Heading\n\nText old at <https://example.com>.\n", got)
}
// TestRuleFixAlertAndByteRangeFeatureCompose verifies that Fix() runs
// alerts stripping AND byte-range fixes in the same call. Before the
// pipeline composition fix, alerts caused an early return that left
// strikethrough / bare URLs / heading IDs unfixed in alert-bearing
// docs; the next fixer pass would catch them, but it required two
// passes for what should be one.
func TestRuleFixAlertAndByteRangeFeatureCompose(t *testing.T) {
src := "> [!NOTE]\n> Visit https://example.com for ~~old~~ details.\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t,
"> Visit <https://example.com> for old details.\n", got)
}
// TestFixGitHubAlerts_LazyContinuation verifies that fixGitHubAlerts
// re-adds the "> " prefix to lazy-continuation lines (lines in the
// first paragraph that lack a blockquote marker in the raw source)
// after stripping the [!TYPE] marker line. This exercises the addPrefix
// map in fixGitHubAlerts.
func TestFixGitHubAlerts_LazyContinuation(t *testing.T) {
// The second line is a CommonMark lazy continuation: no "> " prefix.
src := "> [!NOTE]\nThis is lazy-continuation body.\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t, "> This is lazy-continuation body.\n", got)
}
// TestFixGitHubAlerts_IndentedLazyContinuation covers the leading-
// whitespace branch of the addPrefix rewrite (rule.go's
// line[:len(line)-len(trimmed)] slice): the continuation line's
// existing indent must be preserved ahead of the re-added "> ".
//
// This test is scoped to a 3-space indent, where preserving it keeps
// the result a valid blockquote. An indented chunk cannot interrupt a
// paragraph (CommonMark §4.4), so a continuation indented 4+ spaces is
// still lazy continuation too — addPrefix fires for it exactly the
// same way — but preserving 4+ spaces of indent ahead of "> " makes
// the rewritten line parse as indented code, not a blockquote,
// silently turning the alert body into a code block. That bug
// predates this PR (byte-identical on origin/main) and this rewrite
// does not change it; it is not covered by a test here because fixing
// it is out of scope for this performance-focused change.
func TestFixGitHubAlerts_IndentedLazyContinuation(t *testing.T) {
src := "> [!NOTE]\n indented lazy continuation body.\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t, " > indented lazy continuation body.\n", got)
}
// TestRuleFixStrikethroughWithNestedInlineSkips guards the robustness
// of delimiterPairEdits: a wrapper containing nested inline markup
// (emphasis, link, code span) cannot be safely unwrapped without
// tracking each nested marker's own span, so the fix declines and the
// diagnostic remains for the user.
func TestRuleFixStrikethroughWithNestedInlineSkips(t *testing.T) {
src := "Text ~~*bold*~~ here.\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t, src, got)
}
// TestRuleFixStrikethroughWithMixedChildrenSkips exercises the
// sibling loop in delimiterPairEdits: a wrapper whose first child is
// Text but whose later children include nested inline markup must
// still skip the fix.
func TestRuleFixStrikethroughWithMixedChildrenSkips(t *testing.T) {
src := "Text ~~start *mid* end~~ here.\n"
got := fixWith(t, "commonmark", src)
assert.Equal(t, src, got)
}
// TestRuleFixFlavorAnyIsNoop covers the early-out paths in Fix and
// fixByteRangeFeatures when the flavor accepts every tracked feature.
func TestRuleFixFlavorAnyIsNoop(t *testing.T) {
src := "# Head {#top}\n\nText ~~strike~~ and https://example.com\n"
got := fixWith(t, "any", src)
assert.Equal(t, src, got)
}
// TestRuleDualNodeEditsSupportedFeaturesReturnNil exercises the
// "feature is supported" branches in dualNodeEdits for super/sub.
// Pandoc supports every dual-parser feature so needsAnyDualFix is
// false in real Fix calls; we invoke dualNodeEdits directly with a
// constructed AST so the supports-branch returns are reached.
func TestRuleDualNodeEditsSupportedFeaturesReturnNil(t *testing.T) {
r := &Rule{}
require.NoError(t, r.ApplySettings(map[string]any{"flavor": "pandoc"}))
assert.Nil(t, r.dualNodeEdits(nil, &ext.SuperscriptNode{}))
assert.Nil(t, r.dualNodeEdits(nil, &ext.SubscriptNode{}))
}
// TestTaskCheckBoxEditsDeclinesOnNonBracketStart hand-constructs an
// AST that breaks the goldmark task-list invariant — a TaskCheckBox
// whose NearestBlockAncestor returns a block starting at a non-'['
// byte (here a bare paragraph). The guard must decline rather than
// silently delete the wrong three bytes; this is the F5 case the
// per-node Lines-empty check missed.
func TestTaskCheckBoxEditsDeclinesOnNonBracketStart(t *testing.T) {
// Source where offset 0 is 'p', not '['; NearestBlockAncestor's
// returned block.Lines().At(0).Start will point at 'p'.
src := []byte("plain paragraph text\n")
f, err := lint.NewFile("t.md", src)
require.NoError(t, err)
para := ast.NewParagraph()
para.Lines().Append(text.NewSegment(0, len(src)-1))
cb := extast.NewTaskCheckBox(true)
para.AppendChild(para, cb)
got := taskCheckBoxEdits(f, cb)
assert.Nil(t, got,
"taskCheckBoxEdits must decline when Lines.At(0) is not at '['")
}
// TestTaskCheckBoxEditsDeclinesOnNilBlock exercises the block==nil
// guard with a TaskCheckBox that has no parent at all.
func TestTaskCheckBoxEditsDeclinesOnNilBlock(t *testing.T) {
src := []byte("- [ ] task\n")
f, err := lint.NewFile("t.md", src)
require.NoError(t, err)
orphan := extast.NewTaskCheckBox(true)
assert.Nil(t, taskCheckBoxEdits(f, orphan))
}
// TestTaskCheckBoxEditsDeclinesWhenBracketRunsPastEOF guards the
// end > len(source) branch: a TaskCheckBox at offset len-1 (last
// byte is '[' with no body after it) cannot produce a 3-byte edit.
func TestTaskCheckBoxEditsDeclinesWhenBracketRunsPastEOF(t *testing.T) {
// Two bytes: "[\n" — start points at '['; start+3 exceeds the
// 2-byte body. The fix must decline rather than build an edit
// whose End is past EOF (which markdown.Splice would panic on).
src := []byte("[\n")
f, err := lint.NewFile("t.md", src)
require.NoError(t, err)
para := ast.NewParagraph()
para.Lines().Append(text.NewSegment(0, len(src)))
cb := extast.NewTaskCheckBox(true)
para.AppendChild(para, cb)
assert.Nil(t, taskCheckBoxEdits(f, cb))
}
// Splice's single-pass behaviour (adjacent edits, pure insertion,
// replacement bytes) is exercised in pkg/markdown's TestSplice; the
// rule layer just feeds edits into markdown.Splice.