assertSame($expected, $yaml);
+ // @todo Fix the parser, preserve numbers.
+ $data[2] = new TaggedValue('number', '5');
+ $this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
}
public function testDumpingTaggedValueMapRespectsInlineLevel()
@@ -437,6 +488,7 @@ public function testDumpingTaggedValueMapRespectsInlineLevel()
'john',
'claire',
]),
+ 'count' => new TaggedValue('number', 5),
];
$yaml = $this->dumper->dump($data, 2);
@@ -447,9 +499,13 @@ public function testDumpingTaggedValueMapRespectsInlineLevel()
names1: !names
- john
- claire
+count: !number 5
YAML;
$this->assertSame($expected, $yaml);
+ // @todo Fix the parser, preserve numbers.
+ $data['count'] = new TaggedValue('number', '5');
+ $this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
}
public function testDumpingTaggedValueMapWithInlinedTagValues()
@@ -472,6 +528,7 @@ public function testDumpingTaggedValueMapWithInlinedTagValues()
YAML;
$this->assertSame($expected, $yaml);
+ $this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
}
public function testDumpingNotInlinedScalarTaggedValue()
@@ -487,6 +544,7 @@ public function testDumpingNotInlinedScalarTaggedValue()
YAML;
$this->assertSame($expected, $this->dumper->dump($data, 2));
+ $this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
}
public function testDumpingNotInlinedNullTaggedValue()
@@ -500,6 +558,10 @@ public function testDumpingNotInlinedNullTaggedValue()
YAML;
$this->assertSame($expected, $this->dumper->dump($data, 2));
+
+ // @todo Fix the parser, don't stringify null.
+ $data['foo'] = new TaggedValue('bar', 'null');
+ $this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_CONSTANT));
}
public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
@@ -519,6 +581,53 @@ public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
' baz';
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+ $this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
+ }
+
+ public function testDumpingTaggedMultiLineInList()
+ {
+ $data = [
+ new TaggedValue('bar', "a\nb"),
+ ];
+ $expected = "- !bar |\n a\n b";
+ $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+
+ // @todo Fix the parser, eliminate these exceptions.
+ $this->expectException(ParseException::class);
+ $this->expectExceptionMessage('Unable to parse at line 3 (near "!bar |").');
+
+ $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS);
+ }
+
+ public function testDumpingTaggedMultiLineTrailingNewlinesInMap()
+ {
+ $data = [
+ 'foo' => new TaggedValue('bar', "a\nb\n\n\n"),
+ ];
+ $expected = "foo: !bar |\n a\n b\n \n \n ";
+ $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+
+ // @todo Fix the parser, the result should be identical to $data.
+ $this->assertSameData(
+ [
+ 'foo' => new TaggedValue('bar', "a\nb\n"),
+ ],
+ $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
+ }
+
+ public function testDumpingTaggedMultiLineTrailingNewlinesInList()
+ {
+ $data = [
+ new TaggedValue('bar', "a\nb\n\n\n"),
+ ];
+ $expected = "- !bar |\n a\n b\n \n \n ";
+ $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+
+ // @todo Fix the parser, eliminate these exceptions.
+ $this->expectException(ParseException::class);
+ $this->expectExceptionMessage('Unable to parse at line 6 (near "!bar |").');
+
+ $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS);
}
public function testDumpingInlinedMultiLineIfRnBreakLineInTaggedValue()
@@ -528,8 +637,14 @@ public function testDumpingInlinedMultiLineIfRnBreakLineInTaggedValue()
'foo' => new TaggedValue('bar', "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
],
];
+ $expected = <<<'YAML'
+data:
+ foo: !bar "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"
- $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_for_tagged_values.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+YAML;
+ $yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
+ $this->assertSame($expected, $yml);
+ $this->assertSameData($data, $this->parser->parse($expected, Yaml::PARSE_CUSTOM_TAGS));
}
public function testDumpMultiLineStringAsScalarBlock()
@@ -544,8 +659,27 @@ public function testDumpMultiLineStringAsScalarBlock()
],
],
];
-
- $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+ $yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
+ $expected = str_replace("@\n", "\n", <<<'YAML'
+data:
+ single_line: 'foo bar baz'
+ multi_line: |-
+ foo
+ line with trailing spaces:
+ @
+ bar
+ integer like line:
+ 123456789
+ empty line:
+
+ baz
+ multi_line_with_carriage_return: "foo\nbar\r\nbaz"
+ nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }
+
+YAML
+);
+ $this->assertSame($expected, $yml);
+ $this->assertSame($data, $this->parser->parse($yml));
}
public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
@@ -558,27 +692,33 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
$expected = "data:\n multi_line: |4-\n the first line has leading spaces\n The second line does not.";
- $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+ $yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
+ $this->assertSame($expected, $yml);
+ $this->assertSame($data, $this->parser->parse($yml));
}
public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
{
- $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+ $data = ["a\r\nb\nc"];
+ $expected = "- \"a\\r\\nb\\nc\"\n";
+ $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+ $this->assertSame($data, $this->parser->parse($expected));
}
public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMultiLineLiteralBlock()
{
+ $data = [
+ 'parent' => [
+ 'foo' => "bar\n\rbaz: qux",
+ ],
+ ];
$expected = <<<'YAML'
parent:
foo: "bar\n\rbaz: qux"
YAML;
-
- $this->assertSame($expected, $this->dumper->dump([
- 'parent' => [
- 'foo' => "bar\n\rbaz: qux",
- ],
- ], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+ $this->assertSame($expected, $this->dumper->dump($data, 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
+ $this->assertSame($data, $this->parser->parse($expected));
}
public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
@@ -590,7 +730,15 @@ public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$this->assertSame("- |-\n a\n b\n- |-\n c\n d", $yaml);
- $this->assertSame($data, Yaml::parse($yaml));
+ $this->assertSame($data, $this->parser->parse($yaml));
+ }
+
+ public function testTopLevelMultiLineStringLiteral()
+ {
+ $data = "a\nb\n";
+ $yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
+ $this->assertSame('"a\nb\n"', $yaml);
+ $this->assertSame($data, $this->parser->parse($yaml));
}
public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
@@ -600,6 +748,7 @@ public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
'clip 2' => "one\ntwo\n",
'keep 1' => "one\ntwo\n",
'keep 2' => "one\ntwo\n\n",
+ 'keep 3' => "one\ntwo\n\n\n",
'strip 1' => "one\ntwo",
'strip 2' => "one\ntwo",
];
@@ -619,6 +768,11 @@ public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
one
two
+'keep 3': |+
+ one
+ two
+
+
'strip 1': |-
one
two
@@ -628,7 +782,7 @@ public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
YAML;
$this->assertSame($expected, $yaml);
- $this->assertSame($data, Yaml::parse($yaml));
+ $this->assertSame($data, $this->parser->parse($yaml));
}
public function testZeroIndentationThrowsException()
@@ -664,6 +818,15 @@ public function testDumpIdeographicSpaces()
'regular_space' => 'a b',
], 2));
}
+
+ private function assertSameData($expected, $actual)
+ {
+ $this->assertEquals($expected, $actual);
+ $this->assertSame(
+ var_export($expected, true),
+ var_export($actual, true)
+ );
+ }
}
class A
diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml
deleted file mode 100644
index 1f61eb1216a52..0000000000000
--- a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml
+++ /dev/null
@@ -1,14 +0,0 @@
-data:
- single_line: 'foo bar baz'
- multi_line: |-
- foo
- line with trailing spaces:
-
- bar
- integer like line:
- 123456789
- empty line:
-
- baz
- multi_line_with_carriage_return: "foo\nbar\r\nbaz"
- nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }
diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_for_tagged_values.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_for_tagged_values.yml
deleted file mode 100644
index f8c9112fd52a5..0000000000000
--- a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_for_tagged_values.yml
+++ /dev/null
@@ -1,2 +0,0 @@
-data:
- foo: !bar "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"
diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php
index 769af36eaa6dd..228c2f2ee9c69 100644
--- a/src/Symfony/Component/Yaml/Tests/ParserTest.php
+++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php
@@ -34,6 +34,90 @@ protected function tearDown(): void
chmod(__DIR__.'/Fixtures/not_readable.yml', 0644);
}
+ public function testTopLevelNumber()
+ {
+ $yml = '5';
+ $data = $this->parser->parse($yml);
+ $expected = 5;
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTopLevelNull()
+ {
+ $yml = 'null';
+ $data = $this->parser->parse($yml);
+ $expected = null;
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTaggedValueTopLevelNumber()
+ {
+ $yml = '!number 5';
+ $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
+ // @todo Preserve the number, don't turn into string.
+ $expected = new TaggedValue('number', '5');
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTaggedValueTopLevelNull()
+ {
+ $yml = '!tag null';
+ $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
+ // @todo Preserve literal null, don't turn into string.
+ $expected = new TaggedValue('tag', 'null');
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTaggedValueTopLevelString()
+ {
+ $yml = '!user barbara';
+ $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
+ $expected = new TaggedValue('user', 'barbara');
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTaggedValueTopLevelAssocInline()
+ {
+ $yml = '!user { name: barbara }';
+ $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
+ $expected = new TaggedValue('user', ['name' => 'barbara']);
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTaggedValueTopLevelAssoc()
+ {
+ $yml = <<<'YAML'
+!user
+name: barbara
+YAML;
+ $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
+ $expected = new TaggedValue('user', ['name' => 'barbara']);
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTaggedValueTopLevelList()
+ {
+ $yml = <<<'YAML'
+!users
+- barbara
+YAML;
+ $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
+ $expected = new TaggedValue('users', ['barbara']);
+ $this->assertSameData($expected, $data);
+ }
+
+ public function testTaggedTextAsListItem()
+ {
+ $yml = <<<'YAML'
+- !text |
+ first line
+YAML;
+ // @todo Fix the parser, eliminate this exception.
+ $this->expectException(ParseException::class);
+ $this->expectExceptionMessage('Unable to parse at line 2 (near "!text |").');
+ $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
+ }
+
/**
* @dataProvider getDataFormSpecifications
*/
@@ -104,7 +188,7 @@ public function testParserIsStateless()
public function testValidTokenSeparation(string $given, array $expected)
{
$actual = $this->parser->parse($given);
- $this->assertEquals($expected, $actual);
+ $this->assertSameData($expected, $actual);
}
public function validTokenSeparators(): array
@@ -482,7 +566,7 @@ public function testObjectSupportEnabled()
foo: !php/object O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
bar: 1
EOF;
- $this->assertEquals(['foo' => new B(), 'bar' => 1], $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
+ $this->assertSameData(['foo' => new B(), 'bar' => 1], $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
}
public function testObjectSupportDisabledButNoExceptions()
@@ -491,7 +575,7 @@ public function testObjectSupportDisabledButNoExceptions()
foo: !php/object O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
bar: 1
EOF;
- $this->assertEquals(['foo' => null, 'bar' => 1], $this->parser->parse($input), '->parse() does not parse objects');
+ $this->assertSameData(['foo' => null, 'bar' => 1], $this->parser->parse($input), '->parse() does not parse objects');
}
/**
@@ -501,7 +585,7 @@ public function testObjectForMap($yaml, $expected)
{
$flags = Yaml::PARSE_OBJECT_FOR_MAP;
- $this->assertEquals($expected, $this->parser->parse($yaml, $flags));
+ $this->assertSameData($expected, $this->parser->parse($yaml, $flags));
}
public function getObjectForMapTests()
@@ -956,12 +1040,12 @@ public function testEmptyValue()
hash:
EOF;
- $this->assertEquals(['hash' => null], Yaml::parse($input));
+ $this->assertSame(['hash' => null], Yaml::parse($input));
}
public function testCommentAtTheRootIndent()
{
- $this->assertEquals([
+ $this->assertSame([
'services' => [
'app.foo_service' => [
'class' => 'Foo',
@@ -987,7 +1071,7 @@ class: Bar
public function testStringBlockWithComments()
{
- $this->assertEquals(['content' => <<<'EOT'
+ $this->assertSame(['content' => <<<'EOT'
# comment 1
header
@@ -1015,7 +1099,7 @@ public function testStringBlockWithComments()
public function testFoldedStringBlockWithComments()
{
- $this->assertEquals([['content' => <<<'EOT'
+ $this->assertSame([['content' => <<<'EOT'
# comment 1
header
@@ -1044,7 +1128,7 @@ public function testFoldedStringBlockWithComments()
public function testNestedFoldedStringBlockWithComments()
{
- $this->assertEquals([[
+ $this->assertSame([[
'title' => 'some title',
'content' => <<<'EOT'
# comment 1
@@ -1076,7 +1160,7 @@ public function testNestedFoldedStringBlockWithComments()
public function testReferenceResolvingInInlineStrings()
{
- $this->assertEquals([
+ $this->assertSame([
'var' => 'var-value',
'scalar' => 'var-value',
'list' => ['var-value'],
@@ -1116,7 +1200,7 @@ public function testYamlDirective()
foo: 1
bar: 2
EOF;
- $this->assertEquals(['foo' => 1, 'bar' => 2], $this->parser->parse($yaml));
+ $this->assertSame(['foo' => 1, 'bar' => 2], $this->parser->parse($yaml));
}
public function testFloatKeys()
@@ -1166,7 +1250,7 @@ public function testExplicitStringCasting()
'~' => 'null',
];
- $this->assertEquals($expected, $this->parser->parse($yaml));
+ $this->assertSame($expected, $this->parser->parse($yaml));
}
public function testColonInMappingValueException()
@@ -1467,7 +1551,7 @@ public function testParseDateAsMappingValue()
$expectedDate->setDate(2002, 12, 14);
$expectedDate->setTime(0, 0, 0);
- $this->assertEquals(['date' => $expectedDate], $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
+ $this->assertSameData(['date' => $expectedDate], $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
}
/**
@@ -1687,7 +1771,7 @@ public function testBackslashInSingleQuotedString()
public function testParseMultiLineString()
{
- $this->assertEquals("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz"));
+ $this->assertSame("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz"));
}
/**
@@ -1695,7 +1779,7 @@ public function testParseMultiLineString()
*/
public function testParseMultiLineMappingValue($yaml, $expected, $parseError)
{
- $this->assertEquals($expected, $this->parser->parse($yaml));
+ $this->assertSame($expected, $this->parser->parse($yaml));
}
public function multiLineDataProvider()
@@ -1762,7 +1846,7 @@ public function multiLineDataProvider()
*/
public function testInlineNotationSpanningMultipleLines($expected, string $yaml)
{
- $this->assertEquals($expected, $this->parser->parse($yaml));
+ $this->assertSame($expected, $this->parser->parse($yaml));
}
public function inlineNotationSpanningMultipleLinesProvider(): array
@@ -2136,7 +2220,7 @@ public function testRootLevelInlineMappingFollowedByMoreContentIsInvalid()
public function testTaggedInlineMapping()
{
- $this->assertEquals(new TaggedValue('foo', ['foo' => 'bar']), $this->parser->parse('!foo {foo: bar}', Yaml::PARSE_CUSTOM_TAGS));
+ $this->assertSameData(new TaggedValue('foo', ['foo' => 'bar']), $this->parser->parse('!foo {foo: bar}', Yaml::PARSE_CUSTOM_TAGS));
}
public function testInvalidInlineSequenceContainingStringWithEscapedQuotationCharacter()
@@ -2151,7 +2235,7 @@ public function testInvalidInlineSequenceContainingStringWithEscapedQuotationCha
*/
public function testCustomTagSupport($expected, $yaml)
{
- $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS));
+ $this->assertSameData($expected, $this->parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS));
}
public function taggedValuesProvider()
@@ -2347,7 +2431,7 @@ public function testCanParseVeryLongValue()
$yamlString = Yaml::dump($trickyVal);
$arrayFromYaml = $this->parser->parse($yamlString);
- $this->assertEquals($trickyVal, $arrayFromYaml);
+ $this->assertSame($trickyVal, $arrayFromYaml);
}
public function testParserCleansUpReferencesBetweenRuns()
@@ -2462,7 +2546,7 @@ public function testMergeKeysWhenMappingsAreParsedAsObjects()
],
];
- $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
+ $this->assertSameData($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
}
public function testFilenamesAreParsedAsStringsWithoutFlag()
@@ -2555,7 +2639,7 @@ public function testParseReferencesOnMergeKeysWithMappingsParsedAsObjects()
],
];
- $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
+ $this->assertSameData($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
}
public function testEvalRefException()
@@ -2830,6 +2914,15 @@ public function testParseIdeographicSpaces()
'regular_space' => 'a b',
], $this->parser->parse($expected));
}
+
+ private function assertSameData($expected, $actual)
+ {
+ $this->assertEquals($expected, $actual);
+ $this->assertSame(
+ var_export($expected, true),
+ var_export($actual, true)
+ );
+ }
}
class B
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy