forked from artempyanykh/marksman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserTests.fs
318 lines (257 loc) · 9.34 KB
/
ParserTests.fs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
module Marksman.ParserTests
open Ionide.LanguageServerProtocol.Types
open Snapper
open Snapper.Attributes
open Xunit
open Marksman.Cst
open Marksman.Parser
open Marksman.Misc
open Marksman.Helpers
let checkSnapshot (document: array<Element>) =
let lines =
Array.map (fun x -> (Element.fmt x).Lines()) document |> Array.concat
lines.ShouldMatchSnapshot()
let checkInlineSnapshot = checkInlineSnapshot Element.fmt
let scrapeString content = parseText (Text.mkText content)
[<StoreSnapshotsPerClass>]
module HeadingTests =
[<Fact>]
let parse_empty () =
let text = ""
let document = scrapeString text
checkSnapshot document
[<Fact>]
let parse_title_single () =
let text = "# Title text"
let document = scrapeString text
checkSnapshot document
[<Fact>]
let parse_title_multiple () =
let text = "# Title 1\n# Title ... (2)\r\n# 3rd Title"
let document = scrapeString text
checkSnapshot document
[<Fact>]
let parse_title_with_child_paragraph () =
let text = "# Title 1\nSome text\r\n# Title 2"
let document = scrapeString text
checkSnapshot document
[<Fact>]
let parse_nested_headings () =
let text = "# H1 \n## H2.1\n## H2.2\n"
let document = scrapeString text
checkSnapshot document
[<StoreSnapshotsPerClass>]
module WikiLinkTests =
[<Fact>]
let parser_xref_note () =
// 01234567890123456
let text = "[[note]]"
let document = scrapeString text
checkInlineSnapshot document [ "WL: [[note]]; (0,0)-(0,8)"; " doc=note; (0,2)-(0,6)" ]
[<Fact>]
let parser_xref_note_heading () =
// 01234567890123456
let text = "[[note#heading]]"
let document = scrapeString text
checkInlineSnapshot
document
[ "WL: [[note#heading]]; (0,0)-(0,16)"
" doc=note; (0,2)-(0,6)"
" head=heading; (0,7)-(0,14)" ]
[<Fact>]
let parser_xref_text_before () =
// 0123456789012
let text = "Before [[N]]"
let document = scrapeString text
checkInlineSnapshot document [ "WL: [[N]]; (0,7)-(0,12)"; " doc=N; (0,9)-(0,10)" ]
[<Fact>]
let parser_xref_text_after () =
// 0123456789012345
let text = "[[note]]! Other"
let document = scrapeString text
checkInlineSnapshot document [ "WL: [[note]]; (0,0)-(0,8)"; " doc=note; (0,2)-(0,6)" ]
[<Fact>]
let parser_xref_text_around () =
// 0123456789012
let text = "To [[note]]!"
let document = scrapeString text
checkInlineSnapshot document [ "WL: [[note]]; (0,3)-(0,11)"; " doc=note; (0,5)-(0,9)" ]
[<Fact>]
let parser_xref_2nd_line () =
// 1 2 3
// 0123456789012345678901234567890
let text = "# H1\nThis is [[note]] huh!\n"
// 01234 0123456789012345678901
let document = scrapeString text
checkSnapshot document
[<Fact>]
let parser_wiki_empty_heading () =
// 0123456
let text = "[[T#]]"
let doc = scrapeString text
checkInlineSnapshot
doc
[ "WL: [[T#]]; (0,0)-(0,6)" //
" doc=T; (0,2)-(0,3)"
" head=; (0,4)-(0,4)" ]
[<Fact>]
let parse_wiki_escaped_hash () =
// 01234567
let text = "[[F\#]]"
let doc = scrapeString text
checkSnapshot doc
[<Fact>]
let parse_wiki_escaped_hash_and_heading () =
// 0123456789012345
let text = "[[F\##Section]]"
let doc = scrapeString text
checkSnapshot doc
[<Fact>]
let parse_wiki_escaped_hash_and_heading_with_hash () =
// 0123456789012345
let text = "[[F\##Section #3]]"
let doc = scrapeString text
checkSnapshot doc
[<Fact>]
let complex_example_1 () =
let text =
// 1 2 3 4 5
//1234 5 67890123 45678901234567 890123 45678901 23456789012345678901
"# H1\n\n## H2.1\nP2.1 [[ref1]]\n[[cp1\n## H2.2\nP2.2 [:cp2 next"
// 0 12 3 4 5 6
let document = scrapeString text
checkSnapshot document
[<StoreSnapshotsPerClass>]
module MdLinkTest =
[<Fact>]
let parser_link_1 () =
// 0123456789012
let text = "[title](url)"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [title](url) @ (0,0)-(0,12)"
" IL: label=title @ (0,1)-(0,6); url=url @ (0,8)-(0,11); title=∅" ]
[<Fact>]
let parser_link_2 () =
// Without any text inside this is not considered a link
let text = "[]"
let document = scrapeString text
checkInlineSnapshot document []
[<Fact>]
let parser_link_3 () =
// Without any text inside this is not considered a link
let text = "[][]"
let document = scrapeString text
checkInlineSnapshot document []
[<Fact>]
let parser_link_4 () =
// This is considered a link even though the contents are empty
let text = "[]()"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: []() @ (0,0)-(0,4)"
" IL: label= @ (0,0)-(0,0); url=∅; title=∅" ]
[<Fact>]
let parser_link_5 () =
let text = "[la bel](url \"title\")"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [la bel](url \"title\") @ (0,0)-(0,21)"
" IL: label=la bel @ (0,1)-(0,7); url=url @ (0,9)-(0,12); title=title @ (0,13)-(0,20)" ]
[<Fact>]
let parser_link_6 () =
let text = "[la bel](url title)" // without quotation of title only the shortcut parses
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [la bel] @ (0,0)-(0,8)" //
" RS: label=la bel @ (0,1)-(0,7)" ]
[<Fact>]
let parser_link_7 () =
let text = "[](url)"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [](url) @ (0,0)-(0,7)"
" IL: label= @ (0,0)-(0,0); url=url @ (0,3)-(0,6); title=∅" ]
[<Fact>]
let parser_link_8 () =
let text = "[short_cut]"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [short_cut] @ (0,0)-(0,11)"
" RS: label=short_cut @ (0,1)-(0,10)" ]
[<Fact>]
let parser_link_9 () =
let text = "[short cut][]"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [short cut][] @ (0,0)-(0,13)"
" RC: label=short cut @ (0,1)-(0,10)" ]
[<Fact>]
let parser_link_10 () =
let text = "[label][ref]"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [label][ref] @ (0,0)-(0,12)"
" RF: text=label @ (0,1)-(0,6); label=ref @ (0,8)-(0,11)" ]
[<Fact>]
let parser_link_11 () =
let text = "[foo]: /foo 'foo title'"
let document = scrapeString text
checkInlineSnapshot
document
[ "MLD: [foo]: /foo 'foo title' @ (0,0)-(0,23)"
" label=foo @ (0,1)-(0,4); url=/foo @ (0,7)-(0,11); title=foo title @ (0,12)-(0,23)" ]
[<Fact>]
let parser_link_12 () =
let text = "[foo]: /foo"
let document = scrapeString text
checkInlineSnapshot
document
[ "MLD: [foo]: /foo @ (0,0)-(0,11)"
" label=foo @ (0,1)-(0,4); url=/foo @ (0,7)-(0,11); title=∅" ]
[<Fact>]
let parser_link_13 () =
let text = "[foo]: /bar\nHere comes [foo]."
let document = scrapeString text
checkInlineSnapshot
document
[ "MLD: [foo]: /bar @ (0,0)-(0,11)"
" label=foo @ (0,1)-(0,4); url=/bar @ (0,7)-(0,11); title=∅"
"ML: [foo] @ (1,11)-(1,16)"
" RS: label=foo @ (1,12)-(1,15)" ]
[<Fact>]
let parser_link_14 () =
let text = "[label][ref]\n\n[ref]: https://some.url"
let document = scrapeString text
checkInlineSnapshot
document
[ "ML: [label][ref] @ (0,0)-(0,12)"
" RF: text=label @ (0,1)-(0,6); label=ref @ (0,8)-(0,11)"
"MLD: [ref]: https://some.url @ (2,0)-(2,23)"
" label=ref @ (2,1)-(2,4); url=https://some.url @ (2,7)-(2,23); title=∅" ]
module DocUrlTests =
let mkTextNode str = Node.mkText str (Range.Mk(0, 0, 0, str.Length))
[<Fact>]
let test1 () =
let actual = mkTextNode "/some.md" |> Url.ofUrlNode
Assert.Equal("docUrl=/some.md @ (0,0)-(0,8)", actual.ToString())
[<Fact>]
let test2 () =
let actual = mkTextNode "/some.md#anchor" |> Url.ofUrlNode
Assert.Equal(
"docUrl=/some.md @ (0,0)-(0,8);anchor=anchor @ (0,9)-(0,15)",
actual.ToString()
)
[<Fact>]
let test3 () =
// 01234567
let actual = mkTextNode "#anchor" |> Url.ofUrlNode
Assert.Equal("anchor=anchor @ (0,1)-(0,7)", actual.ToString())