1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-26 20:15:02 +01:00

Fix empty array parsing

Closes #21.
This commit is contained in:
Aaron Piotrowski 2020-01-16 14:12:26 -06:00
parent 15f840335d
commit d88ec8cd35
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 76 additions and 4 deletions

View File

@ -42,15 +42,23 @@ final class ArrayParser
*/
private function parser(string $data, callable $cast = null, string $delimiter = ','): \Generator
{
if ($data[0] !== '{' || \substr($data, -1) !== '}') {
throw new ParseException("Missing opening or closing brackets");
if ($data === '') {
throw new ParseException("Unexpected end of data");
}
if ($data[0] !== '{') {
throw new ParseException("Missing opening bracket");
}
$data = \ltrim(\substr($data, 1));
do {
if ($data === '') {
throw new ParseException("Missing closing bracket");
throw new ParseException("Unexpected end of data");
}
if ($data[0] === '}') { // Empty array
return \ltrim(\substr($data, 1));
}
if ($data[0] === '{') { // Array

View File

@ -128,15 +128,79 @@ class ArrayParserTest extends TestCase
$this->assertSame($array, $this->parser->parse($string));
}
public function testEmptyArray()
{
$array = [];
$string = '{}';
$this->assertSame($array, $this->parser->parse($string));
}
public function testArrayContainingEmptyArray()
{
$array = [[], [1], []];
$string = '{{},{1},{}}';
$cast = function (string $value): int {
return (int) $value;
};
$this->assertSame($array, $this->parser->parse($string, $cast));
}
public function testArrayWithEmptyString()
{
$array = [''];
$string = '{""}';
$this->assertSame($array, $this->parser->parse($string));
}
public function testMalformedNestedArray()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Unexpected end of data');
$string = '{{}';
$this->parser->parse($string);
}
public function testEmptyString()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Unexpected end of data');
$string = ' ';
$this->parser->parse($string);
}
public function testNoOpeningBracket()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Missing opening bracket');
$string = '"one", "two"}';
$this->parser->parse($string);
}
public function testNoClosingBracket()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Missing opening or closing brackets');
$this->expectExceptionMessage('Unexpected end of data');
$string = '{"one", "two"';
$this->parser->parse($string);
}
public function testExtraClosingBracket()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Data left in buffer after parsing');
$string = '{"one", "two"}}';
$this->parser->parse($string);
}
public function testTrailingData()
{
$this->expectException(ParseException::class);