From ebd37c71f62d5ec5f6e27de3e06fee492d4c6298 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 25 Nov 2022 16:40:49 +0100 Subject: [PATCH] parse unquoted digits in tag values as integers --- Inline.php | 3 +-- Tests/DumperTest.php | 9 +-------- Tests/Fixtures/YtsSpecificationExamples.yml | 2 +- Tests/InlineTest.php | 19 +++++++++++++++++++ Tests/ParserTest.php | 12 ++++-------- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Inline.php b/Inline.php index d8994cb3..f93f5967 100644 --- a/Inline.php +++ b/Inline.php @@ -86,7 +86,7 @@ public static function parse(string $value = null, int $flags = 0, array &$refer ++$i; break; default: - $result = self::parseScalar($value, $flags, null, $i, null === $tag, $references); + $result = self::parseScalar($value, $flags, null, $i, true, $references); } // some comments are allowed at the end @@ -657,7 +657,6 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer } return octdec($value); - // Optimize for returning strings. case \in_array($scalar[0], ['+', '-', '.'], true) || is_numeric($scalar[0]): if (Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar)) { $scalar = str_replace('_', '', $scalar); diff --git a/Tests/DumperTest.php b/Tests/DumperTest.php index 59a47a81..4b9c74c2 100644 --- a/Tests/DumperTest.php +++ b/Tests/DumperTest.php @@ -492,8 +492,6 @@ public function testDumpingTaggedValueSequenceWithInlinedTagValues() YAML; $this->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)); } @@ -522,8 +520,6 @@ public function testDumpingTaggedValueMapRespectsInlineLevel() 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)); } @@ -577,9 +573,6 @@ 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)); } @@ -696,7 +689,7 @@ public function testDumpMultiLineStringAsScalarBlock() 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)); } diff --git a/Tests/Fixtures/YtsSpecificationExamples.yml b/Tests/Fixtures/YtsSpecificationExamples.yml index b5f41a2b..2acc4998 100644 --- a/Tests/Fixtures/YtsSpecificationExamples.yml +++ b/Tests/Fixtures/YtsSpecificationExamples.yml @@ -913,7 +913,7 @@ yaml: | no int: ! 12 string: !!str 12 php: | - [ 'integer' => 12, 'no int' => '12', 'string' => '12' ] + [ 'integer' => 12, 'no int' => 12, 'string' => '12' ] --- test: Private types todo: true diff --git a/Tests/InlineTest.php b/Tests/InlineTest.php index 5d338a41..f71509d2 100644 --- a/Tests/InlineTest.php +++ b/Tests/InlineTest.php @@ -743,6 +743,24 @@ public function testTagWithEmptyValueInMapping() $this->assertSame('', $value['foo']->getValue()); } + public function testTagWithQuotedInteger() + { + $value = Inline::parse('!number "5"', Yaml::PARSE_CUSTOM_TAGS); + + $this->assertInstanceOf(TaggedValue::class, $value); + $this->assertSame('number', $value->getTag()); + $this->assertSame('5', $value->getValue()); + } + + public function testTagWithUnquotedInteger() + { + $value = Inline::parse('!number 5', Yaml::PARSE_CUSTOM_TAGS); + + $this->assertInstanceOf(TaggedValue::class, $value); + $this->assertSame('number', $value->getTag()); + $this->assertSame(5, $value->getValue()); + } + public function testUnfinishedInlineMap() { $this->expectException(ParseException::class); @@ -769,6 +787,7 @@ public function getTestsForOctalNumbers() /** * @group legacy + * * @dataProvider getTestsForOctalNumbersYaml11Notation */ public function testParseOctalNumbersYaml11Notation(int $expected, string $yaml, string $replacement) diff --git a/Tests/ParserTest.php b/Tests/ParserTest.php index 228c2f2e..0941075b 100644 --- a/Tests/ParserTest.php +++ b/Tests/ParserTest.php @@ -54,8 +54,7 @@ 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'); + $expected = new TaggedValue('number', 5); $this->assertSameData($expected, $data); } @@ -63,9 +62,8 @@ 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); + + $this->assertSameData(new TaggedValue('tag', null), $data); } public function testTaggedValueTopLevelString() @@ -1555,8 +1553,6 @@ public function testParseDateAsMappingValue() } /** - * @param $lineNumber - * @param $yaml * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider */ public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml) @@ -2310,7 +2306,7 @@ public function taggedValuesProvider() public function testNonSpecificTagSupport() { - $this->assertSame('12', $this->parser->parse('! 12')); + $this->assertSame(12, $this->parser->parse('! 12')); } public function testCustomTagsDisabled()