-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathcorner-cases.js
305 lines (277 loc) · 7.2 KB
/
corner-cases.js
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
import { source } from 'common-tags'
import parse from '../../src/cst/parse'
describe('folded block with chomp: keep', () => {
test('nl + nl', () => {
const src = `>+\nblock\n\n`
const doc = parse(src)[0]
expect(doc.contents[0].strValue).toBe('block\n\n')
})
test('nl + nl + sp + nl', () => {
const src = '>+\nab\n\n \n'
const doc = parse(src)[0]
expect(doc.contents[0].strValue).toBe('ab\n\n \n')
})
})
describe('folded block with indent indicator + leading empty lines + leading whitespace', () => {
test('one blank line', () => {
const src = '>1\n\n line\n'
const doc = parse(src)[0]
expect(doc.contents[0].strValue).toBe('\n line\n')
})
test('two blank lines', () => {
const src = '>1\n\n\n line\n'
const doc = parse(src)[0]
expect(doc.contents[0].strValue).toBe('\n\n line\n')
})
})
describe('multiple linebreaks in scalars', () => {
test('plain', () => {
const src = `trimmed\n\n\n\nlines\n`
const doc = parse(src)[0]
expect(doc.contents[0].strValue).toBe('trimmed\n\n\nlines')
})
test('single-quoted', () => {
const src = `'trimmed\n\n\n\nlines'\n`
const doc = parse(src)[0]
expect(doc.contents[0].strValue).toBe('trimmed\n\n\nlines')
})
})
test('no null document for document-end marker', () => {
const src = '---\nx\n...\n'
const docs = parse(src)
expect(docs).toHaveLength(1)
})
test('explicit key after empty value', () => {
const src = 'one:\n? two\n'
const doc = parse(src)[0]
const raw = doc.contents[0].items.map(it => it.rawValue)
expect(raw).toMatchObject(['one', ':', '? two'])
})
test('seq with anchor as explicit key', () => {
const src = '? &key\n- a\n'
const doc = parse(src)[0]
expect(doc.contents).toHaveLength(1)
expect(doc.contents[0].items[0].node.rawValue).toBe('- a')
})
test('unindented single-quoted string', () => {
const src = `key: 'two\nlines'\n`
const doc = parse(src)[0]
const { node } = doc.contents[0].items[1]
expect(node.error).toBeNull()
expect(node.strValue).toMatchObject({
str: 'two lines',
errors: [
new SyntaxError(
'Multi-line single-quoted string needs to be sufficiently indented'
)
]
})
})
describe('seq unindent to non-empty indent', () => {
test('after map', () => {
// const src = `
// - a:| - b| - c|`
const src = `
- a:
- b
- c\n`
const doc = parse(src)[0]
expect(doc.contents).toHaveLength(2)
expect(doc.contents[1].items).toHaveLength(2)
expect(doc.contents[1].items[1].error).toBeNull()
})
test('after seq', () => {
const src = `
-
- a
- b\n`
const doc = parse(src)[0]
expect(doc.contents).toHaveLength(2)
expect(doc.contents[1].items).toHaveLength(2)
expect(doc.contents[1].items[1].error).toBeNull()
})
})
test('eemeli/yaml#10', () => {
const src = `
a:
- b
c: d
`
const doc = parse(src)[0]
expect(doc.contents).toHaveLength(2)
expect(doc.contents[1].items).toHaveLength(4)
expect(doc.contents[1].items[1].error).toBeNull()
})
test('eemeli/yaml#19', () => {
const src = 'a:\n # 123'
const doc = parse(src)[0]
const { items } = doc.contents[0]
expect(items).toHaveLength(2)
expect(items[1].comment).toBe(' 123')
})
test('eemeli/yaml#20', () => {
const src = 'a:\r\n 123\r\nb:\r\n 456\r\n'
const docStream = parse(src)
const a = docStream[0].contents[0].items[1].node
expect(a.strValue).toBe('123')
expect(docStream.setOrigRanges()).toBe(true)
const { origStart: a0, origEnd: a1 } = a.valueRange
expect(src.slice(a0, a1)).toBe('123')
const b = docStream[0].contents[0].items[3].node
expect(b.strValue).toBe('456')
const { origStart: b0, origEnd: b1 } = b.valueRange
expect(src.slice(b0, b1)).toBe('456')
})
test('eemeli/yaml#38', () => {
const src = `
-
- - a
-
`
const doc = parse(src)[0]
const { items } = doc.contents[1]
expect(items).toHaveLength(3)
items.forEach(item => {
expect(item.error).toBe(null)
})
})
test('eemeli/yaml#56', () => {
const src = ':,\n'
const doc = parse(src)[0]
expect(doc.contents).toHaveLength(1)
expect(doc.contents[0]).toMatchObject({
error: null,
strValue: ':,',
type: 'PLAIN'
})
})
describe('collection indicator as last char', () => {
test('seq item', () => {
const src = '-'
const doc = parse(src)[0]
expect(doc.contents[0]).toMatchObject({
type: 'SEQ',
items: [{ type: 'SEQ_ITEM', node: null }]
})
})
test('explicit map key', () => {
const src = '?'
const doc = parse(src)[0]
expect(doc.contents[0]).toMatchObject({
type: 'MAP',
items: [{ type: 'MAP_KEY', node: null }]
})
})
test('empty map value', () => {
const src = ':'
const doc = parse(src)[0]
expect(doc.contents[0]).toMatchObject({
type: 'MAP',
items: [{ type: 'MAP_VALUE', node: null }]
})
})
test('indented seq-in-seq', () => {
const src = ` -\n - - a\n -`
const doc = parse(src)[0]
expect(doc.contents[0]).toMatchObject({
items: [
{ error: null },
{ node: { items: [{ node: { type: 'PLAIN', strValue: 'a' } }] } },
{ error: null }
]
})
})
})
test('parse an empty string as an empty document', () => {
const doc = parse('')[0]
expect(doc).toMatchObject({
error: null,
contents: []
})
})
test('re-stringify flow seq with comments', () => {
const src = '[ #c\n1, #d\n2 ]\n'
const doc = parse(src)
expect(String(doc)).toBe(src)
})
test('blank line after less-indented comment (eemeli/yaml#91)', () => {
const src = `
foo1: bar
# comment
foo2: baz`
const doc = parse(src)
expect(doc).toHaveLength(1)
expect(doc[0].contents).toMatchObject([
{ type: 'BLANK_LINE' },
{ type: 'MAP' }
])
})
describe('flow collection as same-line mapping key value', () => {
test('eemeli/yaml#113', () => {
const src = source`
---
foo:
bar:
enum: [
"abc",
"cde"
]
`
const doc = parse(src)
const barValue = doc[0].contents[0].items[1].node.items[1].node
expect(barValue.items[1].node).toMatchObject({
error: null,
items: [
{ char: '[' },
{ type: 'QUOTE_DOUBLE' },
{ char: ',' },
{ type: 'QUOTE_DOUBLE' },
{ char: ']' }
],
type: 'FLOW_SEQ'
})
})
test('eemeli/yaml#114', () => {
const src = source`
foo: {
bar: boom
}
`
const doc = parse(src)
const flowCollection = doc[0].contents[0].items[1].node
expect(flowCollection).toMatchObject({
error: null,
items: [
{ char: '{' },
{ type: 'PLAIN' },
{ char: ':' },
{ type: 'PLAIN' },
{ char: '}' }
],
type: 'FLOW_MAP'
})
})
test('Fails on insufficient indent', () => {
const src = source`
foo: {
bar: boom
}
`
const doc = parse(' ' + src)
const flowCollection = doc[0].contents[0].items[1].node
expect(flowCollection).toMatchObject({
error: {
message: 'Insufficient indentation in flow collection',
name: 'YAMLSemanticError'
},
items: [
{ char: '{' },
{ type: 'PLAIN' },
{ char: ':' },
{ type: 'PLAIN' },
{ char: '}' }
],
type: 'FLOW_MAP'
})
})
})