From 58878991349bb2892e2c088aad4f041554e669dc Mon Sep 17 00:00:00 2001 From: Filip Halaxa Date: Fri, 4 Feb 2022 15:30:03 +0100 Subject: [PATCH] Fixed another bug in TokensWithDebugging::getPosition() --- src/TokensWithDebugging.php | 2 +- test/JsonMachineTest/ParserTest.php | 32 +++++++++++++++++++++++++++++ test/JsonMachineTest/TokensTest.php | 25 +++++++++++++++++++--- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/TokensWithDebugging.php b/src/TokensWithDebugging.php index 98a7f95..59b9400 100644 --- a/src/TokensWithDebugging.php +++ b/src/TokensWithDebugging.php @@ -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; diff --git a/test/JsonMachineTest/ParserTest.php b/test/JsonMachineTest/ParserTest.php index 2e72ef1..0b11025 100644 --- a/test/JsonMachineTest/ParserTest.php +++ b/test/JsonMachineTest/ParserTest.php @@ -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(); + } } diff --git a/test/JsonMachineTest/TokensTest.php b/test/JsonMachineTest/TokensTest.php index ec1740d..09cf7d9 100644 --- a/test/JsonMachineTest/TokensTest.php +++ b/test/JsonMachineTest/TokensTest.php @@ -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()