Skip to content

Commit

Permalink
Add an example of custom pattern in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Dec 20, 2019
1 parent c605226 commit 41a3f8e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/JsPhpize/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getNextParseLength($input = null)
return $length - mb_strlen($parser->rest());
}

protected function consume($consumed)
public function consume($consumed)
{
$consumed = is_int($consumed) ? mb_substr($this->input, 0, $consumed) : $consumed;
$this->consumed = mb_strlen(trim($consumed)) > 1 ? $consumed : $this->consumed . $consumed;
Expand Down
4 changes: 1 addition & 3 deletions src/JsPhpize/Lexer/StringLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace JsPhpize\Lexer;

use JsPhpize\Parser\Exception;

class StringLexer
{
/**
Expand Down Expand Up @@ -54,7 +52,7 @@ public function getBackTickString(): string
}

if (!preg_match('/^(\\\\.|\\$(?!{)|[^`$])*`/U', $this->input, $match)) {
throw new Exception('Unterminated ` string after ' . $this->string);
throw new Exception('Unterminated ` string after ' . $this->string, 27);
}

return $this->string . $match[0];
Expand Down
19 changes: 19 additions & 0 deletions tests/CustomPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class CustomPattern extends \JsPhpize\Lexer\Pattern
{
public function __construct()
{
parent::__construct(42, 'string', []);
}

public function lexWith(\JsPhpize\Lexer\Lexer $lexer): Generator
{
if (substr($lexer->rest(), 0, 1) === '@') {
$lexer->consume(1);
yield new \JsPhpize\Lexer\Token('string', ['value' => '"@1"']);
yield new \JsPhpize\Lexer\Token(',', []);
yield new \JsPhpize\Lexer\Token('string', ['value' => '"@2"']);
}
}
}
11 changes: 11 additions & 0 deletions tests/badSyntaxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,15 @@ public function testPhpCode()
$jsPhpize = new JsPhpize();
$jsPhpize->render('DateTime::createFromFormat(\'j-M-Y\', \'15-Feb-2009\')');
}

/**
* @expectedException \JsPhpize\Lexer\Exception
* @expectedExceptionCode 27
* @expectedExceptionMessage Unterminated ` string after `foo ${`bar`}
*/
public function testUnterminatedString()
{
$jsPhpize = new JsPhpize();
$jsPhpize->render('`foo ${`bar`}');
}
}
12 changes: 12 additions & 0 deletions tests/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ public function testPatterns()
$this->assertSame('1 @ 8;', trim($code));
}

/**
* @group patterns
*/
public function testCustomPattern()
{
include_once __DIR__ . '/CustomPattern.php';
$jsPhpize = new JsPhpize();
$jsPhpize->addPattern(new CustomPattern());
$code = $jsPhpize->compile('foo(@)');
$this->assertSame('(function_exists(\'foo\') ? foo("@1", "@2") : $foo("@1", "@2"));', trim($code));
}

/**
* @group patterns
* @expectedException \JsPhpize\Lexer\Exception
Expand Down

0 comments on commit 41a3f8e

Please sign in to comment.