File: LexerTest.php

package info (click to toggle)
symfony 7.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 147,804 kB
  • sloc: php: 1,506,509; xml: 6,816; javascript: 1,043; sh: 586; makefile: 241; pascal: 70
file content (245 lines) | stat: -rw-r--r-- 8,087 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?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\SyntaxError;
use Symfony\Component\ExpressionLanguage\Token;
use Symfony\Component\ExpressionLanguage\TokenStream;

class LexerTest extends TestCase
{
    private Lexer $lexer;

    protected function setUp(): void
    {
        $this->lexer = new Lexer();
    }

    #[\PHPUnit\Framework\Attributes\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 testTokenizeMultilineComment()
    {
        $expression = <<<EXPRESSION
            /**
             * This is 2^16, even if we could have
             * used the hexadecimal representation 0x10000. Just a
             * matter of taste! 🙂
             */
            65536 and
            /*
             This time, only one star to start the comment and that's it.
             It is valid too!
             */
            65535 /* this is 2^16 - 1 */
            EXPRESSION;

        $tokens = [
            new Token('number', 65536, 128),
            new Token('operator', 'and', 134),
            new Token('number', 65535, 225),
            new Token('end of expression', null, \strlen($expression) + 1),
        ];

        $this->assertEquals(new TokenStream($tokens, str_replace("\n", ' ', $expression)), $this->lexer->tokenize($expression));
    }

    public function testTokenizeThrowsErrorWithMessage()
    {
        $this->expectException(SyntaxError::class);
        $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(SyntaxError::class);
        $this->expectExceptionMessage('Unclosed "(" around position 7 for expression `service(unclosed.expression.dummyMethod()`.');
        $expression = 'service(unclosed.expression.dummyMethod()';
        $this->lexer->tokenize($expression);
    }

    public function testTokenizeOnNotOpenedBracket()
    {
        $this->expectException(SyntaxError::class);
        $this->expectExceptionMessage('Unexpected ")" around position 7 for expression `service)not.opened.expression.dummyMethod()`.');

        $expression = 'service)not.opened.expression.dummyMethod()';

        $this->lexer->tokenize($expression);
    }

    public static 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),
                    new Token('operator', '-', 29),
                    new Token('number', 1990, 31),
                    new Token('operator', '+', 39),
                    new Token('operator', '~', 41),
                    new Token('name', 'qux', 42),
                ],
                '(3 + 5) ~ foo("bar").baz[4] - 1.99E+3 + ~qux',
            ],
            [
                [new Token('operator', '..', 1)],
                '..',
            ],
            [
                [
                    new Token('number', 23, 1),
                    new Token('operator', '..', 3),
                    new Token('number', 26, 5),
                ],
                '23..26',
            ],
            [
                [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]',
            ],
            [
                [new Token('number', 0.787, 1)],
                '0.787',
            ],
            [
                [new Token('number', 0.1234, 1)],
                '.1234',
            ],
            [
                [new Token('number', 188165.1178, 1)],
                '188_165.1_178',
            ],
            [
                [
                    new Token('operator', '-', 1),
                    new Token('number', 7189000000.0, 2),
                ],
                '-.7_189e+10',
            ],
            [
                [
                    new Token('number', 65536, 1),
                ],
                '65536 /* this is 2^16 */',
            ],
            [
                [
                    new Token('number', 2, 1),
                    new Token('operator', '*', 21),
                    new Token('number', 4, 23),
                ],
                '2 /* /* comment1 */ * 4',
            ],
            [
                [
                    new Token('string', '/* this is', 1),
                    new Token('operator', '~', 14),
                    new Token('string', 'not a comment */', 16),
                ],
                '"/* this is" ~ "not a comment */"',
            ],
            [
                [
                    new Token('string', '/* this is not a comment */', 1),
                ],
                '"/* this is not a comment */"',
            ],
            [
                [
                    new Token('name', 'foo', 1),
                    new Token('operator', 'xor', 5),
                    new Token('name', 'bar', 9),
                ],
                'foo xor bar',
            ],
        ];
    }

    public function testOperatorRegexWasGeneratedWithScript()
    {
        ob_start();
        try {
            require $script = \dirname(__DIR__).'/Resources/bin/generate_operator_regex.php';
        } finally {
            $output = ob_get_clean();
        }

        self::assertStringContainsString(
            $output,
            file_get_contents((new \ReflectionClass(Lexer::class))->getFileName()),
            \sprintf('You need to run "%s" to generate the operator regex.', $script),
        );
    }
}