1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-17 21:18:23 +01:00

Fixed another bug in TokensWithDebugging::getPosition()

This commit is contained in:
Filip Halaxa 2022-02-04 15:30:03 +01:00
parent eabe243664
commit 5887899134
3 changed files with 55 additions and 4 deletions

View File

@ -76,7 +76,7 @@ class TokensWithDebugging implements \IteratorAggregate, PositionAware
$tokenWidth = 0;
}
if ($$byte) { // is not whitespace
$this->position = $position + $i;
$this->position = $position + $i + 1;
$this->column = $column;
$this->line = $line;
yield $byte;

View File

@ -12,6 +12,7 @@ use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\Parser;
use JsonMachine\StringChunks;
use JsonMachine\Tokens;
use JsonMachine\TokensWithDebugging;
/**
* @covers \JsonMachine\Parser
@ -478,4 +479,35 @@ class ParserTest extends \PHPUnit_Framework_TestCase
$this->assertSame('value', $item);
}
}
public function testGetPositionReturnsCorrectPositionWithDebugEnabled()
{
$parser = new Parser(new TokensWithDebugging(['[ 1, "two", false ]']));
$expectedPosition = [5, 12, 19];
$this->assertSame(0, $parser->getPosition());
foreach ($parser as $index => $item) {
$this->assertSame($expectedPosition[$index], $parser->getPosition(), "index:$index, item:$item");
}
$this->assertSame(21, $parser->getPosition());
}
public function testGetPositionReturns0WithDebugDisabled()
{
$parser = new Parser(new Tokens(['[ 1, "two", false ]']));
$this->assertSame(0, $parser->getPosition());
foreach ($parser as $index => $item) {
$this->assertSame(0, $parser->getPosition());
}
$this->assertSame(0, $parser->getPosition());
}
public function testGetPositionThrowsIfTokensDoNotSupportGetPosition()
{
$parser = new Parser(new \ArrayObject());
$this->expectException(JsonMachineException::class);
$parser->getPosition();
}
}

View File

@ -168,8 +168,6 @@ class TokensTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expectedToken[1], $tokens->getLine(), 'line failed with expected token #'.$i);
$this->assertEquals($expectedToken[2], $tokens->getColumn(), 'column failed with expected token #'.$i);
}
$this->assertEquals(strlen($jsonFileContents), $tokens->getPosition());
}
/**
@ -189,8 +187,29 @@ class TokensTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, $tokens->getLine(), 'line failed with expected token #'.$i);
$this->assertEquals(0, $tokens->getColumn(), 'column failed with expected token #'.$i);
}
}
$this->assertEquals(0, $tokens->getPosition());
public function testGetPositionWthDebugging()
{
$tokens = new TokensWithDebugging(['[ 1, "two", false ]']);
$expectedPosition = [1, 5, 6, 12, 13, 19, 21];
$this->assertSame(0, $tokens->getPosition());
foreach ($tokens as $index => $item) {
$this->assertSame($expectedPosition[$index], $tokens->getPosition(), "index:$index, item:$item");
}
$this->assertSame(21, $tokens->getPosition());
}
public function testGetPositionNoDebugging()
{
$tokens = new Tokens(['[ 1, "two", false ]']);
$this->assertSame(0, $tokens->getPosition());
foreach ($tokens as $index => $item) {
$this->assertSame(0, $tokens->getPosition(), "index:$index, item:$item");
}
$this->assertSame(0, $tokens->getPosition());
}
public function jsonFilesWithDifferentLineEndings()