-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocate_test.go
More file actions
262 lines (233 loc) · 8.4 KB
/
Copy pathlocate_test.go
File metadata and controls
262 lines (233 loc) · 8.4 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
package index
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLocateHeading(t *testing.T) {
t.Parallel()
src := "# Top heading\n\nbody\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 1, 3)
assert.Equal(t, TokenHeading, res.Tag)
assert.Equal(t, "top-heading", res.Anchor)
assert.Equal(t, 1, res.Level)
}
func TestLocateAnchorLink(t *testing.T) {
t.Parallel()
src := "# Top\n\nSee [here](#sec).\n\n## Sec\n"
// Cursor inside `[here](#sec)` (line 3, col 8).
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 14)
assert.Equal(t, TokenAnchorLink, res.Tag)
assert.Equal(t, "sec", res.TargetAnchor)
}
func TestLocateFileLink(t *testing.T) {
t.Parallel()
src := "# Top\n\n[a](./other.md#sub)\n"
res := Locator{Path: "doc.md"}.Locate([]byte(src), 3, 6)
assert.Equal(t, TokenFileLink, res.Tag)
assert.Equal(t, "other.md", res.TargetFile)
assert.Equal(t, "sub", res.TargetAnchor)
}
func TestLocateRefUse(t *testing.T) {
t.Parallel()
src := "# Top\n\nSee [linked][label].\n\n[label]: https://example.com\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 8)
assert.Equal(t, TokenRefUse, res.Tag)
assert.Equal(t, "label", res.Label)
}
func TestLocateRefDef(t *testing.T) {
t.Parallel()
src := "# T\n\n[See][label]\n\n[label]: https://example.com\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 5, 3)
assert.Equal(t, TokenRefDef, res.Tag)
assert.Equal(t, "label", res.Label)
}
func TestLocateDirectiveArg(t *testing.T) {
t.Parallel()
src := strings.Join([]string{
"# Top",
"",
"<?include",
`file: "x.md"`,
"?>",
"<?/include?>",
"",
}, "\n")
res := Locator{Path: "a.md"}.Locate([]byte(src), 4, 8)
assert.Equal(t, TokenDirectiveArg, res.Tag)
assert.Equal(t, "include", res.DirectiveName)
assert.Equal(t, "file", res.DirectiveArg)
assert.Equal(t, "x.md", res.DirectiveValue)
assert.Equal(t, "x.md", res.DirectiveTargetFile)
}
func TestLocateFrontMatterKey(t *testing.T) {
t.Parallel()
src := "---\ntitle: Hello\nkinds:\n - guide\n---\n# Body\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 2, 2)
assert.Equal(t, TokenFrontMatterKey, res.Tag)
assert.Equal(t, "title", res.FrontMatterKey)
}
func TestLocateFrontMatterValue(t *testing.T) {
t.Parallel()
src := "---\ntitle: Hello\nkind: guide\n---\n# Body\n"
// Cursor after the colon on line 3.
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 8)
assert.Equal(t, TokenFrontMatterValue, res.Tag)
assert.Equal(t, "kind", res.FrontMatterKey)
assert.Equal(t, "guide", res.FrontMatterValue)
}
func TestLocateFileTop(t *testing.T) {
t.Parallel()
src := "# Top\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 1, 1)
assert.Equal(t, TokenFileTop, res.Tag)
}
func TestLocateNoneOnPlainProse(t *testing.T) {
t.Parallel()
src := "# Top\n\nordinary text without links\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 5)
assert.Equal(t, TokenNone, res.Tag)
}
func TestLocateFrontMatterKindsListItem(t *testing.T) {
t.Parallel()
src := "---\ntitle: T\nkinds:\n - guide\n - reference\n---\n# Body\n"
// Cursor on ` - guide` line (line 4).
res := Locator{Path: "a.md"}.Locate([]byte(src), 4, 5)
assert.Equal(t, TokenFrontMatterValue, res.Tag)
assert.Equal(t, "kinds", res.FrontMatterKey)
assert.Equal(t, "guide", res.FrontMatterValue)
}
func TestLocateOutOfRangeSafe(t *testing.T) {
t.Parallel()
src := "# Top\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), -1, -1)
// negative coords clamp to (1, 1) which is FileTop on body line 1.
assert.Equal(t, TokenFileTop, res.Tag)
res = Locator{Path: "a.md"}.Locate([]byte(src), 99, 99)
assert.Equal(t, TokenNone, res.Tag)
}
func TestLocateFrontMatterEmptyKey(t *testing.T) {
t.Parallel()
src := "---\nfoo bar baz\n---\n# Body\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 2, 2)
assert.Equal(t, TokenNone, res.Tag)
}
func TestLocateRefDefOnNonRefLine(t *testing.T) {
t.Parallel()
src := "# T\n\nplain text\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 5)
assert.Equal(t, TokenNone, res.Tag)
}
func TestLocateBuildDirectiveInputListItem(t *testing.T) {
t.Parallel()
// Cursor on an inputs: list item resolves to that input file so
// go-to-definition works on a <?build?> input.
src := "# Top\n\n<?build\nrecipe: r\ninputs:\n - \"x.md\"\noutputs:\n - out.html\n?>\n<?/build?>\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 6, 6)
assert.Equal(t, TokenDirectiveArg, res.Tag)
assert.Equal(t, "build", res.DirectiveName)
assert.Equal(t, "inputs", res.DirectiveArg)
assert.Equal(t, "x.md", res.DirectiveTargetFile)
}
func TestLocateFileLinkResolvesAgainstSourceDir(t *testing.T) {
t.Parallel()
// Source file lives in `docs/`; the relative link `./b.md`
// resolves to `docs/b.md`, not bare `b.md`.
src := "# Top\n\n[next](./b.md)\n"
res := Locator{Path: "docs/a.md"}.Locate([]byte(src), 3, 4)
assert.Equal(t, TokenFileLink, res.Tag)
assert.Equal(t, "docs/b.md", res.TargetFile)
}
func TestLocateFileLinkEscapingRoot(t *testing.T) {
t.Parallel()
src := "# Top\n\n[bad](../up.md)\n"
res := Locator{Path: "docs/a.md"}.Locate([]byte(src), 3, 4)
assert.Equal(t, TokenFileLink, res.Tag)
// `../up.md` from `docs/a.md` resolves to bare `up.md`.
assert.Equal(t, "up.md", res.TargetFile)
}
func TestLocateHeadingWithDuplicateAnchorDisambiguates(t *testing.T) {
t.Parallel()
src := "# Same\n\n# Same\n\n# Same\n"
// Cursor on the second `# Same` line (line 3) — slug is
// disambiguated to "same-1".
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 3)
assert.Equal(t, TokenHeading, res.Tag)
assert.Equal(t, "same-1", res.Anchor)
}
func TestLocateRefDefOnLineWithoutLabel(t *testing.T) {
t.Parallel()
src := "# T\n\n# Other heading\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 3)
// Heading on this line, not a ref def.
assert.Equal(t, TokenHeading, res.Tag)
}
func TestLocateCursorAfterLinkOnSameLine(t *testing.T) {
t.Parallel()
// Cursor sits on the prose after a link on the same line. The
// previous bound stretched the link's range to end-of-line and
// would mis-tag this position as TokenAnchorLink.
src := "# T\n\nSee [a](#sec) and then plain prose here.\n\n## Sec\n"
// Column 28 is somewhere in `plain prose here`.
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 28)
assert.NotEqual(t, TokenAnchorLink, res.Tag,
"cursor in trailing prose must not be tagged as link, got %+v", res)
}
func TestLocateRefStyleInlineLink(t *testing.T) {
t.Parallel()
// Reference-style link: cursor inside `[text][label]` should
// surface as TokenRefUse; cursor on the prose after the link
// must not.
src := "# T\n\nSee [text][lab] here.\n\n[lab]: https://x.com\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 8)
assert.Equal(t, TokenRefUse, res.Tag)
res = Locator{Path: "a.md"}.Locate([]byte(src), 3, 22)
assert.NotEqual(t, TokenRefUse, res.Tag)
}
func TestLocateEmptyAnchorOnRefDef(t *testing.T) {
t.Parallel()
src := "# T\n\n[label]: https://x.com\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 3)
assert.Equal(t, TokenRefDef, res.Tag)
assert.Equal(t, "label", res.Label)
}
func TestLocatePIContainsLineMultiline(t *testing.T) {
t.Parallel()
src := "# T\n\n<?include\nfile: \"x.md\"\nstrip-frontmatter: \"true\"\n?>\n<?/include?>\n"
// Line 5 is inside the multi-line PI.
res := Locator{Path: "a.md"}.Locate([]byte(src), 5, 1)
assert.Equal(t, TokenDirectiveArg, res.Tag)
assert.Equal(t, "include", res.DirectiveName)
}
func TestLocatePIWithSingleLineClosure(t *testing.T) {
t.Parallel()
src := "# T\n\n<?allow-empty-section?>\n"
res := Locator{Path: "a.md"}.Locate([]byte(src), 3, 5)
assert.Equal(t, TokenDirectiveArg, res.Tag)
assert.Equal(t, "allow-empty-section", res.DirectiveName)
}
func TestSafeURLPathEscape(t *testing.T) {
t.Parallel()
assert.Equal(t, "abc", SafeURLPathEscape("abc"))
assert.Contains(t, SafeURLPathEscape("a b"), "%20")
}
func TestLocateFrontMatterKindsListItemWithDifferentValues(t *testing.T) {
t.Parallel()
src := "---\nkinds:\n - guide\n - reference\n---\n# Body\n"
// Cursor on the second list item.
res := Locator{Path: "a.md"}.Locate([]byte(src), 4, 5)
assert.Equal(t, TokenFrontMatterValue, res.Tag)
assert.Equal(t, "kinds", res.FrontMatterKey)
assert.Equal(t, "reference", res.FrontMatterValue)
}
func TestEnclosingListKey_FindsParentKey(t *testing.T) {
lines := [][]byte{
[]byte("inputs:"),
[]byte(" - alpha"),
[]byte(" - beta"),
[]byte(" - gamma"),
}
// Line 4 (1-based) is "gamma"; the enclosing key is "inputs".
got := enclosingListKey(lines, 4)
assert.Equal(t, "inputs", got)
}