Path: blob/main/contrib/lyaml/spec/ext_yaml_parser_spec.yaml
178586 views
# LYAML binding for Lua 5.1, 5.2, 5.3 & 5.41# Copyright (C) 2013-2022 Gary V. Vaughan23specify parsing:4- it parses empty streams:5e = yaml.parser ""6expect (e ().type).to_be "STREAM_START"7expect (e ().type).to_be "STREAM_END"8expect (e ()).to_be (nil)9expect (e ()).to_be (nil)10- it ignores comments: '11e = yaml.parser "# A comment\nnon-comment # trailing comment\n"12expect (e ().type).to_be "STREAM_START"13expect (e ().type).to_be "DOCUMENT_START"14expect (e ().value).to_be "non-comment"15expect (e ().type).to_be "DOCUMENT_END"'1617- describe STREAM_START:18- before:19e = yaml.parser "# no BOM"20- it is the first event:21expect (e ().type).to_be "STREAM_START"22- it reports event start marker:23expect (e ().start_mark).to_equal {line = 0, column = 0, index = 0}24- it reports event end marker:25expect (e ().end_mark).to_equal {line = 0, column = 0, index = 0}26- it uses UTF-8 by default:27expect (e ().encoding).to_be "UTF8"28- it recognizes UTF-16 BOM:29e = yaml.parser (BOM .. " BOM")30expect (e ().encoding).to_match "UTF16[BL]E"3132- describe STREAM_END:33- before:34for t in yaml.parser "nothing to see" do ev = t end35- it is the last event:36expect (ev.type).to_be "STREAM_END"37- it reports event start marker:38expect (ev.start_mark).to_equal {line = 1, column = 0, index = 14}39- it reports event end marker:40expect (ev.end_mark).to_equal {line = 1, column = 0, index = 14}4142- describe DOCUMENT_START:43- before:44e = consume (1, "---")45- it recognizes document start marker:46expect (filter (e (), "type", "implicit")).47to_equal {type = "DOCUMENT_START", implicit = false}48- it reports implicit document start:49e = consume (1, "foo")50expect (e ().implicit).to_be (true)51- it reports event start marker:52expect (e ().start_mark).to_equal {line = 0, column = 0, index = 0}53- it reports event end marker:54expect (e ().end_mark).to_equal {line = 0, column = 3, index = 3}5556- context parser directives:57- it can recognize document versions:58e = consume (1, "%YAML 1.1\n---")59expect (e ().version_directive).to_equal {major = 1, minor = 1}60- it can diagnose missing document start:61e = consume (1, "%YAML 1.1\n")62expect (e ()).to_error "expected <document start>"63- it can diagnose multiple versions:64e = consume (1, "%YAML 1.1\n%YAML 1.1\n---")65expect (e ()).to_error "duplicate %YAML directive"66- it can diagnose too-new versions:67e = consume (1, "%YAML 2.0\n---")68expect (e ()).to_error "incompatible YAML document"69- it warns of newer minor versions:70pending (github_issue "1")71e = consume (1, "%YAML 1.9\n---")72expect (e ()).73to_error "attempting parsing of newer minor document version"7475- it can recognize primary tag handles:76e = consume (1, "%TAG ! tag:ben-kiki.org,2000:app/\n---")77expect (e ().tag_directives).78to_equal {{handle = "!", prefix = "tag:ben-kiki.org,2000:app/"}}79- it can recognize secondary tag handles:80e = consume (1, "%TAG !! tag:yaml.org,2002:\n---")81expect (e ().tag_directives).82to_equal {{handle = "!!", prefix = "tag:yaml.org,2002:"}}83- it can recognize named tag handles:84e = consume (1, "%TAG !o! tag:ben-kiki.org,2000:\n---")85expect (e ().tag_directives).86to_equal {{handle = "!o!", prefix = "tag:ben-kiki.org,2000:"}}87- it can concatenate multiple tag handles:88e = consume (1, "%TAG ! !\n" ..89"%TAG !! tag:yaml.org,2002:\n" ..90"%TAG !o! tag:ben-kiki.org,2000:\n" ..91"---")92expect (e ().tag_directives).to_contain.93all_of {{handle = "!", prefix = "!"},94{handle = "!!", prefix = "tag:yaml.org,2002:"},95{handle = "!o!", prefix = "tag:ben-kiki.org,2000:"}}96- it can diagnose missing document start:97e = consume (1, "%TAG ! !\n")98expect (e ()).to_error "expected <document start>"99100- describe DOCUMENT_END:101- before:102e = consume (3, "foo\n...")103- it recognizes the document end marker:104expect (filter (e (), "type", "implicit")).105to_equal {type = "DOCUMENT_END", implicit = false}106- it reports an implicit document end marker:107e = consume (3, "foo\n")108expect (filter (e (), "type", "implicit")).109to_equal {type = "DOCUMENT_END", implicit = true}110- it reports event start marker:111expect (e ().start_mark).to_equal {line = 1, column = 0, index = 4}112- it reports event end marker:113expect (e ().end_mark).to_equal {line = 1, column = 3, index = 7}114115- describe ALIAS:116- before:117e = consume (10, "---\n" ..118"hr:\n" ..119"- Mark McGwire\n" ..120"- &SS Sammy Sosa\n" ..121"rbi:\n" ..122"- *SS\n" ..123"- Ken Griffey")124- it recognizes an alias event:125expect (filter (e (), "type", "anchor")).126to_equal {type = "ALIAS", anchor = "SS"}127- it reports event start marker:128expect (e ().start_mark).to_equal {line = 5, column = 2, index = 47}129- it reports event end marker:130expect (e ().end_mark).to_equal {line = 5, column = 5, index = 50}131132- describe SCALAR:133- before:134e = consume (6, "---\n" ..135"hr:\n" ..136"- Mark McGwire\n" ..137"- &SS Sammy Sosa\n" ..138"rbi:\n" ..139"- *SS\n" ..140"- Ken Griffey")141- it recognizes a scalar event:142expect (filter (e (), "type", "value")).143to_equal {type = "SCALAR", value = "Sammy Sosa"}144- it records anchors:145expect (e ().anchor).to_be "SS"146- it reports event start marker:147expect (e ().start_mark).to_equal {line = 3, column = 2, index = 25}148- it reports event end marker:149expect (e ().end_mark).to_equal {line = 3, column = 16, index = 39}150151- context with quoting style:152- context plain style:153- before:154e = consume (2, "---\n" ..155" Mark McGwire's\n" ..156" year was crippled\n" ..157" by a knee injury.\n")158- it ignores line-breaks and indentation:159expect (e ().value).160to_be "Mark McGwire's year was crippled by a knee injury."161- it recognizes implicit plain style:162e = consume (2, "---\n" ..163" Mark McGwire's\n" ..164" year was crippled\n" ..165" by a knee injury.\n")166expect (e ().plain_implicit).to_be (true)167- it recognizes explicit plain style:168e = consume (2, "|\n" ..169" Mark McGwire's\n" ..170" year was crippled\n" ..171" by a knee injury.\n")172expect (e ().plain_implicit).to_be (false)173- it recognizes implicit quoted style:174e = consume (2, "|\n" ..175" Mark McGwire's\n" ..176" year was crippled\n" ..177" by a knee injury.\n")178expect (e ().quoted_implicit).to_be (true)179- it recognizes explicit quoted style:180e = consume (2, "'\n" ..181" Mark McGwire's\n" ..182" year was crippled\n" ..183" by a knee injury.'\n")184expect (e ().plain_implicit).to_be (false)185- context folded style:186- it preserves blank lines and deeper indentation:187e = consume (2, ">\n" ..188" Sammy Sosa completed another\n" ..189" fine season with great stats.\n" ..190"\n" ..191" 63 Home Runs\n" ..192" 0.288 Batting Average\n" ..193"\n" ..194" What a year!\n")195expect (e ().value).196to_be ("Sammy Sosa completed another fine season with great stats.\n" ..197"\n" ..198" 63 Home Runs\n" ..199" 0.288 Batting Average\n" ..200"\n" ..201"What a year!\n")202- context literal style:203- it removes indentation but preserves all line-breaks:204e = consume (2, [[# ASCII Art]] .. "\n" ..205[[--- |]] .. "\n" ..206[[ \//||\/||]] .. "\n" ..207[[ // || ||__]] .. "\n")208expect (e ().value).209to_be ([[\//||\/||]] .. "\n" ..210[[// || ||__]] .. "\n")211212- context single quoted style:213- it folds line breaks:214e = consume (2, [['This quoted scalar]] .. "\n" ..215[[ spans two lines.']])216expect (e ().value).217to_be "This quoted scalar spans two lines."218- it does not process escape sequences:219# Lua [[ quoting makes sure libyaml sees all the quotes.220e = consume (2, [['"Howdy!"\t\u263A']])221expect (e ().value).to_be [["Howdy!"\t\u263A]]222223# Note that we have to single quote the Lua snippets to prevent224# libyaml from interpreting the bytes as the spec file is read, so225# that the raw strings get correctly passed to the Lua compiler.226- context double quoted style:227- it folds line breaks: '228e = consume (4, [[quoted: "This quoted scalar]] .. "\n" ..229[[ spans two lines\n"]])230expect (e ().value).231to_be "This quoted scalar spans two lines\n"'232- it recognizes unicode escape sequences: '233e = consume (4, [[unicode: "Sosa did fine.\u263A"]])234expect (e ().value).to_be "Sosa did fine.\226\152\186"'235- it recognizes control escape sequences: '236e = consume (4, [[control: "\b1998\t1999\t2000\n"]])237expect (e ().value).to_be "\b1998\t1999\t2000\n"'238- it recognizes hexadecimal escape sequences: '239e = consume (4, [[hexesc: "\x41\x42\x43 is ABC"]])240expect (e ().value).to_be "ABC is ABC"'241242- context indentation determines scope: '243e = consume (4, "name: Mark McGwire\n" ..244"accomplishment: >\n" ..245" Mark set a major league\n" ..246" home run record in 1998.\n" ..247"stats: |\n" ..248" 65 Home Runs\n" ..249" 0.278 Batting Average\n")250expect (e ().value).to_be "Mark McGwire"251expect (e ().value).to_be "accomplishment"252expect (e ().value).253to_be "Mark set a major league home run record in 1998.\n"254expect (e ().value).to_be "stats"255expect (e ().value).to_be "65 Home Runs\n0.278 Batting Average\n"'256257- context with tag:258- it recognizes local tags: '259e = consume (4, "application specific tag: !something |\n" ..260" The semantics of the tag\n" ..261" above may be different for\n" ..262" different documents.")263expect (e ().tag).to_be "!something"'264- it recognizes global tags: '265e = consume (4, "picture: !!binary |\n" ..266" R0lGODlhDAAMAIQAAP//9/X\n" ..267" 17unp5WZmZgAAAOfn515eXv\n" ..268" Pz7Y6OjuDg4J+fn5OTk6enp\n" ..269" 56enmleECcgggoBADs=")270expect (e ().tag).to_be "tag:yaml.org,2002:binary"'271- it resolves %TAG declarations: '272e = consume (5, "%TAG ! tag:clarkevans.com,2002:\n" ..273"---\n" ..274"shape:\n" ..275"- !circle\n" ..276" center: &ORIGIN {x: 73, y: 129}\n" ..277" radius: 7")278expect (e ().tag).to_be "tag:clarkevans.com,2002:circle"'279280- describe SEQUENCE_START:281- before: '282e = consume (4, "fubar: &FOO\n" ..283" - foo\n" ..284" - bar\n")'285- it recognizes a sequence start event:286expect (e ().type).to_be "SEQUENCE_START"287- it records anchors:288expect (e ().anchor).to_be "FOO"289- it reports event start marker:290expect (e ().start_mark).to_equal {line = 0, column = 7, index = 7}291- it reports event end marker:292expect (e ().end_mark).to_equal {line = 1, column = 2, index = 14}293294- context with tag:295- it recognizes local tags: '296e = consume (2, "--- !something\n" ..297"- foo\n")298expect (filter (e (), "type", "tag")).299to_equal {type = "SEQUENCE_START", tag = "!something"}'300- it recognizes global tags: '301e = consume (2, "--- !!omap\n" ..302"- Mark McGwire: 65\n" ..303"- Sammy Sosa: 63\n" ..304"- Ken Griffy: 58\n")305expect (filter (e (), "type", "tag")).306to_equal {type = "SEQUENCE_START",307tag = "tag:yaml.org,2002:omap"}'308- it resolves %TAG declarations: '309e = consume (2, "%TAG ! tag:clarkevans.com,2002:\n" ..310"--- !shape\n" ..311"- !circle\n" ..312" center: &ORIGIN {x: 73, y: 129}\n" ..313" radius: 7\n")314expect (filter (e (), "type", "tag")).315to_equal {type = "SEQUENCE_START",316tag = "tag:clarkevans.com,2002:shape"}'317318- context with style:319- it recognizes block style:320e = consume (2, "- first\n- second")321expect (filter (e (), "type", "style")).322to_equal {type = "SEQUENCE_START", style = "BLOCK"}323- it recognizes flow style:324e = consume (2, "[first, second]")325expect (filter (e (), "type", "style")).326to_equal {type = "SEQUENCE_START", style = "FLOW"}327328- describe SEQUENCE_END:329- before:330e = consume (5, "- foo\n- bar\n")331- it recognizes a sequence end event:332expect (e ().type).to_equal "SEQUENCE_END"333- it reports event start marker:334expect (e ().start_mark).to_equal {line = 2, column = 0, index = 12}335- it reports event end marker:336expect (e ().end_mark).to_equal {line = 2, column = 0, index = 12}337338- describe MAPPING_START:339- before: 'e = consume (3, "- &FUBAR\n foo: bar\n")'340- it recognizes a mapping start event:341expect (e ().type).to_be "MAPPING_START"342- it records anchors:343expect (e ().anchor).to_be "FUBAR"344- it reports event start marker:345expect (e ().start_mark).to_equal {line = 0, column = 2, index = 2}346- it reports event end marker:347expect (e ().end_mark).to_equal {line = 1, column = 2, index = 11}348349- context with tag:350- it recognizes local tags: '351e = consume (2, "--- !something\nfoo: bar\n")352expect (filter (e (), "type", "tag")).353to_equal {type = "MAPPING_START", tag = "!something"}'354- it recognizes global tags: '355e = consume (2, "--- !!set\n" ..356"? Mark McGwire\n" ..357"? Sammy Sosa\n" ..358"? Ken Griffy\n")359expect (filter (e (), "type", "tag")).360to_equal {type = "MAPPING_START",361tag = "tag:yaml.org,2002:set"}'362- it resolves %TAG declarations: '363e = consume (3, "%TAG ! tag:clarkevans.com,2002:\n" ..364"--- !shape\n" ..365"- !circle\n" ..366" center: &ORIGIN {x: 73, y: 129}\n" ..367" radius: 7\n")368expect (filter (e (), "type", "tag")).369to_equal {type = "MAPPING_START",370tag = "tag:clarkevans.com,2002:circle"}'371372- context with style:373- it recognizes block style: '374e = consume (2, "foo: bar\nbaz:\n quux")375expect (filter (e (), "type", "style")).376to_equal {type = "MAPPING_START", style = "BLOCK"}'377- it recognizes flow style: '378e = consume (2, "{foo: bar, baz: quux}")379expect (filter (e (), "type", "style")).380to_equal {type = "MAPPING_START", style = "FLOW"}'381382383- describe MAPPING_END:384- before: 'e = consume (5, "foo: bar\n")'385- it recognizes the mapping end event:386expect (e ().type).to_equal "MAPPING_END"387- it reports event start marker:388expect (e ().start_mark).to_equal {line = 1, column = 0, index = 9}389- it reports event end marker:390expect (e ().end_mark).to_equal {line = 1, column = 0, index = 9}391392393