Share Pastebin
Guest
Public paste!

Sjoerd de Jong

By: a guest | Feb 8th, 2010 | Syntax: PHP | Size: 12.43 KB | Hits: 199 | Expires: Never
Copy text to clipboard
  1. <?php
  2. require_once dirname(__FILE__).'/../bootstrap/unit.php';
  3.  
  4. // Tests for 1.2 spec compliance
  5. // http://www.yaml.org/spec/1.2/spec.html
  6.  
  7. // See blog post on http://www.redotheoffice
  8.  
  9. // each example is only tested on proper loading, output is not tested
  10. function test($name, $yml)
  11. {
  12.   echo "<tr><th rowspan='3'>".substr($name, 7, 6)."</th><th colspan='2'>".substr($name, 13)."</th></tr>\n";
  13.   echo "  <tr><td colspan='2'><pre>".htmlentities($yml)."</pre></td></tr>\n";
  14.  
  15.   foreach (array('1.2') as $spec) //'1.1',
  16.   {
  17.     echo "  <tr><th>sfYaml<br/>spec<br/>$spec</th>\n  ";
  18.     sfYaml::setSpecVersion($spec);
  19.     try
  20.     {
  21.       echo "<td style='background: #dfd'><pre>".print_r(sfYaml::load($yml), true)."</pre></td>";
  22.     }
  23.     catch (Exception $e)
  24.     {
  25.       echo "<td style='background: #fdd'>Error, unable to load YML: <strong>".$e->getMessage()."</strong></td>";
  26.     }
  27.     echo "\n  </tr>\n";
  28.   }
  29. }
  30.  
  31. echo "<table border='1' cellpadding='10'>\n";
  32.  
  33. $name = "Example 2.1.  Sequence of Scalars";
  34. $yml = <<<EOF
  35. - Mark McGwire
  36. - Sammy Sosa
  37. - Ken Griffey
  38. EOF;
  39. test($name, $yml);
  40. // ok 1 - Array
  41. // (
  42. //     [0] => Mark McGwire
  43. //     [1] => Sammy Sosa
  44. //     [2] => Ken Griffey
  45. // )
  46.  
  47. $name = "Example 2.2.  Mapping Scalars to Scalars";
  48. $yml = <<<EOF
  49. hr:  65    # Home runs
  50. avg: 0.278 # Batting average
  51. rbi: 147   # Runs Batted In
  52. EOF;
  53. test($name, $yml);
  54. // ok 2 - Array
  55. // (
  56. //     [hr] => 65
  57. //     [avg] => 0.278
  58. //     [rbi] => 147
  59. // )
  60.  
  61. $name = "Example 2.3.  Mapping Scalars to Sequences";
  62. $yml = <<<EOF
  63. american:
  64.   - Boston Red Sox
  65.   - Detroit Tigers
  66.   - New York Yankees
  67. national:
  68.   - New York Mets
  69.   - Chicago Cubs
  70.   - Atlanta Braves
  71. EOF;
  72. test($name, $yml);
  73. // ok 3 - Array
  74. // (
  75. //     [american] => Array
  76. //         (
  77. //             [0] => Boston Red Sox
  78. //             [1] => Detroit Tigers
  79. //             [2] => New York Yankees
  80. //         )
  81. //
  82. //     [national] => Array
  83. //         (
  84. //             [0] => New York Mets
  85. //             [1] => Chicago Cubs
  86. //             [2] => Atlanta Braves
  87. //         )
  88. //
  89. // )
  90.  
  91. $name = "Example 2.4.  Sequence of Mappings";
  92. $yml = <<<EOF
  93. -
  94.   name: Mark McGwire
  95.   hr:   65
  96.   avg:  0.278
  97. -
  98.   name: Sammy Sosa
  99.   hr:   63
  100.   avg:  0.288
  101.  
  102. EOF;
  103. test($name, $yml);
  104. // ok 4 - Array
  105. // (
  106. //     [0] => Array
  107. //         (
  108. //             [name] => Mark McGwire
  109. //             [hr] => 65
  110. //             [avg] => 0.278
  111. //         )
  112. //
  113. //     [1] => Array
  114. //         (
  115. //             [name] => Sammy Sosa
  116. //             [hr] => 63
  117. //             [avg] => 0.288
  118. //         )
  119. //
  120. // )
  121.  
  122. $name = "Example 2.5. Sequence of Sequences";
  123. $yml = <<<EOF
  124. - [name        , hr, avg  ]
  125. - [Mark McGwire, 65, 0.278]
  126. - [Sammy Sosa  , 63, 0.288]
  127. EOF;
  128. test($name, $yml);
  129. // ok 5 - Array
  130. // (
  131. //     [0] => Array
  132. //         (
  133. //             [0] => name
  134. //             [1] => hr
  135. //             [2] => avg
  136. //         )
  137. //
  138. //     [1] => Array
  139. //         (
  140. //             [0] => Mark McGwire
  141. //             [1] => 65
  142. //             [2] => 0.278
  143. //         )
  144. //
  145. //     [2] => Array
  146. //         (
  147. //             [0] => Sammy Sosa
  148. //             [1] => 63
  149. //             [2] => 0.288
  150. //         )
  151. //
  152. // )
  153.  
  154. $name = "Example 2.6. Mapping of Mappings";
  155. $yml = <<<EOF
  156. Mark McGwire: {hr: 65, avg: 0.278}
  157. Sammy Sosa: {
  158.     hr: 63,
  159.     avg: 0.288
  160.   }
  161. EOF;
  162. test($name, $yml);
  163. // not ok 6 - Error, unable to load YML:
  164. // Unable to parse string:
  165. // Malformed inline YAML string {
  166.  
  167. $name = "Example 2.7.  Two Documents in a Stream";
  168. $yml = <<<EOF
  169. # Ranking of 1998 home runs
  170. ---
  171. - Mark McGwire
  172. - Sammy Sosa
  173. - Ken Griffey
  174.  
  175. # Team ranking
  176. ---
  177. - Chicago Cubs
  178. - St Louis Cardinals
  179. EOF;
  180. test($name, $yml);
  181. // not ok 7 - Error, unable to load YML:
  182. // Unable to parse string:
  183. // Unable to parse line 2 (---).
  184.  
  185. $name = "Example 2.8.  Play by Play Feed";
  186. $yml = <<<EOF
  187. ---
  188. time: 20:03:20
  189. player: Sammy Sosa
  190. action: strike (miss)
  191. ...
  192. ---
  193. time: 20:03:47
  194. player: Sammy Sosa
  195. action: grand slam
  196. ...
  197. EOF;
  198. test($name, $yml);
  199. // not ok 8 - Error, unable to load YML:
  200. // Unable to parse string:
  201. // Unable to parse line 4 (...).
  202.  
  203. $name = "Example 2.9.  Single Document with Two Comments";
  204. $yml = <<<EOF
  205. ---
  206. hr: # 1998 hr ranking
  207.   - Mark McGwire
  208.   - Sammy Sosa
  209. rbi:
  210.   # 1998 rbi ranking
  211.   - Sammy Sosa
  212.   - Ken Griffey
  213. EOF;
  214. test($name, $yml);
  215. // ok 9 - Array
  216. // (
  217. //     [hr] => Array
  218. //         (
  219. //             [0] => Mark McGwire
  220. //             [1] => Sammy Sosa
  221. //         )
  222. //
  223. //     [rbi] => Array
  224. //         (
  225. //             [0] => Sammy Sosa
  226. //             [1] => Ken Griffey
  227. //         )
  228. //
  229. // )
  230.  
  231. $name = "Example 2.10.  Node for 'Sammy Sosa' appears twice in this document";
  232. $yml = <<<EOF
  233. ---
  234. hr:
  235.   - Mark McGwire
  236.   # Following node labeled SS
  237.   - &SS Sammy Sosa
  238. rbi:
  239.   - *SS # Subsequent occurrence
  240.   - Ken Griffey
  241. EOF;
  242. test($name, $yml);
  243. // ok 10 - Array
  244. // (
  245. //     [hr] => Array
  246. //         (
  247. //             [0] => Mark McGwire
  248. //             [1] => Sammy Sosa
  249. //         )
  250. //
  251. //     [rbi] => Array
  252. //         (
  253. //             [0] => Sammy Sosa
  254. //             [1] => Ken Griffey
  255. //         )
  256. //
  257. // )
  258.  
  259. $name = "Example 2.11. Mapping between Sequences";
  260. $yml = <<<EOF
  261. ? - Detroit Tigers
  262.   - Chicago cubs
  263. :
  264.   - 2001-07-23
  265.  
  266. ? [ New York Yankees,
  267.     Atlanta Braves ]
  268. : [ 2001-07-02, 2001-08-12,
  269.     2001-08-14 ]
  270. EOF;
  271. test($name, $yml);
  272. // not ok 11 - Error, unable to load YML:
  273. // Unable to parse string:
  274. // Unable to parse line 1 (? - Detroit Tigers).
  275.  
  276. $name = "Example 2.12. Compact Nested Mapping";
  277. $yml = <<<EOF
  278. ---
  279. # Products purchased
  280. - item    : Super Hoop
  281.   quantity: 1
  282. - item    : Basketball
  283.   quantity: 4
  284. - item    : Big Shoes
  285.   quantity: 1
  286. EOF;
  287. test($name, $yml);
  288. // not ok 12 - Error, unable to load YML:
  289. // Unable to parse string:
  290. // Unable to parse line 3 (  quantity: 1).
  291.  
  292. $name = "Example 2.13.  In literals, newlines are preserved";
  293. $yml = <<<EOF
  294. # ASCII Art
  295. --- |
  296.   \//||\/||
  297.   // ||  ||__
  298. EOF;
  299. test($name, $yml);
  300. // not ok 13 - Error, unable to load YML:
  301. // Unable to parse string:
  302. // Unable to parse line 2 (--- |).
  303.  
  304. $name = "Example 2.14.  In the folded scalars, newlines become spaces";
  305. $yml = <<<EOF
  306. --- >
  307.   Mark McGwire's
  308.   year was crippled
  309.   by a knee injury.
  310. EOF;
  311. test($name, $yml);
  312. // not ok 14 - Error, unable to load YML:
  313. // Unable to parse string:
  314. // Unable to parse line 1 (  Mark McGwire's).
  315.  
  316. $name = "Example 2.15.  Folded newlines are preserved for 'more indented' and blank lines";
  317. $yml = <<<EOF
  318. >
  319.  Sammy Sosa completed another
  320.  fine season with great stats.
  321.  
  322.    63 Home Runs
  323.    0.288 Batting Average
  324.  
  325.  What a year!
  326. EOF;
  327. test($name, $yml);
  328. // not ok 15 - Error, unable to load YML:
  329. // Unable to parse string:
  330. // Unable to parse line 1 (>).
  331.  
  332. $name = "Example 2.16.  Indentation determines scope";
  333. $yml = <<<EOF
  334. name: Mark McGwire
  335. accomplishment: >
  336.   Mark set a major league
  337.   home run record in 1998.
  338. stats: |
  339.   65 Home Runs
  340.   0.278 Batting Average
  341. EOF;
  342. test($name, $yml);
  343. // ok 16 - Array
  344. // (
  345. //     [name] => Mark McGwire
  346. //     [accomplishment] => Mark set a major league home run record in 1998.
  347. //
  348. //     [stats] => 65 Home Runs
  349. // 0.278 Batting Average
  350. //
  351. // )
  352.  
  353. $name = "Example 2.17. Quoted Scalars";
  354. $yml = <<<EOF
  355. unicode: "Sosa did fine.\u263A"
  356. control: "\b1998\t1999\t2000\n"
  357. hex esc: "\x0d\x0a is \r\n"
  358.  
  359. single: '"Howdy!" he cried.'
  360. quoted: ' # Not a ''comment''.'
  361. tie-fighter: '|\-*-/|'
  362. EOF;
  363. test($name, $yml);
  364. // not ok 17 - Error, unable to load YML:
  365. // Unable to parse string:
  366. // Malformed inline YAML string ("\b1998    1999    2000).
  367.  
  368. $name = "Example 2.18. Multi-line Flow Scalars";
  369. $yml = <<<EOF
  370. plain:
  371.   This unquoted scalar
  372.   spans many lines.
  373.  
  374. quoted: "So does this
  375.   quoted scalar.\n"
  376.  
  377. EOF;
  378. test($name, $yml);
  379. // not ok 18 - Error, unable to load YML:
  380. // Unable to parse string:
  381. // Unable to parse line 2 (This unquoted scalar).
  382.  
  383. $name = "Example 2.19. Integers";
  384. $yml = <<<EOF
  385. canonical: 12345
  386. decimal: +12345
  387. octal: 0o14
  388. hexadecimal: 0xC
  389. EOF;
  390. test($name, $yml);
  391. // ok 19 - Array
  392. // (
  393. //     [canonical] => 12345
  394. //     [decimal] => 12345
  395. //     [octal] => 0o14
  396. //     [hexadecimal] => 12
  397. // )
  398.  
  399. $name = "Example 2.20. Floating Point";
  400. $yml = <<<EOF
  401. canonical: 1.23015e+3
  402. exponential: 12.3015e+02
  403. fixed: 1230.15
  404. negative infinity: -.inf
  405. not a number: .NaN
  406. EOF;
  407. test($name, $yml);
  408. // ok 20 - Array
  409. // (
  410. //     [canonical] => 1230.15
  411. //     [exponential] => 1230.15
  412. //     [fixed] => 1230.15
  413. //     [negative infinity] => -INF
  414. //     [not a number] => INF
  415. // )
  416.  
  417. $name = "Example 2.21. Miscellaneous";
  418. $yml = <<<EOF
  419. null:
  420. booleans: [ true, false ]
  421. string: '012345'
  422. EOF;
  423. test($name, $yml);
  424. // ok 21 - Array
  425. // (
  426. //     [] =>
  427. //     [booleans] => Array
  428. //         (
  429. //             [0] => 1
  430. //             [1] =>
  431. //         )
  432. //
  433. //     [string] => 012345
  434. // )
  435. // Editor note: Actually this is not ok. "null" as a key should evaluate to string "null", not value 'null'
  436.  
  437. $name = "Example 2.22. Timestamps";
  438. $yml = <<<EOF
  439. canonical: 2001-12-15T02:59:43.1Z
  440. iso8601: 2001-12-14t21:59:43.10-05:00
  441. spaced: 2001-12-14 21:59:43.10 -5
  442. date: 2002-12-14
  443. EOF;
  444. test($name, $yml);
  445. // ok 22 - Array
  446. // (
  447. //     [canonical] => 1008385183
  448. //     [iso8601] => 1008385183
  449. //     [spaced] => 1008385183
  450. //     [date] => 1039824000
  451. // )
  452.  
  453. $name = "Example 2.23. Various Explicit Tags";
  454. $yml = <<<EOF
  455. ---
  456. not-date: !!str 2002-04-28
  457.  
  458. picture: !!binary |
  459.  R0lGODlhDAAMAIQAAP//9/X
  460.  17unp5WZmZgAAAOfn515eXv
  461.  Pz7Y6OjuDg4J+fn5OTk6enp
  462.  56enmleECcgggoBADs=
  463.  
  464. application specific tag: !something |
  465.  The semantics of the tag
  466.  above may be different for
  467.  different documents.
  468.  
  469. EOF;
  470. test($name, $yml);
  471. // not ok 23 - Error, unable to load YML:
  472. // Unable to parse string:
  473. // Unable to parse line 4 ( R0lGODlhDAAMAIQAAP//9/X).
  474.  
  475. $name = "Example 2.24. Global Tags";
  476. $yml = <<<EOF
  477. %TAG ! tag:clarkevans.com,2002:
  478. --- !shape
  479.   # Use the ! handle for presenting
  480.   # tag:clarkevans.com,2002:circle
  481. - !circle
  482.   center: &ORIGIN {x: 73, y: 129}
  483.   radius: 7
  484. - !line
  485.   start: *ORIGIN
  486.   finish: { x: 89, y: 102 }
  487. - !label
  488.   start: *ORIGIN
  489.   color: 0xFFEEBB
  490.   text: Pretty vector drawing.
  491. EOF;
  492. test($name, $yml);
  493. // not ok 24 - Error, unable to load YML:
  494. // Unable to parse string:
  495. // Unable to parse line 2 (--- !shape).
  496.  
  497. $name = "Example 2.25. Unordered Sets";
  498. $yml = <<<EOF
  499. # Sets are represented as a
  500. # Mapping where each key is
  501. # associated with a null value
  502. --- !!set
  503. ? Mark McGwire
  504. ? Sammy Sosa
  505. ? Ken Griff
  506. EOF;
  507. test($name, $yml);
  508. // not ok 25 - Error, unable to load YML:
  509. // Unable to parse string:
  510. // Unable to parse line 4 (--- !!set).
  511.  
  512. $name = "Example 2.26. Ordered Mappings";
  513. $yml = <<<EOF
  514. # Ordered maps are represented as
  515. # A sequence of mappings, with
  516. # each mapping having one key
  517. --- !!omap
  518. - Mark McGwire: 65
  519. - Sammy Sosa: 63
  520. - Ken Griffy: 58
  521. EOF;
  522. test($name, $yml);
  523. // not ok 26 - Error, unable to load YML:
  524. // Unable to parse string:
  525. // Unable to parse line 4 (--- !!omap).
  526.  
  527. $name = "Example 2.27. Invoice";
  528. $yml = <<<EOF
  529. --- !<tag:clarkevans.com,2002:invoice>
  530. invoice: 34843
  531. date   : 2001-01-23
  532. bill-to: &id001
  533.   given  : Chris
  534.   family : Dumars
  535.   address:
  536.     lines: |
  537.       458 Walkman Dr.
  538.       Suite #292
  539.     city    : Royal Oak
  540.     state   : MI
  541.     postal  : 48046
  542. ship-to: *id001
  543. product:
  544.   - sku         : BL394D
  545.     quantity    : 4
  546.     description : Basketball
  547.     price       : 450.00
  548.   - sku         : BL4438H
  549.     quantity    : 1
  550.     description : Super Hoop
  551.     price       : 2392.00
  552. tax  : 251.42
  553. total: 4443.52
  554. comments:
  555.   Late afternoon is best.
  556.   Backup contact is Nancy
  557.   Billsmer @ 338-4338.
  558. EOF;
  559. test($name, $yml);
  560. // not ok 27 - Error, unable to load YML:
  561. // Unable to parse string:
  562. // Unable to parse line 16 (  quantity    : 4).
  563.  
  564. $name = "Example 2.28. Log File";
  565. $yml = <<<EOF
  566. ---
  567. Time: 2001-11-23 15:01:42 -5
  568. User: ed
  569. Warning:
  570.   This is an error message
  571.   for the log file
  572. ---
  573. Time: 2001-11-23 15:02:31 -5
  574. User: ed
  575. Warning:
  576.   A slightly different error
  577.   message.
  578. ---
  579. Date: 2001-11-23 15:03:17 -5
  580. User: ed
  581. Fatal:
  582.   Unknown variable "bar"
  583. Stack:
  584.   - file: TopClass.py
  585.     line: 23
  586.     code: |
  587.       x = MoreObject("345\n")
  588.   - file: MoreClass.py
  589.     line: 58
  590.     code: |-
  591.       foo = bar
  592.  
  593. EOF;
  594. test($name, $yml);
  595. // not ok 28 - Error, unable to load YML:
  596. // Unable to parse string:
  597. // Unable to parse line 4 (This is an error message).
  598.  
  599. echo "</table>";