Path: blob/main/contrib/lyaml/spec/ext_yaml_scanner_spec.yaml
178469 views
# LYAML binding for Lua 5.1, 5.2, 5.3 & 5.41# Copyright (C) 2013-2022 Gary V. Vaughan23before:4function consume (n, str)5local k = yaml.scanner (str)6for n = 1, n do k () end7return k8end910specify scanning:11- it scans empty streams:12k = yaml.scanner ""13expect (k ().type).to_be "STREAM_START"14expect (k ().type).to_be "STREAM_END"15expect (k ()).to_be (nil)16expect (k ()).to_be (nil)17- it ignores comments: '18k = yaml.scanner "# A comment\nnon-comment # trailing comment\n"19expect (k ().type).to_be "STREAM_START"20expect (k ().value).to_be "non-comment"21expect (k ().type).to_be "STREAM_END"'2223- describe STREAM_START:24- before:25k = yaml.scanner "# no BOM"26- it is the first token:27expect (k ().type).to_be "STREAM_START"28- it reports token start marker:29expect (k ().start_mark).to_equal {line = 0, column = 0, index = 0}30- it reports token end marker:31expect (k ().end_mark).to_equal {line = 0, column = 0, index = 0}32- it uses UTF-8 by default:33expect (k ().encoding).to_be "UTF8"34- it recognizes UTF-16 BOM:35k = yaml.scanner (BOM .. " BOM")36expect (k ().encoding).to_match "UTF16[BL]E"3738- describe STREAM_END:39- before:40for t in yaml.scanner "nothing to see" do k = t end41- it is the last token:42expect (k.type).to_be "STREAM_END"43- it reports token start marker:44expect (k.start_mark).to_equal {line = 1, column = 0, index = 14}45- it reports token end marker:46expect (k.end_mark).to_equal {line = 1, column = 0, index = 14}4748- describe VERSION_DIRECTIVE:49- before:50k = consume (1, "%YAML 1.0")51- it can recognize document versions:52expect (filter (k (), "type", "major", "minor")).53to_equal {type = "VERSION_DIRECTIVE", major = 1, minor = 0}54- it reports token start marker:55expect (k ().start_mark).to_equal {line = 0, column = 0, index = 0}56- it reports token end marker:57expect (k ().end_mark).to_equal {line = 0, column = 9, index = 9}5859- describe TAG_DIRECTIVE:60- it can recognize primary tag handles:61k = consume (1, "%TAG ! tag:ben-kiki.org,2000:app/")62expect (filter (k (), "handle", "prefix")).63to_equal {handle = "!", prefix = "tag:ben-kiki.org,2000:app/"}64- it can recognize secondary tag handles:65k = consume (1, "%TAG !! tag:yaml.org,2002:")66expect (filter (k (), "handle", "prefix")).67to_equal {handle = "!!", prefix = "tag:yaml.org,2002:"}68- it can recognize named tag handles:69k = consume (1, "%TAG !o! tag:ben-kiki.org,2000:\n---")70expect (filter (k (), "handle", "prefix")).71to_equal {handle = "!o!", prefix = "tag:ben-kiki.org,2000:"}7273- describe DOCUMENT_START:74- before:75k = consume (1, "---")76- it recognizes document start marker:77expect (k ().type).to_be "DOCUMENT_START"78- it reports token start marker:79expect (k ().start_mark).to_equal {line = 0, column = 0, index = 0}80- it reports token end marker:81expect (k ().end_mark).to_equal {line = 0, column = 3, index = 3}8283- describe DOCUMENT_END:84- before:85k = consume (2, "foo\n...")86- it recognizes the document end marker:87expect (k ().type).to_be "DOCUMENT_END"88- it reports token start marker:89expect (k ().start_mark).to_equal {line = 1, column = 0, index = 4}90- it reports token end marker:91expect (k ().end_mark).to_equal {line = 1, column = 3, index = 7}9293- describe ALIAS:94- before:95k = consume (15, "---\n" ..96"hr:\n" ..97"- Mark McGwire\n" ..98"- &SS Sammy Sosa\n" ..99"rbi:\n" ..100"- *SS\n" ..101"- Ken Griffey")102- it recognizes an alias token:103expect (filter (k (), "type", "value")).104to_equal {type = "ALIAS", value = "SS"}105- it reports token start marker:106expect (k ().start_mark).to_equal {line = 5, column = 2, index = 47}107- it reports token end marker:108expect (k ().end_mark).to_equal {line = 5, column = 5, index = 50}109110- describe ANCHOR:111- before:112k = consume (9, "---\n" ..113"hr:\n" ..114"- Mark McGwire\n" ..115"- &SS Sammy Sosa\n" ..116"rbi:\n" ..117"- *SS\n" ..118"- Ken Griffey")119- it recognizes an anchor token:120expect (filter (k (), "type", "value")).121to_equal {type = "ANCHOR", value = "SS"}122- it reports token start marker:123expect (k ().start_mark).to_equal {line = 3, column = 2, index = 25}124- it reports token end marker:125expect (k ().end_mark).to_equal {line = 3, column = 5, index = 28}126127- describe SCALAR:128- before:129k = consume (10, "---\n" ..130"hr:\n" ..131"- Mark McGwire\n" ..132"- &SS Sammy Sosa\n" ..133"rbi:\n" ..134"- *SS\n" ..135"- Ken Griffey")136- it recognizes a scalar token:137expect (filter (k (), "type", "value")).138to_equal {type = "SCALAR", value = "Sammy Sosa"}139- it reports token start marker:140expect (k ().start_mark).to_equal {line = 3, column = 6, index = 29}141- it reports token end marker:142expect (k ().end_mark).to_equal {line = 3, column = 16, index = 39}143144- context with quoting style:145- context plain style:146- before:147k = consume (2, "---\n" ..148" Mark McGwire's\n" ..149" year was crippled\n" ..150" by a knee injury.\n")151- it ignores line-breaks and indentation:152expect (k ().value).153to_be "Mark McGwire's year was crippled by a knee injury."154- it recognizes PLAIN style:155expect (k ().style).to_be "PLAIN"156- context folded style:157- before:158k = consume (1, ">\n" ..159" Sammy Sosa completed another\n" ..160" fine season with great stats.\n" ..161"\n" ..162" 63 Home Runs\n" ..163" 0.288 Batting Average\n" ..164"\n" ..165" What a year!\n")166- it preserves blank lines and deeper indentation:167expect (k ().value).168to_be ("Sammy Sosa completed another fine season with great stats.\n" ..169"\n" ..170" 63 Home Runs\n" ..171" 0.288 Batting Average\n" ..172"\n" ..173"What a year!\n")174- it recognizes FOLDED style:175expect (k ().style).to_be "FOLDED"176- context literal style:177- before:178k = consume (2, [[# ASCII Art]] .. "\n" ..179[[--- |]] .. "\n" ..180[[ \//||\/||]] .. "\n" ..181[[ // || ||__]] .. "\n")182- it removes indentation but preserves all line-breaks:183expect (k ().value).184to_be ([[\//||\/||]] .. "\n" ..185[[// || ||__]] .. "\n")186- it recognizes LITERAL style:187expect (k ().style).to_be "LITERAL"188189- context single quoted style:190- before:191k = consume (1, [['This quoted scalar]] .. "\n" ..192[[ spans two lines.']])193- it folds line breaks:194expect (k ().value).195to_be "This quoted scalar spans two lines."196- it does not process escape sequences:197# Lua [[ quoting makes sure libyaml sees all the quotes.198k = consume (1, [['"Howdy!"\t\u263A']])199expect (k ().value).to_be [["Howdy!"\t\u263A]]200- it recognizes LITERAL style:201expect (k ().style).to_be "SINGLE_QUOTED"202203# Note that we have to single quote the Lua snippets to prevent204# libyaml from interpreting the bytes as the spec file is read, so205# that the raw strings get correctly passed to the Lua compiler.206- context double quoted style:207- it folds line breaks: '208k = consume (5, [[quoted: "This quoted scalar]] .. "\n" ..209[[ spans two lines\n"]])210expect (k ().value).211to_be "This quoted scalar spans two lines\n"'212- it recognizes unicode escape sequences: '213k = consume (5, [[unicode: "Sosa did fine.\u263A"]])214expect (k ().value).to_be "Sosa did fine.\226\152\186"'215- it recognizes control escape sequences: '216k = consume (5, [[control: "\b1998\t1999\t2000\n"]])217expect (k ().value).to_be "\b1998\t1999\t2000\n"'218- it recognizes hexadecimal escape sequences: '219k = consume (5, [[hexesc: "\x41\x42\x43 is ABC"]])220expect (k ().value).to_be "ABC is ABC"'221222- context indentation determines scope: '223k = consume (5, "name: Mark McGwire\n" ..224"accomplishment: >\n" ..225" Mark set a major league\n" ..226" home run record in 1998.\n" ..227"stats: |\n" ..228" 65 Home Runs\n" ..229" 0.278 Batting Average\n")230expect (k ().value).to_be "Mark McGwire"231expect (k ().type).to_be "KEY"232expect (k ().value).to_be "accomplishment"233expect (k ().type).to_be "VALUE"234expect (k ().value).235to_be "Mark set a major league home run record in 1998.\n"236expect (k ().type).to_be "KEY"237expect (k ().value).to_be "stats"238expect (k ().type).to_be "VALUE"239expect (k ().value).to_be "65 Home Runs\n0.278 Batting Average\n"'240241- describe TAG:242- it recognizes local tags: '243k = consume (5, "application specific tag: !something |\n" ..244" The semantics of the tag\n" ..245" above may be different for\n" ..246" different documents.")247expect (filter (k (), "type", "handle", "suffix")).248to_equal {type = "TAG", handle = "!", suffix = "something"}'249- it recognizes global tags: '250k = consume (5, "picture: !!binary |\n" ..251" R0lGODlhDAAMAIQAAP//9/X\n" ..252" 17unp5WZmZgAAAOfn515eXv\n" ..253" Pz7Y6OjuDg4J+fn5OTk6enp\n" ..254" 56enmleECcgggoBADs=")255expect (filter (k (), "type", "handle", "suffix")).256to_equal {type = "TAG", handle = "!!", suffix = "binary"}'257258- describe BLOCK_SEQUENCE_START:259- before: '260k = consume (5, "fubar:\n" ..261" - foo\n" ..262" - bar\n")'263- it recognizes a sequence start token:264expect (k ().type).to_be "BLOCK_SEQUENCE_START"265- it reports token start marker:266expect (k ().start_mark).to_equal {line = 1, column = 2, index = 9}267- it reports token end marker:268expect (k ().end_mark).to_equal {line = 1, column = 2, index = 9}269270- describe BLOCK_MAPPING_START:271- before: 'k = consume (3, "-\n foo: bar\n-")'272- it recognizes a mapping start token:273expect (k ().type).to_be "BLOCK_MAPPING_START"274- it reports token start marker:275expect (k ().start_mark).to_equal {line = 1, column = 2, index = 4}276- it reports token end marker:277expect (k ().end_mark).to_equal {line = 1, column = 2, index = 4}278279- describe BLOCK_ENTRY:280- before: 'k = consume (2, "-\n foo: bar\n-")'281- it recognizes a sequence block entry token: '282k = consume (8, "fubar:\n" ..283" - foo\n" ..284" - bar\n")285expect (k ().type).to_be "BLOCK_ENTRY"'286- it recognizes a mapping block entry token:287expect (k ().type).to_be "BLOCK_ENTRY"288- it reports token start marker:289expect (k ().start_mark).to_equal {line = 0, column = 0, index = 0}290- it reports token end marker:291expect (k ().end_mark).to_equal {line = 0, column = 1, index = 1}292293- describe BLOCK_END:294- before: 'k = consume (8, "-\n foo: bar\n-")'295- it recognizes a sequence block end token: '296k = consume (10, "fubar:\n" ..297" - foo\n" ..298" - bar\n")299expect (k ().type).to_be "BLOCK_END"'300- it recognizes a mapping block end token:301expect (k ().type).to_be "BLOCK_END"302- it reports token start marker:303expect (k ().start_mark).to_equal {line = 2, column = 0, index = 13}304- it reports token end marker:305expect (k ().end_mark).to_equal {line = 2, column = 0, index = 13}306307- describe FLOW_SEQUENCE_START:308- before: '309k = consume (5, "fubar: [foo, bar]\n")'310- it recognizes a sequence start token:311expect (k ().type).to_be "FLOW_SEQUENCE_START"312- it reports token start marker:313expect (k ().start_mark).to_equal {line = 0, column = 7, index = 7}314- it reports token end marker:315expect (k ().end_mark).to_equal {line = 0, column = 8, index = 8}316317- describe FLOW_SEQUENCE_END:318- before: '319k = consume (9, "fubar: [foo, bar]\n")'320- it recognizes a sequence end token:321expect (k ().type).to_equal "FLOW_SEQUENCE_END"322- it reports token start marker:323expect (k ().start_mark).to_equal {line = 0, column = 16, index = 16}324- it reports token end marker:325expect (k ().end_mark).to_equal {line = 0, column = 17, index = 17}326327- describe FLOW_ENTRY:328- before: 'k = consume (6, "{foo: bar, baz: quux}")'329- it recognizes a sequence flow entry: '330k = consume (6, "[foo: bar, baz: quux]")331expect (k ().type).to_be "FLOW_ENTRY"'332- it recognizes a mapping flow entry:333expect (k ().type).to_be "FLOW_ENTRY"334- it reports token start marker:335expect (k ().start_mark).to_equal {line = 0, column = 9, index = 9}336- it reports token end marker:337expect (k ().end_mark).to_equal {line = 0, column = 10, index = 10}338339- describe FLOW_MAPPING_START:340- before: 'k = consume (1, "{foo: bar, baz: quux}")'341- it recognizes flow style:342expect (k ().type).to_be "FLOW_MAPPING_START"343- it reports token start marker:344expect (k ().start_mark).to_equal {line = 0, column = 0, index = 0}345- it reports token end marker:346expect (k ().end_mark).to_equal {line = 0, column = 1, index = 1}347348- describe FLOW_MAPPING_END:349- before: 'k = consume (6, "{foo: bar}\n")'350- it recognizes the mapping end token:351expect (k ().type).to_equal "FLOW_MAPPING_END"352- it reports token start marker:353expect (k ().start_mark).to_equal {line = 0, column = 9, index = 9}354- it reports token end marker:355expect (k ().end_mark).to_equal {line = 0, column = 10, index = 10}356357- describe KEY:358- before: 'k = consume (2, "{the key: the value, another key: meh}")'359- it recognizes a flow mapping key token:360expect (k ().type).to_be "KEY"361- it recognizes a block mapping key token: '362k = consume (2, "the key: the value\nanother key: meh\n")363expect (k ().type).to_be "KEY"'364- it reports token start marker:365expect (k ().start_mark).to_equal {line = 0, column = 1, index = 1}366- it reports token end marker:367expect (k ().end_mark).to_equal {line = 0, column = 1, index = 1}368369- describe VALUE:370- before: 'k = consume (4, "{the key: the value, another key: meh}")'371- it recognizes a flow mapping value token:372expect (k ().type).to_be "VALUE"373- it recognizes a block mapping key value: '374k = consume (4, "the key: the value\nanother key: meh\n")375expect (k ().type).to_be "VALUE"'376- it reports token start marker:377expect (k ().start_mark).to_equal {line = 0, column = 8, index = 8}378- it reports token end marker:379expect (k ().end_mark).to_equal {line = 0, column = 9, index = 9}380381382