mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 17:36:53 +00:00
lexer tests for interpolated strings
This commit is contained in:
@@ -138,4 +138,90 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
// should not throw an exception
|
||||
}
|
||||
|
||||
public function testString()
|
||||
{
|
||||
$template = 'foo {{ "bar #{ baz + 1 }" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment());
|
||||
$stream = $lexer->tokenize($template);
|
||||
$stream->expect(Twig_Token::TEXT_TYPE, 'foo ');
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar ');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_START_TYPE);
|
||||
$stream->expect(Twig_Token::NAME_TYPE, 'baz');
|
||||
$stream->expect(Twig_Token::OPERATOR_TYPE, '+');
|
||||
$stream->expect(Twig_Token::NUMBER_TYPE, '1');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
|
||||
$stream->expect(Twig_Token::VAR_END_TYPE);
|
||||
}
|
||||
|
||||
public function testStringWithEscapedInterpolation()
|
||||
{
|
||||
$template = '{{ "bar \#{baz+1}" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment());
|
||||
$stream = $lexer->tokenize($template);
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar #{baz+1}');
|
||||
$stream->expect(Twig_Token::VAR_END_TYPE);
|
||||
}
|
||||
|
||||
public function testStringWithHash()
|
||||
{
|
||||
$template = '{{ "bar # baz" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment());
|
||||
$stream = $lexer->tokenize($template);
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar # baz');
|
||||
$stream->expect(Twig_Token::VAR_END_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage Unclosed """
|
||||
*/
|
||||
public function testStringWithUnterminatedInterpolation()
|
||||
{
|
||||
$template = '{{ "bar #{x" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment());
|
||||
$stream = $lexer->tokenize($template);
|
||||
}
|
||||
|
||||
public function testStringWithNestedInterpolations()
|
||||
{
|
||||
$template = '{{ "bar #{ "foo#{bar}" }" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment());
|
||||
$stream = $lexer->tokenize($template);
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar ');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'foo');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_START_TYPE);
|
||||
$stream->expect(Twig_Token::NAME_TYPE, 'bar');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
|
||||
$stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
|
||||
$stream->expect(Twig_Token::VAR_END_TYPE);
|
||||
}
|
||||
|
||||
public function testStringWithNestedInterpolationsInBlock()
|
||||
{
|
||||
$template = '{% foo "bar #{ "foo#{bar}" }" %}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment());
|
||||
$stream = $lexer->tokenize($template);
|
||||
$stream->expect(Twig_Token::BLOCK_START_TYPE);
|
||||
$stream->expect(Twig_Token::NAME_TYPE, 'foo');
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar ');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'foo');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_START_TYPE);
|
||||
$stream->expect(Twig_Token::NAME_TYPE, 'bar');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
|
||||
$stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
|
||||
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user