File: ext_yaml_parser_spec.yaml

package info (click to toggle)
lua-yaml 5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,896 kB
  • ctags: 673
  • sloc: sh: 11,895; ansic: 1,013; makefile: 34
file content (388 lines) | stat: -rw-r--r-- 16,746 bytes parent folder | download | duplicates (2)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
specify parsing:
- it parses empty streams:
    e = yaml.parser ""
    expect (e ().type).to_be "STREAM_START"
    expect (e ().type).to_be "STREAM_END"
    expect (e ()).to_be (nil)
    expect (e ()).to_be (nil)
- it ignores comments: '
    e = yaml.parser "# A comment\nnon-comment # trailing comment\n"
    expect (e ().type).to_be "STREAM_START"
    expect (e ().type).to_be "DOCUMENT_START"
    expect (e ().value).to_be "non-comment"
    expect (e ().type).to_be "DOCUMENT_END"'

- describe STREAM_START:
  - before:
      e = yaml.parser "# no BOM"
  - it is the first event:
      expect (e ().type).to_be "STREAM_START"
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 0, column = 0, index = 0}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 0, column = 0, index = 0}
  - it uses UTF-8 by default:
      expect (e ().encoding).to_be "UTF8"
  - it recognizes UTF-16 BOM:
      e = yaml.parser (BOM .. " BOM")
      expect (e ().encoding).to_match "UTF16[BL]E"

- describe STREAM_END:
  - before:
      for t in yaml.parser "nothing to see" do ev = t end
  - it is the last event:
      expect (ev.type).to_be "STREAM_END"
  - it reports event start marker:
      expect (ev.start_mark).to_equal {line = 1, column = 0, index = 14}
  - it reports event end marker:
      expect (ev.end_mark).to_equal {line = 1, column = 0, index = 14}

- describe DOCUMENT_START:
  - before:
      e = consume (1, "---")
  - it recognizes document start marker:
      expect (filter (e (), "type", "implicit")).
        to_equal {type = "DOCUMENT_START", implicit = false}
  - it reports implicit document start:
      e = consume (1, "foo")
      expect (e ().implicit).to_be (true)
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 0, column = 0, index = 0}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 0, column = 3, index = 3}

  - context parser directives:
    - it can recognize document versions:
        e = consume (1, "%YAML 1.1\n---")
        expect (e ().version_directive).to_equal {major = 1, minor = 1}
    - it can diagnose missing document start:
        e = consume (1, "%YAML 1.1\n")
        expect (e ()).to_error "expected <document start>"
    - it can diagnose multiple versions:
        e = consume (1, "%YAML 1.1\n%YAML 1.1\n---")
        expect (e ()).to_error "duplicate %YAML directive"
    - it can diagnose too-new versions:
        e = consume (1, "%YAML 2.0\n---")
        expect (e ()).to_error "incompatible YAML document"
    - it warns of newer minor versions:
        pending (github_issue "1")
        e = consume (1, "%YAML 1.9\n---")
        expect (e ()).
          to_error "attempting parsing of newer minor document version"

    - it can recognize primary tag handles:
        e = consume (1, "%TAG ! tag:ben-kiki.org,2000:app/\n---")
        expect (e ().tag_directives).
          to_equal {{handle = "!", prefix = "tag:ben-kiki.org,2000:app/"}}
    - it can recognize secondary tag handles:
        e = consume (1, "%TAG !! tag:yaml.org,2002:\n---")
        expect (e ().tag_directives).
          to_equal {{handle = "!!", prefix = "tag:yaml.org,2002:"}}
    - it can recognize named tag handles:
        e = consume (1, "%TAG !o! tag:ben-kiki.org,2000:\n---")
        expect (e ().tag_directives).
          to_equal {{handle = "!o!", prefix = "tag:ben-kiki.org,2000:"}}
    - it can concatenate multiple tag handles:
        e = consume (1, "%TAG ! !\n" ..
                        "%TAG !! tag:yaml.org,2002:\n" ..
                        "%TAG !o! tag:ben-kiki.org,2000:\n" ..
                        "---")
        expect (e ().tag_directives).to_contain.
          all_of {{handle = "!", prefix = "!"},
                  {handle = "!!", prefix = "tag:yaml.org,2002:"},
                  {handle = "!o!", prefix = "tag:ben-kiki.org,2000:"}}
    - it can diagnose missing document start:
        e = consume (1, "%TAG ! !\n")
        expect (e ()).to_error "expected <document start>"

