aboutsummaryrefslogtreecommitdiffstats
path: root/lib/fabpot-yaml/test/sfYamlInlineTest.php
blob: 097fb266d5c458baba676e491969dc3ce77c3af8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

require_once(dirname(__FILE__).'/lime/lime.php');
require_once(dirname(__FILE__).'/../lib/sfYaml.php');
require_once(dirname(__FILE__).'/../lib/sfYamlInline.php');

sfYaml::setSpecVersion('1.1');

$t = new lime_test(124);

// ::load()
$t->diag('::load()');

$testsForLoad = array(
  '' => '',
  'null' => null,
  'false' => false,
  'true' => true,
  '12' => 12,
  '"quoted string"' => 'quoted string',
  "'quoted string'" => 'quoted string',
  '12.30e+02' => 12.30e+02,
  '0x4D2' => 0x4D2,
  '02333' => 02333,
  '.Inf' => -log(0),
  '-.Inf' => log(0),
  '123456789123456789' => '123456789123456789',
  '"foo\r\nbar"' => "foo\r\nbar",
  "'foo#bar'" => 'foo#bar',
  "'foo # bar'" => 'foo # bar',
  "'#cfcfcf'" => '#cfcfcf',

  '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),

  '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',

  // sequences
  // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  '[  foo  ,   bar , false  ,  null     ,  12  ]' => array('foo', 'bar', false, null, 12),
  '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),

  // mappings
  '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  '{ foo  : bar, bar : foo,  false  :   false,  null  :   null,  integer :  12  }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),

  // nested sequences and mappings
  '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
  '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),

  '[  foo, [  bar, foo  ]  ]' => array('foo', array('bar', 'foo')),

  '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),

  '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),

  '[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),

  '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
);

foreach ($testsForLoad as $yaml => $value)
{
  $t->is_deeply(sfYamlInline::load($yaml), $value, sprintf('::load() converts an inline YAML to a PHP structure (%s)', $yaml));
}

$testsForDump = array(
  'null' => null,
  'false' => false,
  'true' => true,
  '12' => 12,
  "'quoted string'" => 'quoted string',
  '12.30e+02' => 12.30e+02,
  '1234' => 0x4D2,
  '1243' => 02333,
  '.Inf' => -log(0),
  '-.Inf' => log(0),
  '"foo\r\nbar"' => "foo\r\nbar",
  "'foo#bar'" => 'foo#bar',
  "'foo # bar'" => 'foo # bar',
  "'#cfcfcf'" => '#cfcfcf',

  "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',

  // sequences
  '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),

  // mappings
  '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),

  // nested sequences and mappings
  '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),

  '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),

  '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),

  '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),

  '[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),
);

// ::dump()
$t->diag('::dump()');
foreach ($testsForDump as $yaml => $value)
{
  $t->is(sfYamlInline::dump($value), $yaml, sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
}

foreach ($testsForLoad as $yaml => $value)
{
  if ($value == 1230)
  {
    continue;
  }

  $t->is_deeply(sfYamlInline::load(sfYamlInline::dump($value)), $value, 'check consistency');
}

foreach ($testsForDump as $yaml => $value)
{
  if ($value == 1230)
  {
    continue;
  }

  $t->is_deeply(sfYamlInline::load(sfYamlInline::dump($value)), $value, 'check consistency');
}