Skip to content

Commit

Permalink
Added support for an empty comment
Browse files Browse the repository at this point in the history
  • Loading branch information
waltertamboer committed Feb 22, 2022
1 parent a223e5f commit a490936
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Lexer/AbstractLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function getNextToken(): ?TokenInterface
}

// Match a comment:
if (preg_match('/^\s*\{(.+?)\}\s*/s', $this->buffer, $matches)) {
if (preg_match('/^\s*\{(.*?)\}\s*/s', $this->buffer, $matches)) {
$this->buffer = substr($this->buffer, strlen($matches[0]));
return new Comment($matches[1]);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Lexer/StringLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace ChessZebra\PortableGameNotation\Lexer;

use ChessZebra\PortableGameNotation\Token\Comment;
use ChessZebra\PortableGameNotation\Token\MoveNumber;
use PHPUnit\Framework\TestCase;

Expand All @@ -27,4 +28,34 @@ public function testConstructor()
// Assert
static::assertInstanceOf(MoveNumber::class, $result);
}

public function testEmptyComment()
{
// Arrange
$buffer = '{}';

$lexer = new StringLexer($buffer);

// Act
$result = $lexer->peekNextToken();

// Assert
static::assertInstanceOf(Comment::class, $result);
static::assertEquals('', $result->getComment());
}

public function testPopulatedComment()
{
// Arrange
$buffer = '{ hello world }';

$lexer = new StringLexer($buffer);

// Act
$result = $lexer->peekNextToken();

// Assert
static::assertInstanceOf(Comment::class, $result);
static::assertEquals(' hello world ', $result->getComment());
}
}

0 comments on commit a490936

Please sign in to comment.