<?php
require_once dirname(__FILE__).'/../bootstrap/unit.php';
// Tests for 1.2 spec compliance
// http://www.yaml.org/spec/1.2/spec.html
// See blog post on http://www.redotheoffice
// each example is only tested on proper loading, output is not tested
function test($name, $yml)
{
echo "<tr><th rowspan='3'>".substr($name, 7, 6)."</th><th colspan='2'>".substr($name, 13)."</th></tr>\n";
echo " <tr><td colspan='2'><pre>".htmlentities($yml)."</pre></td></tr>\n";
foreach (array('1.2') as $spec) //'1.1',
{
echo " <tr><th>sfYaml<br/>spec<br/>$spec</th>\n ";
sfYaml::setSpecVersion($spec);
try
{
echo "<td style='background: #dfd'><pre>".print_r(sfYaml
::load($yml), true)."</pre></td>";
}
catch (Exception $e)
{
echo "<td style='background: #fdd'>Error, unable to load YML: <strong>".$e->getMessage()."</strong></td>";
}
echo "\n </tr>\n";
}
}
echo "<table border='1' cellpadding='10'>\n";
$name = "Example 2.1. Sequence of Scalars";
$yml = <<<EOF
- Mark McGwire
- Sammy Sosa
- Ken Griffey
EOF;
test($name, $yml);
// ok 1 - Array
// (
// [0] => Mark McGwire
// [1] => Sammy Sosa
// [2] => Ken Griffey
// )
$name = "Example 2.2. Mapping Scalars to Scalars";
$yml = <<<EOF
hr: 65 # Home runs
avg: 0.278 # Batting average
rbi: 147 # Runs Batted In
EOF;
test($name, $yml);
// ok 2 - Array
// (
// [hr] => 65
// [avg] => 0.278
// [rbi] => 147
// )
$name = "Example 2.3. Mapping Scalars to Sequences";
$yml = <<<EOF
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves
EOF;
test($name, $yml);
// ok 3 - Array
// (
// [american] => Array
// (
// [0] => Boston Red Sox
// [1] => Detroit Tigers
// [2] => New York Yankees
// )
//
// [national] => Array
// (
// [0] => New York Mets
// [1] => Chicago Cubs
// [2] => Atlanta Braves
// )
//
// )
$name = "Example 2.4. Sequence of Mappings";
$yml = <<<EOF
-
name: Mark McGwire
hr: 65
avg: 0.278
-
name: Sammy Sosa
hr: 63
avg: 0.288
EOF;
test($name, $yml);
// ok 4 - Array
// (
// [0] => Array
// (
// [name] => Mark McGwire
// [hr] => 65
// [avg] => 0.278
// )
//
// [1] => Array
// (
// [name] => Sammy Sosa
// [hr] => 63
// [avg] => 0.288
// )
//
// )
$name = "Example 2.5. Sequence of Sequences";
$yml = <<<EOF
- [name , hr, avg ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa , 63, 0.288]
EOF;
test($name, $yml);
// ok 5 - Array
// (
// [0] => Array
// (
// [0] => name
// [1] => hr
// [2] => avg
// )
//
// [1] => Array
// (
// [0] => Mark McGwire
// [1] => 65
// [2] => 0.278
// )
//
// [2] => Array
// (
// [0] => Sammy Sosa
// [1] => 63
// [2] => 0.288
// )
//
// )
$name = "Example 2.6. Mapping of Mappings";
$yml = <<<EOF
Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
hr: 63,
avg: 0.288
}
EOF;
test($name, $yml);
// not ok 6 - Error, unable to load YML:
// Unable to parse string:
// Malformed inline YAML string {
$name = "Example 2.7. Two Documents in a Stream";
$yml = <<<EOF
# Ranking of 1998 home runs
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey
# Team ranking
---
- Chicago Cubs
- St Louis Cardinals
EOF;
test($name, $yml);
// not ok 7 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 2 (---).
$name = "Example 2.8. Play by Play Feed";
$yml = <<<EOF
---
time: 20:03:20
player: Sammy Sosa
action: strike (miss)
...
---
time: 20:03:47
player: Sammy Sosa
action: grand slam
...
EOF;
test($name, $yml);
// not ok 8 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 4 (...).
$name = "Example 2.9. Single Document with Two Comments";
$yml = <<<EOF
---
hr: # 1998 hr ranking
- Mark McGwire
- Sammy Sosa
rbi:
# 1998 rbi ranking
- Sammy Sosa
- Ken Griffey
EOF;
test($name, $yml);
// ok 9 - Array
// (
// [hr] => Array
// (
// [0] => Mark McGwire
// [1] => Sammy Sosa
// )
//
// [rbi] => Array
// (
// [0] => Sammy Sosa
// [1] => Ken Griffey
// )
//
// )
$name = "Example 2.10. Node for 'Sammy Sosa' appears twice in this document";
$yml = <<<EOF
---
hr:
- Mark McGwire
# Following node labeled SS
- &SS Sammy Sosa
rbi:
- *SS # Subsequent occurrence
- Ken Griffey
EOF;
test($name, $yml);
// ok 10 - Array
// (
// [hr] => Array
// (
// [0] => Mark McGwire
// [1] => Sammy Sosa
// )
//
// [rbi] => Array
// (
// [0] => Sammy Sosa
// [1] => Ken Griffey
// )
//
// )
$name = "Example 2.11. Mapping between Sequences";
$yml = <<<EOF
? - Detroit Tigers
- Chicago cubs
:
- 2001-07-23
? [ New York Yankees,
Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
2001-08-14 ]
EOF;
test($name, $yml);
// not ok 11 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 1 (? - Detroit Tigers).
$name = "Example 2.12. Compact Nested Mapping";
$yml = <<<EOF
---
# Products purchased
- item : Super Hoop
quantity: 1
- item : Basketball
quantity: 4
- item : Big Shoes
quantity: 1
EOF;
test($name, $yml);
// not ok 12 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 3 ( quantity: 1).
$name = "Example 2.13. In literals, newlines are preserved";
$yml = <<<EOF
# ASCII Art
--- |
\//||\/||
// || ||__
EOF;
test($name, $yml);
// not ok 13 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 2 (--- |).
$name = "Example 2.14. In the folded scalars, newlines become spaces";
$yml = <<<EOF
--- >
Mark McGwire's
year was crippled
by a knee injury.
EOF;
test($name, $yml);
// not ok 14 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 1 ( Mark McGwire's).
$name = "Example 2.15. Folded newlines are preserved for 'more indented' and blank lines";
$yml = <<<EOF
>
Sammy Sosa completed another
fine season with great stats.
63 Home Runs
0.288 Batting Average
What a year!
EOF;
test($name, $yml);
// not ok 15 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 1 (>).
$name = "Example 2.16. Indentation determines scope";
$yml = <<<EOF
name: Mark McGwire
accomplishment: >
Mark set a major league
home run record in 1998.
stats: |
65 Home Runs
0.278 Batting Average
EOF;
test($name, $yml);
// ok 16 - Array
// (
// [name] => Mark McGwire
// [accomplishment] => Mark set a major league home run record in 1998.
//
// [stats] => 65 Home Runs
// 0.278 Batting Average
//
// )
$name = "Example 2.17. Quoted Scalars";
$yml = <<<EOF
unicode: "Sosa did fine.\u263A"
control: "\b1998\t1999\t2000\n"
hex esc: "\x0d\x0a is \r\n"
single: '"Howdy!" he cried.'
quoted: ' # Not a ''comment''.'
tie-fighter: '|\-*-/|'
EOF;
test($name, $yml);
// not ok 17 - Error, unable to load YML:
// Unable to parse string:
// Malformed inline YAML string ("\b1998 1999 2000).
$name = "Example 2.18. Multi-line Flow Scalars";
$yml = <<<EOF
plain:
This unquoted scalar
spans many lines.
quoted: "So does this
quoted scalar.\n"
EOF;
test($name, $yml);
// not ok 18 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 2 (This unquoted scalar).
$name = "Example 2.19. Integers";
$yml = <<<EOF
canonical: 12345
decimal: +12345
octal: 0o14
hexadecimal: 0xC
EOF;
test($name, $yml);
// ok 19 - Array
// (
// [canonical] => 12345
// [decimal] => 12345
// [octal] => 0o14
// [hexadecimal] => 12
// )
$name = "Example 2.20. Floating Point";
$yml = <<<EOF
canonical: 1.23015e+3
exponential: 12.3015e+02
fixed: 1230.15
negative infinity: -.inf
not a number: .NaN
EOF;
test($name, $yml);
// ok 20 - Array
// (
// [canonical] => 1230.15
// [exponential] => 1230.15
// [fixed] => 1230.15
// [negative infinity] => -INF
// [not a number] => INF
// )
$name = "Example 2.21. Miscellaneous";
$yml = <<<EOF
null:
booleans: [ true, false ]
string: '012345'
EOF;
test($name, $yml);
// ok 21 - Array
// (
// [] =>
// [booleans] => Array
// (
// [0] => 1
// [1] =>
// )
//
// [string] => 012345
// )
// Editor note: Actually this is not ok. "null" as a key should evaluate to string "null", not value 'null'
$name = "Example 2.22. Timestamps";
$yml = <<<EOF
canonical: 2001-12-15T02:59:43.1Z
iso8601: 2001-12-14t21:59:43.10-05:00
spaced: 2001-12-14 21:59:43.10 -5
date: 2002-12-14
EOF;
test($name, $yml);
// ok 22 - Array
// (
// [canonical] => 1008385183
// [iso8601] => 1008385183
// [spaced] => 1008385183
// [date] => 1039824000
// )
$name = "Example 2.23. Various Explicit Tags";
$yml = <<<EOF
---
not-date: !!str 2002-04-28
picture: !!binary |
R0lGODlhDAAMAIQAAP//9/X
17unp5WZmZgAAAOfn515eXv
Pz7Y6OjuDg4J+fn5OTk6enp
56enmleECcgggoBADs=
application specific tag: !something |
The semantics of the tag
above may be different for
different documents.
EOF;
test($name, $yml);
// not ok 23 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 4 ( R0lGODlhDAAMAIQAAP//9/X).
$name = "Example 2.24. Global Tags";
$yml = <<<EOF
%TAG ! tag:clarkevans.com,2002:
--- !shape
# Use the ! handle for presenting
# tag:clarkevans.com,2002:circle
- !circle
center: &ORIGIN {x: 73, y: 129}
radius: 7
- !line
start: *ORIGIN
finish: { x: 89, y: 102 }
- !label
start: *ORIGIN
color: 0xFFEEBB
text: Pretty vector drawing.
EOF;
test($name, $yml);
// not ok 24 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 2 (--- !shape).
$name = "Example 2.25. Unordered Sets";
$yml = <<<EOF
# Sets are represented as a
# Mapping where each key is
# associated with a null value
--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griff
EOF;
test($name, $yml);
// not ok 25 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 4 (--- !!set).
$name = "Example 2.26. Ordered Mappings";
$yml = <<<EOF
# Ordered maps are represented as
# A sequence of mappings, with
# each mapping having one key
--- !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58
EOF;
test($name, $yml);
// not ok 26 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 4 (--- !!omap).
$name = "Example 2.27. Invoice";
$yml = <<<EOF
--- !<tag:clarkevans.com,2002:invoice>
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments:
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
EOF;
test($name, $yml);
// not ok 27 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 16 ( quantity : 4).
$name = "Example 2.28. Log File";
$yml = <<<EOF
---
Time: 2001-11-23 15:01:42 -5
User: ed
Warning:
This is an error message
for the log file
---
Time: 2001-11-23 15:02:31 -5
User: ed
Warning:
A slightly different error
message.
---
Date: 2001-11-23 15:03:17 -5
User: ed
Fatal:
Unknown variable "bar"
Stack:
- file: TopClass.py
line: 23
code: |
x = MoreObject("345\n")
- file: MoreClass.py
line: 58
code: |-
foo = bar
EOF;
test($name, $yml);
// not ok 28 - Error, unable to load YML:
// Unable to parse string:
// Unable to parse line 4 (This is an error message).
echo "</table>";