/
LexerTest.php
129 lines (120 loc) · 4.04 KB
/
LexerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ExpressionLanguage\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\Lexer;
use Symfony\Component\ExpressionLanguage\Token;
use Symfony\Component\ExpressionLanguage\TokenStream;
class LexerTest extends TestCase
{
/**
* @var Lexer
*/
private $lexer;
protected function setUp()
{
$this->lexer = new Lexer();
}
/**
* @dataProvider getTokenizeData
*/
public function testTokenize($tokens, $expression)
{
$tokens[] = new Token('end of expression', null, \strlen($expression) + 1);
$this->assertEquals(new TokenStream($tokens, $expression), $this->lexer->tokenize($expression));
}
public function testTokenizeThrowsErrorWithMessage()
{
$this->expectException('Symfony\Component\ExpressionLanguage\SyntaxError');
$this->expectExceptionMessage('Unexpected character "\'" around position 33 for expression `service(faulty.expression.example\').dummyMethod()`.');
$expression = "service(faulty.expression.example').dummyMethod()";
$this->lexer->tokenize($expression);
}
public function testTokenizeThrowsErrorOnUnclosedBrace()
{
$this->expectException('Symfony\Component\ExpressionLanguage\SyntaxError');
$this->expectExceptionMessage('Unclosed "(" around position 7 for expression `service(unclosed.expression.dummyMethod()`.');
$expression = 'service(unclosed.expression.dummyMethod()';
$this->lexer->tokenize($expression);
}
public function getTokenizeData()
{
return [
[
[new Token('name', 'a', 3)],
' a ',
],
[
[new Token('name', 'a', 1)],
'a',
],
[
[new Token('string', 'foo', 1)],
'"foo"',
],
[
[new Token('number', '3', 1)],
'3',
],
[
[new Token('operator', '+', 1)],
'+',
],
[
[new Token('punctuation', '.', 1)],
'.',
],
[
[
new Token('punctuation', '(', 1),
new Token('number', '3', 2),
new Token('operator', '+', 4),
new Token('number', '5', 6),
new Token('punctuation', ')', 7),
new Token('operator', '~', 9),
new Token('name', 'foo', 11),
new Token('punctuation', '(', 14),
new Token('string', 'bar', 15),
new Token('punctuation', ')', 20),
new Token('punctuation', '.', 21),
new Token('name', 'baz', 22),
new Token('punctuation', '[', 25),
new Token('number', '4', 26),
new Token('punctuation', ']', 27),
],
'(3 + 5) ~ foo("bar").baz[4]',
],
[
[new Token('operator', '..', 1)],
'..',
],
[
[new Token('string', '#foo', 1)],
"'#foo'",
],
[
[new Token('string', '#foo', 1)],
'"#foo"',
],
[
[
new Token('name', 'foo', 1),
new Token('punctuation', '.', 4),
new Token('name', 'not', 5),
new Token('operator', 'in', 9),
new Token('punctuation', '[', 12),
new Token('name', 'bar', 13),
new Token('punctuation', ']', 16),
],
'foo.not in [bar]',
],
];
}
}