Skip to content

Commit

Permalink
parse unquoted digits in tag values as integers
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Nov 25, 2022
1 parent e83fe9a commit ebd37c7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
3 changes: 1 addition & 2 deletions Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 1 addition & 8 deletions Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/YtsSpecificationExamples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -769,6 +787,7 @@ public function getTestsForOctalNumbers()

/**
* @group legacy
*
* @dataProvider getTestsForOctalNumbersYaml11Notation
*/
public function testParseOctalNumbersYaml11Notation(int $expected, string $yaml, string $replacement)
Expand Down
12 changes: 4 additions & 8 deletions Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ 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);
}

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()
Expand Down Expand Up @@ -1555,8 +1553,6 @@ public function testParseDateAsMappingValue()
}

/**
* @param $lineNumber
* @param $yaml
* @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
*/
public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ebd37c7

Please sign in to comment.