1*d5ef99ddSAndreas Gohr<?php 2*d5ef99ddSAndreas Gohr# 3*d5ef99ddSAndreas Gohr# S P Y C 4*d5ef99ddSAndreas Gohr# a simple php yaml class 5*d5ef99ddSAndreas Gohr# v0.3 6*d5ef99ddSAndreas Gohr# 7*d5ef99ddSAndreas Gohr# author: [chris wanstrath, chris@ozmm.org] 8*d5ef99ddSAndreas Gohr# websites: [http://www.yaml.org, http://spyc.sourceforge.net/] 9*d5ef99ddSAndreas Gohr# license: [MIT License, http://www.opensource.org/licenses/mit-license.php] 10*d5ef99ddSAndreas Gohr# copyright: (c) 2005-2006 Chris Wanstrath 11*d5ef99ddSAndreas Gohr# 12*d5ef99ddSAndreas Gohr# We're gonna load a file into memory and see if we get what we expect. 13*d5ef99ddSAndreas Gohr# If not, we're gonna complain. 14*d5ef99ddSAndreas Gohr# 15*d5ef99ddSAndreas Gohr# Pretty lo-fi. Let's see if we can't get some unit testing going in the next, 16*d5ef99ddSAndreas Gohr# I dunno, 20 months? Alright. Go team. 17*d5ef99ddSAndreas Gohr# 18*d5ef99ddSAndreas Gohr 19*d5ef99ddSAndreas Gohrerror_reporting(E_ALL); 20*d5ef99ddSAndreas Gohr 21*d5ef99ddSAndreas Gohrinclude('spyc.php4'); 22*d5ef99ddSAndreas Gohr 23*d5ef99ddSAndreas Gohr$yaml = Spyc::YAMLLoad('../spyc.yaml'); 24*d5ef99ddSAndreas Gohr 25*d5ef99ddSAndreas Gohr// print_r ($yaml); 26*d5ef99ddSAndreas Gohr 27*d5ef99ddSAndreas Gohr# Added in .2 28*d5ef99ddSAndreas Gohrif ($yaml[1040] != "Ooo, a numeric key!") 29*d5ef99ddSAndreas Gohr die('Key: 1040 failed'); 30*d5ef99ddSAndreas Gohr 31*d5ef99ddSAndreas Gohr# Test mappings / types 32*d5ef99ddSAndreas Gohrif ($yaml['String'] != "Anyone's name, really.") 33*d5ef99ddSAndreas Gohr die('Key: String failed'); 34*d5ef99ddSAndreas Gohr 35*d5ef99ddSAndreas Gohrif ($yaml['Int'] !== 13) 36*d5ef99ddSAndreas Gohr die('Key: Int failed'); 37*d5ef99ddSAndreas Gohr 38*d5ef99ddSAndreas Gohrif ($yaml['True'] !== true) 39*d5ef99ddSAndreas Gohr die('Key: True failed'); 40*d5ef99ddSAndreas Gohr 41*d5ef99ddSAndreas Gohrif ($yaml['False'] !== false) 42*d5ef99ddSAndreas Gohr die('Key: False failed'); 43*d5ef99ddSAndreas Gohr 44*d5ef99ddSAndreas Gohrif ($yaml['Zero'] !== 0) 45*d5ef99ddSAndreas Gohr die('Key: Zero failed'); 46*d5ef99ddSAndreas Gohr 47*d5ef99ddSAndreas Gohrif (isset($yaml['Null'])) 48*d5ef99ddSAndreas Gohr die('Key: Null failed'); 49*d5ef99ddSAndreas Gohr 50*d5ef99ddSAndreas Gohrif ($yaml['Float'] !== 5.34) 51*d5ef99ddSAndreas Gohr die('Key: Float failed'); 52*d5ef99ddSAndreas Gohr 53*d5ef99ddSAndreas Gohr 54*d5ef99ddSAndreas Gohr# Test sequences 55*d5ef99ddSAndreas Gohrif ($yaml[0] != "PHP Class") 56*d5ef99ddSAndreas Gohr die('Sequence 0 failed'); 57*d5ef99ddSAndreas Gohr 58*d5ef99ddSAndreas Gohrif ($yaml[1] != "Basic YAML Loader") 59*d5ef99ddSAndreas Gohr die('Sequence 1 failed'); 60*d5ef99ddSAndreas Gohr 61*d5ef99ddSAndreas Gohrif ($yaml[2] != "Very Basic YAML Dumper") 62*d5ef99ddSAndreas Gohr die('Sequence 2 failed'); 63*d5ef99ddSAndreas Gohr 64*d5ef99ddSAndreas Gohr# A sequence of a sequence 65*d5ef99ddSAndreas Gohrif ($yaml[3] != array("YAML is so easy to learn.", 66*d5ef99ddSAndreas Gohr "Your config files will never be the same.")) 67*d5ef99ddSAndreas Gohr die('Sequence 3 failed'); 68*d5ef99ddSAndreas Gohr 69*d5ef99ddSAndreas Gohr# Sequence of mappings 70*d5ef99ddSAndreas Gohrif ($yaml[4] != array("cpu" => "1.5ghz", "ram" => "1 gig", 71*d5ef99ddSAndreas Gohr "os" => "os x 10.4.1")) 72*d5ef99ddSAndreas Gohr die('Sequence 4 failed'); 73*d5ef99ddSAndreas Gohr 74*d5ef99ddSAndreas Gohr# Mapped sequence 75*d5ef99ddSAndreas Gohrif ($yaml['domains'] != array("yaml.org", "php.net")) 76*d5ef99ddSAndreas Gohr die("Key: 'domains' failed"); 77*d5ef99ddSAndreas Gohr 78*d5ef99ddSAndreas Gohr# A sequence like this. 79*d5ef99ddSAndreas Gohrif ($yaml[5] != array("program" => "Adium", "platform" => "OS X", 80*d5ef99ddSAndreas Gohr "type" => "Chat Client")) 81*d5ef99ddSAndreas Gohr die('Sequence 5 failed'); 82*d5ef99ddSAndreas Gohr 83*d5ef99ddSAndreas Gohr# A folded block as a mapped value 84*d5ef99ddSAndreas Gohrif ($yaml['no time'] != "There isn't any time for your tricks!\nDo you understand?") 85*d5ef99ddSAndreas Gohr die("Key: 'no time' failed"); 86*d5ef99ddSAndreas Gohr 87*d5ef99ddSAndreas Gohr# A literal block as a mapped value 88*d5ef99ddSAndreas Gohrif ($yaml['some time'] != "There is nothing but time\nfor your tricks.") 89*d5ef99ddSAndreas Gohr die("Key: 'some time' failed"); 90*d5ef99ddSAndreas Gohr 91*d5ef99ddSAndreas Gohr# Crazy combinations 92*d5ef99ddSAndreas Gohrif ($yaml['databases'] != array( array("name" => "spartan", "notes" => 93*d5ef99ddSAndreas Gohr array( "Needs to be backed up", 94*d5ef99ddSAndreas Gohr "Needs to be normalized" ), 95*d5ef99ddSAndreas Gohr "type" => "mysql" ))) 96*d5ef99ddSAndreas Gohr die("Key: 'databases' failed"); 97*d5ef99ddSAndreas Gohr 98*d5ef99ddSAndreas Gohr# You can be a bit tricky 99*d5ef99ddSAndreas Gohrif ($yaml["if: you'd"] != "like") 100*d5ef99ddSAndreas Gohr die("Key: 'if: you\'d' failed"); 101*d5ef99ddSAndreas Gohr 102*d5ef99ddSAndreas Gohr# Inline sequences 103*d5ef99ddSAndreas Gohrif ($yaml[6] != array("One", "Two", "Three", "Four")) 104*d5ef99ddSAndreas Gohr die("Sequence 6 failed"); 105*d5ef99ddSAndreas Gohr 106*d5ef99ddSAndreas Gohr# Nested Inline Sequences 107*d5ef99ddSAndreas Gohrif ($yaml[7] != array("One", array("Two", "And", "Three"), "Four", "Five")) 108*d5ef99ddSAndreas Gohr die("Sequence 7 failed"); 109*d5ef99ddSAndreas Gohr 110*d5ef99ddSAndreas Gohr# Nested Nested Inline Sequences 111*d5ef99ddSAndreas Gohrif ($yaml[8] != array( "This", array("Is", "Getting", array("Ridiculous", "Guys")), 112*d5ef99ddSAndreas Gohr "Seriously", array("Show", "Mercy"))) 113*d5ef99ddSAndreas Gohr die("Sequence 8 failed"); 114*d5ef99ddSAndreas Gohr 115*d5ef99ddSAndreas Gohr# Inline mappings 116*d5ef99ddSAndreas Gohrif ($yaml[9] != array("name" => "chris", "age" => "young", "brand" => "lucky strike")) 117*d5ef99ddSAndreas Gohr die("Sequence 9 failed"); 118*d5ef99ddSAndreas Gohr 119*d5ef99ddSAndreas Gohr# Nested inline mappings 120*d5ef99ddSAndreas Gohrif ($yaml[10] != array("name" => "mark", "age" => "older than chris", 121*d5ef99ddSAndreas Gohr "brand" => array("marlboro", "lucky strike"))) 122*d5ef99ddSAndreas Gohr die("Sequence 10 failed"); 123*d5ef99ddSAndreas Gohr 124*d5ef99ddSAndreas Gohr# References -- they're shaky, but functional 125*d5ef99ddSAndreas Gohrif ($yaml['dynamic languages'] != array('Perl', 'Python', 'PHP', 'Ruby')) 126*d5ef99ddSAndreas Gohr die("Key: 'dynamic languages' failed"); 127*d5ef99ddSAndreas Gohr 128*d5ef99ddSAndreas Gohrif ($yaml['compiled languages'] != array('C/C++', 'Java')) 129*d5ef99ddSAndreas Gohr die("Key: 'compiled languages' failed"); 130*d5ef99ddSAndreas Gohr 131*d5ef99ddSAndreas Gohrif ($yaml['all languages'] != array( 132*d5ef99ddSAndreas Gohr array('Perl', 'Python', 'PHP', 'Ruby'), 133*d5ef99ddSAndreas Gohr array('C/C++', 'Java') 134*d5ef99ddSAndreas Gohr )) 135*d5ef99ddSAndreas Gohr die("Key: 'all languages' failed"); 136*d5ef99ddSAndreas Gohr 137*d5ef99ddSAndreas Gohr# Added in .2.2: Escaped quotes 138*d5ef99ddSAndreas Gohrif ($yaml[11] != "you know, this shouldn't work. but it does.") 139*d5ef99ddSAndreas Gohr die("Sequence 11 failed."); 140*d5ef99ddSAndreas Gohr 141*d5ef99ddSAndreas Gohrif ($yaml[12] != "that's my value.") 142*d5ef99ddSAndreas Gohr die("Sequence 12 failed."); 143*d5ef99ddSAndreas Gohr 144*d5ef99ddSAndreas Gohrif ($yaml[13] != "again, that's my value.") 145*d5ef99ddSAndreas Gohr die("Sequence 13 failed."); 146*d5ef99ddSAndreas Gohr 147*d5ef99ddSAndreas Gohrif ($yaml[14] != "here's to \"quotes\", boss.") 148*d5ef99ddSAndreas Gohr die("Sequence 14 failed."); 149*d5ef99ddSAndreas Gohr 150*d5ef99ddSAndreas Gohrif ($yaml[15] != array( 'name' => "Foo, Bar's", 'age' => 20)) 151*d5ef99ddSAndreas Gohr die("Sequence 15 failed."); 152*d5ef99ddSAndreas Gohr 153*d5ef99ddSAndreas Gohrif ($yaml[16] != array( 0 => "a", 1 => array (0 => 1, 1 => 2), 2 => "b")) 154*d5ef99ddSAndreas Gohr die("Sequence 16 failed."); 155*d5ef99ddSAndreas Gohr 156*d5ef99ddSAndreas Gohrif ($yaml['endloop'] != "Does this line in the end indeed make Spyc go to an infinite loop?") 157*d5ef99ddSAndreas Gohr die("[endloop] failed."); 158*d5ef99ddSAndreas Gohr 159*d5ef99ddSAndreas Gohr 160*d5ef99ddSAndreas Gohrprint "spyc.yaml parsed correctly\n"; 161*d5ef99ddSAndreas Gohr 162*d5ef99ddSAndreas Gohr?>