2 test: Sequence of scalars
9 array('Mark McGwire', 'Sammy Sosa', 'Ken Griffey')
11 test: Mapping of scalars to scalars
18 array('hr' => 65, 'avg' => 0.278, 'rbi' => 147)
20 test: Mapping of scalars to sequences
33 array( 'Boston Red Sox', 'Detroit Tigers',
36 array( 'New York Mets', 'Chicago Cubs',
40 test: Sequence of mappings
53 array('name' => 'Mark McGwire', 'hr' => 65, 'avg' => 0.278),
54 array('name' => 'Sammy Sosa', 'hr' => 63, 'avg' => 0.288)
74 YAML.pm will be able to emulate this behavior soon. In this regard
75 it may be somewhat more correct than Python's native behaviour which
76 can only use tuples as mapping keys. PyYAML will also need to figure
77 out some clever way to roundtrip structured keys.
81 ('New York Yankees', 'Atlanta Braves'):
82 [yaml.timestamp('2001-07-02'),
83 yaml.timestamp('2001-08-12'),
84 yaml.timestamp('2001-08-14')],
85 ('Detroit Tigers', 'Chicago Cubs'):
86 [yaml.timestamp('2001-07-23')]
91 [ 'New York Yankees', 'Atlanta Braves' ] =>
92 [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), Date.new( 2001, 8, 14 ) ],
93 [ 'Detroit Tigers', 'Chicago Cubs' ] =>
94 [ Date.new( 2001, 7, 23 ) ]
97 struct test_node seq1[] = {
98 { T_STR, 0, "New York Yankees" },
99 { T_STR, 0, "Atlanta Braves" },
102 struct test_node seq2[] = {
103 { T_STR, 0, "2001-07-02" },
104 { T_STR, 0, "2001-08-12" },
105 { T_STR, 0, "2001-08-14" },
108 struct test_node seq3[] = {
109 { T_STR, 0, "Detroit Tigers" },
110 { T_STR, 0, "Chicago Cubs" },
113 struct test_node seq4[] = {
114 { T_STR, 0, "2001-07-23" },
117 struct test_node map[] = {
118 { T_SEQ, 0, 0, seq1 },
119 { T_SEQ, 0, 0, seq2 },
120 { T_SEQ, 0, 0, seq3 },
121 { T_SEQ, 0, 0, seq4 },
124 struct test_node stream[] = {
125 { T_MAP, 0, 0, map },
130 test: Sequence of sequences
133 - [ name , hr , avg ]
134 - [ Mark McGwire , 65 , 0.278 ]
135 - [ Sammy Sosa , 63 , 0.288 ]
138 array( 'name', 'hr', 'avg' ),
139 array( 'Mark McGwire', 65, 0.278 ),
140 array( 'Sammy Sosa', 63, 0.288 )
143 test: Mapping of mappings
147 Mark McGwire: {hr: 65, avg: 0.278}
155 array( 'hr' => 65, 'avg' => 0.278 ),
157 array( 'hr' => 63, 'avg' => 0.288 )
160 test: Two documents in a stream each with a leading comment
164 # Ranking of 1998 home runs
176 y.add( [ 'Mark McGwire', 'Sammy Sosa', 'Ken Griffey' ] )
177 y.add( [ 'Chicago Cubs', 'St Louis Cardinals' ] )
181 test: Play by play feed from a game
188 action: strike (miss)
196 [ 'Mark McGwire', 'Sammy Sosa', 'Ken Griffey' ]
200 test: Single document with two comments
203 hr: # 1998 hr ranking
212 'hr' => array( 'Mark McGwire', 'Sammy Sosa' ),
213 'rbi' => array( 'Sammy Sosa', 'Ken Griffey' )
216 test: Node for Sammy Sosa appears twice in this document
222 # Following node labeled SS
225 - *SS # Subsequent occurrence
230 array('Mark McGwire', 'Sammy Sosa'),
232 array('Sammy Sosa', 'Ken Griffey')
235 test: Mapping between sequences
245 ? [ New York Yankees,
247 : [ 2001-07-02, 2001-08-12,
251 [ 'Detroit Tigers', 'Chicago Cubs' ] => [ Date.new( 2001, 7, 23 ) ],
252 [ 'New York Yankees', 'Atlanta Braves' ] => [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), Date.new( 2001, 8, 14 ) ]
255 struct test_node seq1[] = {
256 { T_STR, 0, "New York Yankees" },
257 { T_STR, 0, "Atlanta Braves" },
260 struct test_node seq2[] = {
261 { T_STR, 0, "2001-07-02" },
262 { T_STR, 0, "2001-08-12" },
263 { T_STR, 0, "2001-08-14" },
266 struct test_node seq3[] = {
267 { T_STR, 0, "Detroit Tigers" },
268 { T_STR, 0, "Chicago Cubs" },
271 struct test_node seq4[] = {
272 { T_STR, 0, "2001-07-23" },
275 struct test_node map[] = {
276 { T_SEQ, 0, 0, seq3 },
277 { T_SEQ, 0, 0, seq4 },
278 { T_SEQ, 0, 0, seq1 },
279 { T_SEQ, 0, 0, seq2 },
282 struct test_node stream[] = {
283 { T_MAP, 0, 0, map },
288 test: Sequence key shortcut
302 'item' => 'Super Hoop',
306 'item' => 'Basketball',
310 'item' => 'Big Shoes',
316 { item => 'Super Hoop', quantity => 1 },
317 { item => 'Basketball', quantity => 4 },
318 { item => 'Big Shoes', quantity => 1 }
323 { 'item' => 'Super Hoop', 'quantity' => 1 },
324 { 'item' => 'Basketball', 'quantity' => 4 },
325 { 'item' => 'Big Shoes', 'quantity' => 1 }
329 { 'item': 'Super Hoop', 'quantity': 1 },
330 { 'item': 'Basketball', 'quantity': 4 },
331 { 'item': 'Big Shoes', 'quantity': 1 }
334 struct test_node map1[] = {
335 { T_STR, 0, "item" },
336 { T_STR, 0, "Super Hoop" },
337 { T_STR, 0, "quantity" },
341 struct test_node map2[] = {
342 { T_STR, 0, "item" },
343 { T_STR, 0, "Basketball" },
344 { T_STR, 0, "quantity" },
348 struct test_node map3[] = {
349 { T_STR, 0, "item" },
350 { T_STR, 0, "Big Shoes" },
351 { T_STR, 0, "quantity" },
355 struct test_node seq[] = {
356 { T_MAP, 0, 0, map1 },
357 { T_MAP, 0, 0, map2 },
358 { T_MAP, 0, 0, map3 },
361 struct test_node stream[] = {
362 { T_SEQ, 0, 0, seq },
368 test: Literal perserves newlines
377 "\\//||\\/||\n// || ||_\n"
379 "\\//||\\/||\n// || ||_\n"
390 struct test_node stream[] = {
391 { T_STR, 0, "\\//||\\/||\n// || ||_\n" },
396 test: Folded treats newlines as a space
405 "Mark McGwire's year was crippled by a knee injury."
407 "Mark McGwire's year was crippled by a knee injury."
409 [ "Mark McGwire's year was crippled by a knee injury." ]
411 struct test_node stream[] = {
412 { T_STR, 0, "Mark McGwire's year was crippled by a knee injury." },
417 test: Newlines preserved for indented and blank lines
422 Sammy Sosa completed another
423 fine season with great stats.
426 0.288 Batting Average
430 "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n"
432 "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n"
437 Sammy Sosa completed another fine season with great stats.
440 0.288 Batting Average
447 struct test_node stream[] = {
448 { T_STR, 0, "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n" },
454 test: Indentation determines scope
459 Mark set a major league
460 home run record in 1998.
463 0.278 Batting Average
466 'name' => 'Mark McGwire',
467 'accomplishment' => "Mark set a major league home run record in 1998.\n",
468 'stats' => "65 Home Runs\n0.278 Batting Average\n"
475 unicode: "Sosa did fine.\u263A"
476 control: "\b1998\t1999\t2000\n"
477 hexesc: "\x0D\x0A is \r\n"
479 single: '"Howdy!" he cried.'
480 quoted: ' # not a ''comment''.'
481 tie-fighter: '|\-*-/|'
484 "tie-fighter" => "|\\-*-/|",
485 "control"=>"\0101998\t1999\t2000\n",
486 "unicode"=>"Sosa did fine." + ["263A".hex ].pack('U*'),
487 "quoted"=>" # not a 'comment'.",
488 "single"=>"\"Howdy!\" he cried.",
489 "hexesc"=>"\r\n is \r\n"
492 test: Multiline flow scalars
500 quoted: "So does this
504 'plain' => 'This unquoted scalar spans many lines.',
505 'quoted' => "So does this quoted scalar.\n"
517 'canonical' => 12345,
518 'decimal' => 12345.0,
523 # FIX: spec shows parens around -inf and NaN
527 canonical: 1.23015e+3
528 exponential: 12.3015e+02
530 negative infinity: -.inf
532 float as whole number: !!float 1
535 'canonical' => 1230.15,
536 'exponential' => 1230.15,
538 'negative infinity' => log(0),
539 'not a number' => -log(0),
540 'float as whole number' => (float) 1
562 canonical: 2001-12-15T02:59:43.1Z
563 iso8601: 2001-12-14t21:59:43.10-05:00
564 spaced: 2001-12-14 21:59:43.10 -05:00
565 date: 2002-12-14 # Time is noon UTC
568 'canonical' => YAML::mktime( 2001, 12, 15, 2, 59, 43, 0.10 ),
569 'iso8601' => YAML::mktime( 2001, 12, 14, 21, 59, 43, 0.10, "-05:00" ),
570 'spaced' => YAML::mktime( 2001, 12, 14, 21, 59, 43, 0.10, "-05:00" ),
571 'date' => Date.new( 2002, 12, 14 )
574 test: legacy Timestamps test
578 canonical: 2001-12-15T02:59:43.00Z
579 iso8601: 2001-02-28t21:59:43.00-05:00
580 spaced: 2001-12-14 21:59:43.00 -05:00
584 'canonical' => Time::utc( 2001, 12, 15, 2, 59, 43, 0 ),
585 'iso8601' => YAML::mktime( 2001, 2, 28, 21, 59, 43, 0, "-05:00" ),
586 'spaced' => YAML::mktime( 2001, 12, 14, 21, 59, 43, 0, "-05:00" ),
587 'date' => Date.new( 2002, 12, 14 )
590 test: Various explicit families
594 not-date: !str 2002-04-28
596 R0lGODlhDAAMAIQAAP//9/X
597 17unp5WZmZgAAAOfn515eXv
598 Pz7Y6OjuDg4J+fn5OTk6enp
601 application specific tag: !!something |
602 The semantics of the tag
603 above may be different for
607 YAML.add_private_type( "something" ) do |type, val|
612 'not-date' => '2002-04-28',
613 'picture' => "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236i^\020' \202\n\001\000;",
614 'application specific tag' => "SOMETHING: The semantics of the tag\nabove may be different for\ndifferent documents.\n"
617 test: Application specific family
621 # Establish a tag prefix
622 --- !clarkevans.com,2002/graph/^shape
623 # Use the prefix: shorthand for
624 # !clarkevans.com,2002/graph/circle
626 center: &ORIGIN {x: 73, 'y': 129}
628 - !^line # !clarkevans.com,2002/graph/line
630 finish: { x: 89, 'y': 102 }
634 value: Pretty vector drawing.
636 YAML.add_domain_type( "clarkevans.com,2002", 'graph/shape' ) { |type, val|
638 val << "Shape Container"
641 raise YAML::Error, "Invalid graph of class #{ val.class }: " + val.inspect
644 one_shape_proc = Proc.new { |type, val|
645 scheme, domain, type = type.split( /:/, 3 )
647 val['TYPE'] = "Shape: #{type}"
650 raise YAML::Error, "Invalid graph of class #{ val.class }: " + val.inspect
653 YAML.add_domain_type( "clarkevans.com,2002", 'graph/circle', &one_shape_proc )
654 YAML.add_domain_type( "clarkevans.com,2002", 'graph/line', &one_shape_proc )
655 YAML.add_domain_type( "clarkevans.com,2002", 'graph/label', &one_shape_proc )
665 "TYPE" => "Shape: graph/circle"
672 "TYPE" => "Shape: graph/line",
679 "TYPE" => "Shape: graph/label",
680 "value" => "Pretty vector drawing.",
691 # test: Unordered set
694 # # sets are represented as a
695 # # mapping where each key is
696 # # associated with the empty string
702 test: Ordered mappings
706 # ordered maps are represented as
707 # a sequence of mappings, with
708 # each mapping having one key
724 --- !clarkevans.com,2002/^invoice
742 description : Basketball
747 description : Super Hoop
752 Late afternoon is best.
753 Backup contact is Nancy
757 'invoice' => 34843, 'date' => gmmktime(0, 0, 0, 1, 23, 2001),
759 array( 'given' => 'Chris', 'family' => 'Dumars', 'address' => array( 'lines' => "458 Walkman Dr.\nSuite #292\n", 'city' => 'Royal Oak', 'state' => 'MI', 'postal' => 48046 ) )
761 array( 'given' => 'Chris', 'family' => 'Dumars', 'address' => array( 'lines' => "458 Walkman Dr.\nSuite #292\n", 'city' => 'Royal Oak', 'state' => 'MI', 'postal' => 48046 ) )
764 array( 'sku' => 'BL394D', 'quantity' => 4, 'description' => 'Basketball', 'price' => 450.00 ),
765 array( 'sku' => 'BL4438H', 'quantity' => 1, 'description' => 'Super Hoop', 'price' => 2392.00 )
767 'tax' => 251.42, 'total' => 4443.52,
768 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.\n"
776 Time: 2001-11-23 15:01:42 -05:00
779 This is an error message
782 Time: 2001-11-23 15:02:31 -05:00
785 A slightly different error
788 Date: 2001-11-23 15:03:17 -05:00
791 Unknown variable "bar"
796 x = MoreObject("345\n")
803 y.add( { 'Time' => YAML::mktime( 2001, 11, 23, 15, 01, 42, 00, "-05:00" ),
804 'User' => 'ed', 'Warning' => "This is an error message for the log file\n" } )
805 y.add( { 'Time' => YAML::mktime( 2001, 11, 23, 15, 02, 31, 00, "-05:00" ),
806 'User' => 'ed', 'Warning' => "A slightly different error message.\n" } )
807 y.add( { 'Date' => YAML::mktime( 2001, 11, 23, 15, 03, 17, 00, "-05:00" ),
808 'User' => 'ed', 'Fatal' => "Unknown variable \"bar\"\n",
810 { 'file' => 'TopClass.py', 'line' => 23, 'code' => "x = MoreObject(\"345\\n\")\n" },
811 { 'file' => 'MoreClass.py', 'line' => 58, 'code' => "foo = bar" } ] } )
815 test: Throwaway comments
817 ### These are four throwaway comment ###
819 ### lines (the second line is empty). ###
820 this: | # Comments may trail lines.
821 contains three lines of text.
822 The third one starts with a
823 # character. This isn't a comment.
825 # These are three throwaway comment
826 # lines (the first line is empty).
829 'this' => "contains three lines of text.\nThe third one starts with a\n# character. This isn't a comment.\n"
832 test: Document with a single value
836 This YAML stream contains a single text value.
837 The next stream is a log file - a sequence of
838 log entries. Adding an entry to the log is a
839 simple matter of appending it at the end.
841 "This YAML stream contains a single text value. The next stream is a log file - a sequence of log entries. Adding an entry to the log is a simple matter of appending it at the end.\n"
843 test: Document stream
847 at: 2001-08-12 09:25:00.00 Z
852 at: 2001-08-12 09:25:10.00 Z
859 'at' => Time::utc( 2001, 8, 12, 9, 25, 00 ),
862 'url' => '/index.html'
865 'at' => Time::utc( 2001, 8, 12, 9, 25, 10 ),
873 test: Top level mapping
875 # This stream is an example of a top-level mapping.
882 'date' => gmmktime(0, 0, 0, 1, 23, 2001),
886 test: Single-line documents
889 # The following is a sequence of three documents.
890 # The first contains an empty mapping, the second
891 # an empty sequence, and the last an empty string.
903 test: Document with pause
906 # A communication channel based on a YAML stream.
908 sent at: 2002-06-06 11:46:25.10 Z
910 # Receiver can process this as soon as the following is sent:
912 # Even if the next message is sent long after:
914 sent at: 2002-06-06 12:05:53.47 Z
920 { 'sent at' => YAML::mktime( 2002, 6, 6, 11, 46, 25, 0.10 ),
921 'payload' => 'Whatever' }
924 { "payload" => "Whatever", "sent at" => YAML::mktime( 2002, 6, 6, 12, 5, 53, 0.47 ) }
929 test: Explicit typing
935 array( 'integer' => 12, 'also int' => 12, 'string' => '12' )
940 # Both examples below make use of the 'x-private:ball'
941 # type family URI, but with different semantics.
952 YAML::PrivateType.new( 'ball',
953 { 'number' => 8, 'color' => 'black' } ) }
955 y.add( { 'bearing' =>
956 YAML::PrivateType.new( 'ball',
957 { 'material' => 'steel' } ) }
962 test: Type family under yaml.org
964 # The URI is 'tag:yaml.org,2002:str'
965 - !str a Unicode string
967 array( 'a Unicode string' )
969 test: Type family under perl.yaml.org
972 # The URI is 'tag:perl.yaml.org,2002:Text::Tabs'
973 - !perl/Text::Tabs {}
975 [ YAML::DomainType.new( 'perl.yaml.org,2002', 'Text::Tabs', {} ) ]
977 test: Type family under clarkevans.com
980 # The URI is 'tag:clarkevans.com,2003-02:timesheet'
981 - !clarkevans.com,2003-02/timesheet {}
983 [ YAML::DomainType.new( 'clarkevans.com,2003-02', 'timesheet', {} ) ]
989 - !domain.tld,2002/type\x30 value
990 - !domain.tld,2002/type0 value
991 different: # As far as the YAML parser is concerned
992 - !domain.tld,2002/type%30 value
993 - !domain.tld,2002/type0 value
995 YAML.add_domain_type( "domain.tld,2002", "type0" ) { |type, val|
998 YAML.add_domain_type( "domain.tld,2002", "type%30" ) { |type, val|
1002 { 'same' => [ 'ONE: value', 'ONE: value' ], 'different' => [ 'TWO: value', 'ONE: value' ] }
1007 # 'tag:domain.tld,2002:invoice' is some type family.
1008 invoice: !domain.tld,2002/^invoice
1009 # 'seq' is shorthand for 'tag:yaml.org,2002:seq'.
1010 # This does not effect '^customer' below
1011 # because it is does not specify a prefix.
1013 # '^customer' is shorthand for the full
1014 # notation 'tag:domain.tld,2002:customer'.
1019 YAML.add_domain_type( "domain.tld,2002", /(invoice|customer)/ ) { |type, val|
1021 scheme, domain, type = type.split( /:/, 3 )
1022 val['type'] = "domain #{type}"
1025 raise YAML::Error, "Not a Hash in domain.tld/invoice: " + val.inspect
1029 { "invoice"=> { "customers"=> [ { "given"=>"Chris", "type"=>"domain customer", "family"=>"Dumars" } ], "type"=>"domain invoice" } }
1032 test: Overriding anchors
1034 anchor : &A001 This scalar has an anchor.
1036 The alias node below is a
1037 repeated use of this value.
1040 array( 'anchor' => 'This scalar has an anchor.',
1041 'override' => "The alias node below is a repeated use of this value.\n",
1042 'alias' => "The alias node below is a repeated use of this value.\n" )
1044 test: Flow and block formatting
1048 flow: [ one, two, three # May span lines,
1049 , four, # indentation is
1050 five ] # mostly ignored.
1052 - First item in top sequence
1054 - Subordinate sequence entry
1056 A folded sequence entry
1057 - Sixth item in top sequence
1059 { 'empty' => [], 'flow' => [ 'one', 'two', 'three', 'four', 'five' ],
1060 'block' => [ 'First item in top sequence', [ 'Subordinate sequence entry' ],
1061 "A folded sequence entry\n", 'Sixth item in top sequence' ] }
1063 test: Complete mapping test
1067 flow: { one: 1, two: 2 }
1073 key: Subordinate mapping
1075 - Subordinate sequence
1077 - Previous mapping is empty.
1078 - A key: value pair in a sequence.
1079 A second: key:value pair.
1080 - The previous entry is equal to the following one.
1082 A key: value pair in a sequence.
1083 A second: key:value pair.
1084 !float 12 : This key is a float.
1087 : This key had to be protected.
1088 "\a" : This key had to be escaped.
1095 ? this also works as a key
1096 : with a value at the next line.
1101 - With a sequence value.
1106 with a: mapping value.
1108 { 'empty' => {}, 'flow' => { 'one' => 1, 'two' => 2 },
1109 'spanning' => { 'one' => 1, 'two' => 2 },
1110 'block' => { 'first' => 'First entry', 'second' =>
1111 { 'key' => 'Subordinate mapping' }, 'third' =>
1112 [ 'Subordinate sequence', {}, 'Previous mapping is empty.',
1113 { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' },
1114 'The previous entry is equal to the following one.',
1115 { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' } ],
1116 12.0 => 'This key is a float.', "?\n" => 'This key had to be protected.',
1117 "\a" => 'This key had to be escaped.',
1118 "This is a multi-line folded key\n" => "Whose value is also multi-line.",
1119 'this also works as a key' => 'with a value at the next line.',
1120 [ 'This key', 'is a sequence' ] => [ 'With a sequence value.' ] } }
1121 # Couldn't recreate map exactly, so we'll do a detailed check to be sure it's entact
1122 obj_y['block'].keys.each { |k|
1124 v = obj_y['block'][k]
1125 if k['This'] == 'key' and k['is a'] == 'mapping' and v['with a'] == 'mapping value.'
1126 obj_r['block'][k] = v
1131 test: Literal explicit indentation
1133 # Explicit indentation must
1134 # be given in all the three
1137 This value starts with four spaces.
1139 leading line break: |2
1141 This value starts with a line break.
1143 leading comment indicator: |2
1144 # first line starts with a
1147 # Explicit indentation may
1148 # also be given when it is
1151 This value is indented 2 spaces.
1154 'leading spaces' => " This value starts with four spaces.\n",
1155 'leading line break' => "\nThis value starts with a line break.\n",
1156 'leading comment indicator' => "# first line starts with a\n# character.\n",
1157 'redundant' => "This value is indented 2 spaces.\n"
1160 test: Chomping and keep modifiers
1163 This has one newline.
1165 same as "clipped" above: "This has one newline.\n"
1168 This has no newline.
1170 same as "stripped" above: "This has no newline."
1173 This has two newlines.
1175 same as "kept" above: "This has two newlines.\n\n"
1178 'clipped' => "This has one newline.\n",
1179 'same as "clipped" above' => "This has one newline.\n",
1180 'stripped' => 'This has no newline.',
1181 'same as "stripped" above' => 'This has no newline.',
1182 'kept' => "This has two newlines.\n\n",
1183 'same as "kept" above' => "This has two newlines.\n\n"
1186 test: Literal combinations
1192 The \ ' " characters may be
1193 freely used. Leading white
1194 space is significant.
1196 Line breaks are significant.
1197 Thus this value contains one
1198 empty line and ends with a
1199 single line break, but does
1202 is equal to: "The \\ ' \" characters may \
1203 be\nfreely used. Leading white\n space \
1204 is significant.\n\nLine breaks are \
1205 significant.\nThus this value contains \
1206 one\nempty line and ends with a\nsingle \
1207 line break, but does\nnot start with one.\n"
1209 # Comments may follow a block
1210 # scalar value. They must be
1213 # Modifiers may be combined in any order.
1214 indented and chomped: |2-
1215 This has no newline.
1217 also written as: |-2
1218 This has no newline.
1220 both are equal to: " This has no newline."
1224 'literal' => "The \\ ' \" characters may be\nfreely used. Leading white\n space " +
1225 "is significant.\n\nLine breaks are significant.\nThus this value contains one\n" +
1226 "empty line and ends with a\nsingle line break, but does\nnot start with one.\n",
1227 'is equal to' => "The \\ ' \" characters may be\nfreely used. Leading white\n space " +
1228 "is significant.\n\nLine breaks are significant.\nThus this value contains one\n" +
1229 "empty line and ends with a\nsingle line break, but does\nnot start with one.\n",
1230 'indented and chomped' => ' This has no newline.',
1231 'also written as' => ' This has no newline.',
1232 'both are equal to' => ' This has no newline.'
1235 test: Folded combinations
1241 Line feeds are converted
1242 to spaces, so this value
1243 contains no line breaks
1244 except for the final one.
1246 multiple paragraphs: >2
1248 An empty line, either
1253 line break. Thus this
1254 value contains three
1274 above is equal to: |
1275 This is a folded paragraph followed by a list:
1278 Followed by another folded paragraph, another list:
1284 And a final folded paragraph.
1286 # Explicit comments may follow
1287 # but must be less indented.
1291 'one paragraph' => 'Line feeds are converted to spaces, so this value'.
1292 " contains no line breaks except for the final one.\n",
1293 'multiple paragraphs' => "\nAn empty line, either at the start or in the value:\n".
1294 "Is interpreted as a line break. Thus this value contains three line breaks.\n",
1295 'indented text' => "This is a folded paragraph followed by a list:\n".
1296 " * first entry\n * second entry\nFollowed by another folded paragraph, ".
1297 "another list:\n\n * first entry\n\n * second entry\n\nAnd a final folded paragraph.\n",
1298 'above is equal to' => "This is a folded paragraph followed by a list:\n".
1299 " * first entry\n * second entry\nFollowed by another folded paragraph, ".
1300 "another list:\n\n * first entry\n\n * second entry\n\nAnd a final folded paragraph.\n"
1307 second: '! : \ etc. can be used freely.'
1308 third: 'a single quote '' must be escaped.'
1309 span: 'this contains
1314 is same as: "this contains six spaces\nand one line break"
1318 'second' => '! : \\ etc. can be used freely.',
1319 'third' => "a single quote ' must be escaped.",
1320 'span' => "this contains six spaces\nand one line break",
1321 'is same as' => "this contains six spaces\nand one line break"
1328 second: "! : etc. can be used freely."
1329 third: "a \" or a \\ must be escaped."
1330 fourth: "this value ends with an LF.\n"
1331 span: "this contains
1334 is equal to: "this contains four spaces"
1338 'second' => '! : etc. can be used freely.',
1339 'third' => 'a " or a \\ must be escaped.',
1340 'fourth' => "this value ends with an LF.\n",
1341 'span' => "this contains four spaces",
1342 'is equal to' => "this contains four spaces"
1345 test: Unquoted strings
1348 first: There is no unquoted empty string.
1350 second: 12 ## This is an integer.
1352 third: !str 12 ## This is a string.
1360 indicators: this has no comments.
1369 note: { one-line keys: but multi-line values }
1373 'first' => 'There is no unquoted empty string.',
1376 'span' => "this contains six spaces\nand one line break",
1377 'indicators' => "this has no comments. #:foo and bar# are both text.",
1378 'flow' => [ 'can span lines', 'like this' ],
1379 'note' => { 'one-line keys' => 'but multi-line values' }
1382 test: Spanning sequences
1385 # The following are equal seqs
1386 # with different identities.
1395 'flow' => [ 'one', 'two' ],
1396 'spanning' => [ 'one', 'two' ],
1397 'block' => [ 'one', 'two' ]
1402 # The following are equal maps
1403 # with different identities.
1404 flow: { one: 1, two: 2 }
1410 'flow' => array( 'one' => 1, 'two' => 2 ),
1411 'block' => array( 'one' => 1, 'two' => 2 )
1414 test: Representations of 12
1418 # The following scalars
1420 # string value '1' '2'.
1428 # Strings containing paths and regexps can be unquoted:
1434 array( 12, '12', '12', '12', '12', '/foo/bar', 'd:/foo/bar', 'foo/bar', '/a.*b/' )
1443 # This sequence has five
1444 # entries, two with values.
1452 four: This mapping has five keys,
1453 only two with values.
1457 'canonical' => null,
1459 'sparse' => array( null, '2nd entry', null, '4th entry', null ]),
1460 'four' => 'This mapping has five keys, only two with values.'
1466 # Explicitly typed dictionary.
1468 - aardvark: African pig-like ant eater. Ugly.
1469 - anteater: South-American ant eater. Two species.
1470 - anaconda: South-American constrictor snake. Scary.
1474 'Bestiary' => YAML::Omap[
1475 'aardvark', 'African pig-like ant eater. Ugly.',
1476 'anteater', 'South-American ant eater. Two species.',
1477 'anaconda', 'South-American constrictor snake. Scary.'
1485 # Explicitly typed pairs.
1487 - meeting: with team.
1488 - meeting: with boss.
1490 - meeting: with client.
1493 'tasks' => YAML::Pairs[
1494 'meeting', 'with team.',
1495 'meeting', 'with boss.',
1497 'meeting', 'with client.'
1505 # Explicitly typed set.
1506 baseball players: !set
1512 'baseball players' => YAML::Set[
1513 'Mark McGwire', nil,
1527 false => 'used as key',
1540 'canonical' => 12345,
1541 'decimal' => 12345.0,
1548 canonical: 1.23015e+3
1549 exponential: 12.3015e+02
1551 negative infinity: -.inf
1555 'canonical' => 1230.15,
1556 'exponential' => 1230.15,
1558 'negative infinity' => log(0),
1559 'not a number' => -log(0)
1565 canonical: 2001-12-15T02:59:43.1Z
1566 valid iso8601: 2001-12-14t21:59:43.10-05:00
1567 space separated: 2001-12-14 21:59:43.10 -05:00
1568 date (noon UTC): 2002-12-14
1571 'canonical' => YAML::mktime( 2001, 12, 15, 2, 59, 43, 0.10 ),
1572 'valid iso8601' => YAML::mktime( 2001, 12, 14, 21, 59, 43, 0.10, "-05:00" ),
1573 'space separated' => YAML::mktime( 2001, 12, 14, 21, 59, 43, 0.10, "-05:00" ),
1574 'date (noon UTC)' => Date.new( 2002, 12, 14 )
1580 canonical: !binary "\
1581 R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\
1582 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\
1583 +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\
1584 AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="
1586 R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
1587 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
1588 +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
1589 AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
1591 The binary value above is a tiny arrow
1592 encoded as a gif image.
1594 arrow_gif = "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236iiiccc\243\243\243\204\204\204\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371!\376\016Made with GIMP\000,\000\000\000\000\f\000\f\000\000\005, \216\2010\236\343@\024\350i\020\304\321\212\010\034\317\200M$z\357\3770\205p\270\2601f\r\e\316\001\303\001\036\020' \202\n\001\000;"
1597 'canonical' => arrow_gif,
1598 'base64' => arrow_gif,
1599 'description' => "The binary value above is a tiny arrow encoded as a gif image.\n"
1607 - &CENTER { x: 1, y: 2 }
1608 - &LEFT { x: 0, y: 2 }
1612 # All the following maps are equal:
1625 - # Merge multiple maps
1626 << : [ *CENTER, *BIG ]
1630 << : [ *BIG, *LEFT, *SMALL ]
1635 center = { 'x' => 1, 'y' => 2 }
1636 left = { 'x' => 0, 'y' => 2 }
1638 small = { 'r' => 1 }
1639 node1 = { 'x' => 1, 'y' => 2, 'r' => 10, 'label' => 'center/big' }
1641 node2.update( { 'r' => 10, 'label' => 'center/big' } )
1643 node3.update( center )
1644 node3.update( { 'label' => 'center/big' } )
1646 node4.update( left )
1648 node4.update( { 'x' => 1, 'label' => 'center/big' } )
1652 center, left, big, small, node1, node2, node3, node4
1670 y = YAML::Stream.new
1671 y.add( { 'link with' => [ 'library1.dll', 'library2.dll' ] } )
1672 obj_h = Hash[ 'version' => 1.2 ]
1673 obj_h.default = 'library1.dll'
1674 obj_h2 = Hash[ 'version' => 2.3 ]
1675 obj_h2.default = 'library2.dll'
1676 y.add( { 'link with' => [ obj_h, obj_h2 ] } )
1683 "!": These three keys
1684 "&": had to be quoted
1685 "=": and are normal strings.
1686 # NOTE: the following node should NOT be serialized this way.
1688 !special '!' : '!type'
1689 !special|canonical '&' : 12
1691 # The proper way to serialize the above node is as follows:
1692 node : !!type &12 value
1694 { '!' => 'These three keys', '&' => 'had to be quoted',
1695 '=' => 'and are normal strings.',
1696 'encoded node' => YAML::PrivateType.new( 'type', 'value' ),
1697 'node' => YAML::PrivateType.new( 'type', 'value' ) }