- describe DOCUMENT_END:
  - before:
      e = consume (3, "foo\n...")
  - it recognizes the document end marker:
      expect (filter (e (), "type", "implicit")).
        to_equal {type = "DOCUMENT_END", implicit = false}
  - it reports an implicit document end marker:
      e = consume (3, "foo\n")
      expect (filter (e (), "type", "implicit")).
        to_equal {type = "DOCUMENT_END", implicit = true}
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 1, column = 0, index = 4}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 1, column = 3, index = 7}

- describe ALIAS:
  - before:
      e = consume (10, "---\n" ..
                      "hr:\n" ..
                      "- Mark McGwire\n" ..
                      "- &SS Sammy Sosa\n" ..
                      "rbi:\n" ..
                      "- *SS\n" ..
                      "- Ken Griffey")
  - it recognizes an alias event:
      expect (filter (e (), "type", "anchor")).
        to_equal {type = "ALIAS", anchor = "SS"}
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 5, column = 2, index = 47}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 5, column = 5, index = 50}

- describe SCALAR:
  - before:
      e = consume (6, "---\n" ..
                      "hr:\n" ..
                      "- Mark McGwire\n" ..
                      "- &SS Sammy Sosa\n" ..
                      "rbi:\n" ..
                      "- *SS\n" ..
                      "- Ken Griffey")
  - it recognizes a scalar event:
      expect (filter (e (), "type", "value")).
        to_equal {type = "SCALAR",  value = "Sammy Sosa"}
  - it records anchors:
      expect (e ().anchor).to_be "SS"
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 3, column = 2, index = 25}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 3, column = 16, index = 39}

  - context with quoting style:
    - context plain style:
      - before:
          e = consume (2, "---\n" ..
                          "  Mark McGwire's\n" ..
                          "  year was crippled\n" ..
                          "  by a knee injury.\n")
      - it ignores line-breaks and indentation:
          expect (e ().value).
            to_be "Mark McGwire's year was crippled by a knee injury."
      - it recognizes implicit plain style:
          e = consume (2, "---\n" ..
                          "  Mark McGwire's\n" ..
                          "  year was crippled\n" ..
                          "  by a knee injury.\n")
          expect (e ().plain_implicit).to_be (true)
      - it recognizes explicit plain style:
          e = consume (2, "|\n" ..
                          "  Mark McGwire's\n" ..
                          "  year was crippled\n" ..
                          "  by a knee injury.\n")
          expect (e ().plain_implicit).to_be (false)
      - it recognizes implicit quoted style:
          e = consume (2, "|\n" ..
                          "  Mark McGwire's\n" ..
                          "  year was crippled\n" ..
                          "  by a knee injury.\n")
          expect (e ().quoted_implicit).to_be (true)
      - it recognizes explicit quoted style:
          e = consume (2, "'\n" ..
                          "  Mark McGwire's\n" ..
                          "  year was crippled\n" ..
                          "  by a knee injury.'\n")
          expect (e ().plain_implicit).to_be (false)
    - context folded style:
      - it preserves blank lines and deeper indentation:
          e = consume (2, ">\n" ..
                          "  Sammy Sosa completed another\n" ..
                          "  fine season with great stats.\n" ..
                          "\n" ..
                          "    63 Home Runs\n" ..
                          "    0.288 Batting Average\n" ..
                          "\n" ..
                          "  What a year!\n")
          expect (e ().value).
            to_be ("Sammy Sosa completed another fine season with great stats.\n" ..
                       "\n" ..
                       "  63 Home Runs\n" ..
                       "  0.288 Batting Average\n" ..
                       "\n" ..
                       "What a year!\n")
    - context literal style:
      - it removes indentation but preserves all line-breaks:
          e = consume (2, [[# ASCII Art]] .. "\n" ..
                          [[--- |]] .. "\n" ..
                          [[  \//||\/||]] .. "\n" ..
                          [[  // ||  ||__]] .. "\n")
          expect (e ().value).
            to_be ([[\//||\/||]] .. "\n" ..
                       [[// ||  ||__]] .. "\n")

    - context single quoted style:
      - it folds line breaks:
          e = consume (2, [['This quoted scalar]] .. "\n" ..
                          [[  spans two lines.']])
          expect (e ().value).
            to_be "This quoted scalar spans two lines."
      - it does not process escape sequences:
          # Lua [[ quoting makes sure libyaml sees all the quotes.
          e = consume (2, [['"Howdy!"\t\u263A']])
          expect (e ().value).to_be [["Howdy!"\t\u263A]]

    # Note that we have to single quote the Lua snippets to prevent
    # libyaml from interpreting the bytes as the spec file is read, so
    # that the raw strings get correctly passed to the Lua compiler.
    - context double quoted style:
      - it folds line breaks: '
          e = consume (4, [[quoted: "This quoted scalar]] .. "\n" ..
                          [[  spans two lines\n"]])
          expect (e ().value).
            to_be "This quoted scalar spans two lines\n"'
      - it recognizes unicode escape sequences: '
          e = consume (4, [[unicode: "Sosa did fine.\u263A"]])
          expect (e ().value).to_be "Sosa did fine.\226\152\186"'
      - it recognizes control escape sequences: '
          e = consume (4, [[control: "\b1998\t1999\t2000\n"]])
          expect (e ().value).to_be "\b1998\t1999\t2000\n"'
      - it recognizes hexadecimal escape sequences: '
          e = consume (4, [[hexesc: "\x41\x42\x43 is ABC"]])
          expect (e ().value).to_be "ABC is ABC"'

    - context indentation determines scope: '
        e = consume (4, "name: Mark McGwire\n" ..
                        "accomplishment: >\n" ..
                        "  Mark set a major league\n" ..
                        "  home run record in 1998.\n" ..
                        "stats: |\n" ..
                        "  65 Home Runs\n" ..
                        "  0.278 Batting Average\n")
        expect (e ().value).to_be "Mark McGwire"
        expect (e ().value).to_be "accomplishment"
        expect (e ().value).
          to_be "Mark set a major league home run record in 1998.\n"
        expect (e ().value).to_be "stats"
        expect (e ().value).to_be "65 Home Runs\n0.278 Batting Average\n"'

  - context with tag:
    - it recognizes local tags: '
        e = consume (4, "application specific tag: !something |\n" ..
                        " The semantics of the tag\n" ..
                        " above may be different for\n" ..
                        " different documents.")
        expect (e ().tag).to_be "!something"'
    - it recognizes global tags: '
        e = consume (4, "picture: !!binary |\n" ..
                        " R0lGODlhDAAMAIQAAP//9/X\n" ..
                        " 17unp5WZmZgAAAOfn515eXv\n" ..
                        " Pz7Y6OjuDg4J+fn5OTk6enp\n" ..
                        " 56enmleECcgggoBADs=")
        expect (e ().tag).to_be "tag:yaml.org,2002:binary"'
    - it resolves %TAG declarations: '
        e = consume (5, "%TAG ! tag:clarkevans.com,2002:\n" ..
                        "---\n" ..
                        "shape:\n" ..
                        "- !circle\n" ..
                        "  center: &ORIGIN {x: 73, y: 129}\n" ..
                        "  radius: 7")
        expect (e ().tag).to_be "tag:clarkevans.com,2002:circle"'

- describe SEQUENCE_START:
  - before: '
      e = consume (4, "fubar: &FOO\n" ..
                      "  - foo\n" ..
                      "  - bar\n")'
  - it recognizes a sequence start event:
      expect (e ().type).to_be "SEQUENCE_START"
  - it records anchors:
      expect (e ().anchor).to_be "FOO"
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 0, column = 7, index = 7}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 1, column = 2, index = 14}

  - context with tag:
    - it recognizes local tags: '
        e = consume (2, "--- !something\n" ..
                        "- foo\n")
        expect (filter (e (), "type", "tag")).
          to_equal {type = "SEQUENCE_START", tag = "!something"}'
    - it recognizes global tags: '
        e = consume (2, "--- !!omap\n" ..
                        "- Mark McGwire: 65\n" ..
                        "- Sammy Sosa: 63\n" ..
                        "- Ken Griffy: 58\n")
        expect (filter (e (), "type", "tag")).
          to_equal {type = "SEQUENCE_START",
                        tag  = "tag:yaml.org,2002:omap"}'
    - it resolves %TAG declarations: '
        e = consume (2, "%TAG ! tag:clarkevans.com,2002:\n" ..
                        "--- !shape\n" ..
                        "- !circle\n" ..
                        "  center: &ORIGIN {x: 73, y: 129}\n" ..
                        "  radius: 7\n")
        expect (filter (e (), "type", "tag")).
          to_equal {type = "SEQUENCE_START",
                        tag  = "tag:clarkevans.com,2002:shape"}'

  - context with style:
    - it recognizes block style:
        e = consume (2, "- first\n- second")
        expect (filter (e (), "type", "style")).
          to_equal {type = "SEQUENCE_START", style = "BLOCK"}
    - it recognizes flow style:
        e = consume (2, "[first, second]")
        expect (filter (e (), "type", "style")).
          to_equal {type = "SEQUENCE_START", style = "FLOW"}

- describe SEQUENCE_END:
  - before:
      e = consume (5, "- foo\n- bar\n")
  - it recognizes a sequence end event:
      expect (e ().type).to_equal "SEQUENCE_END"
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 2, column = 0, index = 12}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 2, column = 0, index = 12}

- describe MAPPING_START:
  - before: 'e = consume (3, "- &FUBAR\n  foo: bar\n")'
  - it recognizes a mapping start event:
        expect (e ().type).to_be "MAPPING_START"
  - it records anchors:
      expect (e ().anchor).to_be "FUBAR"
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 0, column = 2, index = 2}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 1, column = 2, index = 11}

  - context with tag:
    - it recognizes local tags: '
    e = consume (2, "--- !something\nfoo: bar\n")
        expect (filter (e (), "type", "tag")).
          to_equal {type = "MAPPING_START", tag = "!something"}'
    - it recognizes global tags: '
        e = consume (2, "--- !!set\n" ..
                        "? Mark McGwire\n" ..
                        "? Sammy Sosa\n" ..
                        "? Ken Griffy\n")
        expect (filter (e (), "type", "tag")).
          to_equal {type = "MAPPING_START",
                        tag  = "tag:yaml.org,2002:set"}'
    - it resolves %TAG declarations: '
        e = consume (3, "%TAG ! tag:clarkevans.com,2002:\n" ..
                        "--- !shape\n" ..
                        "- !circle\n" ..
                        "  center: &ORIGIN {x: 73, y: 129}\n" ..
                        "  radius: 7\n")
        expect (filter (e (), "type", "tag")).
          to_equal {type = "MAPPING_START",
                        tag  = "tag:clarkevans.com,2002:circle"}'

  - context with style:
    - it recognizes block style: '
        e = consume (2, "foo: bar\nbaz:\n  quux")
        expect (filter (e (), "type", "style")).
          to_equal {type = "MAPPING_START", style = "BLOCK"}'
    - it recognizes flow style: '
        e = consume (2, "{foo: bar, baz: quux}")
        expect (filter (e (), "type", "style")).
          to_equal {type = "MAPPING_START", style = "FLOW"}'


- describe MAPPING_END:
  - before: 'e = consume (5, "foo: bar\n")'
  - it recognizes the mapping end event:
      expect (e ().type).to_equal "MAPPING_END"
  - it reports event start marker:
      expect (e ().start_mark).to_equal {line = 1, column = 0, index = 9}
  - it reports event end marker:
      expect (e ().end_mark).to_equal {line = 1, column = 0, index = 9}