Skip to content

Commit a490936

Browse files
committed
Added support for an empty comment
1 parent a223e5f commit a490936

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Lexer/AbstractLexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function getNextToken(): ?TokenInterface
9797
}
9898

9999
// Match a comment:
100-
if (preg_match('/^\s*\{(.+?)\}\s*/s', $this->buffer, $matches)) {
100+
if (preg_match('/^\s*\{(.*?)\}\s*/s', $this->buffer, $matches)) {
101101
$this->buffer = substr($this->buffer, strlen($matches[0]));
102102
return new Comment($matches[1]);
103103
}

tests/Lexer/StringLexerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace ChessZebra\PortableGameNotation\Lexer;
1111

12+
use ChessZebra\PortableGameNotation\Token\Comment;
1213
use ChessZebra\PortableGameNotation\Token\MoveNumber;
1314
use PHPUnit\Framework\TestCase;
1415

@@ -27,4 +28,34 @@ public function testConstructor()
2728
// Assert
2829
static::assertInstanceOf(MoveNumber::class, $result);
2930
}
31+
32+
public function testEmptyComment()
33+
{
34+
// Arrange
35+
$buffer = '{}';
36+
37+
$lexer = new StringLexer($buffer);
38+
39+
// Act
40+
$result = $lexer->peekNextToken();
41+
42+
// Assert
43+
static::assertInstanceOf(Comment::class, $result);
44+
static::assertEquals('', $result->getComment());
45+
}
46+
47+
public function testPopulatedComment()
48+
{
49+
// Arrange
50+
$buffer = '{ hello world }';
51+
52+
$lexer = new StringLexer($buffer);
53+
54+
// Act
55+
$result = $lexer->peekNextToken();
56+
57+
// Assert
58+
static::assertInstanceOf(Comment::class, $result);
59+
static::assertEquals(' hello world ', $result->getComment());
60+
}
3061
}

0 commit comments

Comments
 (0)