*/
protected function _dumpNode($key, $value, $indent)
{
+ $literal = false;
// Do some folding here, for blocks.
if (strpos($value, "\n") !== false
|| strpos($value, ': ') !== false
|| strpos($value, '- ') !== false) {
$value = $this->_doLiteralBlock($value, $indent);
+ $literal = true;
} else {
$value = $this->_fold($value, $indent);
}
$spaces = str_repeat(' ', $indent);
+ // Quote strings if necessary, and not folded
+ if (!$literal && strpos($value, "\n") === false && strchr($value, '#')) {
+ $value = "'{$value}'";
+ }
+
if (is_int($key)) {
// It's a sequence.
$string = $spaces . '- ' . $value . "\n";
$this->assertEquals($expected, $actual);
}
+ public function testShouldWrapStringsWithCommentDelimiterInQuotes()
+ {
+ $value = array('foo' => 'string # this is not a comment');
+ $expected = "---\n"
+ . "foo: '{$value['foo']}'\n";
+ $actual = $this->dumper->dump($value);
+ $this->assertEquals($expected, $actual);
+ // round-trip assert
+ $this->assertEquals($value, Horde_Yaml::load($actual), "Error with: >{$actual}<");
+ }
+
+ public function testShouldNotWrapStringsWithCommentDelimiterForFoldedStrings()
+ {
+ // stringWithHash: 'string # this is part of the string, not a comment'
+ $value = array('foo' => 'string # this is not a comment but it is a long string that gets folded', 'bar' => 2);
+ $expected = "---\n"
+ . "foo: >\n"
+ . " string # this is not a comment but it is\n"
+ . " a long string that gets folded\n"
+ . "bar: 2\n";
+
+ $actual = $this->dumper->dump($value);
+ $this->assertEquals($expected, $actual);
+ // round-trip assert
+ $this->assertEquals($value, Horde_Yaml::load($actual), "Error: expected: " . print_r($value, true) . ", actual: " . print_r(Horde_Yaml::load($actual), true) . ", from: >" . $actual . "<"); // fails presently due to bug in loader which causes an extra "\n" at end of string
+ }
+
}
return dirname(__FILE__) . "/fixtures/{$name}.yml";
}
+ public function testUnfolding()
+ {
+ $parsed = Horde_Yaml::loadFile($this->fixture('basic'));
+ $expected = "Line 1 Line 2";
+ $this->assertEquals($expected, $parsed['foldedStringTest']);
+ }
+
+ public function testUnliteralizing()
+ {
+ $parsed = Horde_Yaml::loadFile($this->fixture('basic'));
+ $expected = "Line #1\nLine #2";
+ $this->assertEquals($expected, $parsed['literalStringTest']);
+ }
+
}
{
return false;
}
+
}
foo: bar
-
# A folded block as a mapped value
no time: >
There isn't any time
some time: |
There is nothing but time
for your tricks.
-
+literalStringTest: |
+ Line #1
+ Line #2
+foldedStringTest: >
+ Line 1
+ Line 2