|
15 | 15 | use Symfony\Component\ExpressionLanguage\Lexer;
|
16 | 16 | use Symfony\Component\ExpressionLanguage\Node;
|
17 | 17 | use Symfony\Component\ExpressionLanguage\Parser;
|
| 18 | +use Symfony\Component\ExpressionLanguage\SyntaxError; |
18 | 19 |
|
19 | 20 | class ParserTest extends TestCase
|
20 | 21 | {
|
@@ -234,4 +235,98 @@ public function testNameProposal()
|
234 | 235 |
|
235 | 236 | $parser->parse($lexer->tokenize('foo > bar'), ['foo', 'baz']);
|
236 | 237 | }
|
| 238 | + |
| 239 | + /** |
| 240 | + * @dataProvider getLintData |
| 241 | + */ |
| 242 | + public function testLint($expression, $names, ?string $exception = null) |
| 243 | + { |
| 244 | + if ($exception) { |
| 245 | + $this->expectException(SyntaxError::class); |
| 246 | + $this->expectExceptionMessage($exception); |
| 247 | + } |
| 248 | + |
| 249 | + $lexer = new Lexer(); |
| 250 | + $parser = new Parser([]); |
| 251 | + $parser->lint($lexer->tokenize($expression), $names); |
| 252 | + |
| 253 | + // Parser does't return anything when the correct expression is passed |
| 254 | + $this->expectNotToPerformAssertions(); |
| 255 | + } |
| 256 | + |
| 257 | + public function getLintData(): array |
| 258 | + { |
| 259 | + return [ |
| 260 | + 'valid expression' => [ |
| 261 | + 'expression' => 'foo["some_key"].callFunction(a ? b)', |
| 262 | + 'names' => ['foo', 'a', 'b'], |
| 263 | + ], |
| 264 | + 'allow expression without names' => [ |
| 265 | + 'expression' => 'foo.bar', |
| 266 | + 'names' => null, |
| 267 | + ], |
| 268 | + 'disallow expression without names' => [ |
| 269 | + 'expression' => 'foo.bar', |
| 270 | + 'names' => [], |
| 271 | + 'exception' => 'Variable "foo" is not valid around position 1 for expression `foo.bar', |
| 272 | + ], |
| 273 | + 'operator collisions' => [ |
| 274 | + 'expression' => 'foo.not in [bar]', |
| 275 | + 'names' => ['foo', 'bar'], |
| 276 | + ], |
| 277 | + 'incorrect expression ending' => [ |
| 278 | + 'expression' => 'foo["a"] foo["b"]', |
| 279 | + 'names' => ['foo'], |
| 280 | + 'exception' => 'Unexpected token "name" of value "foo" '. |
| 281 | + 'around position 10 for expression `foo["a"] foo["b"]`.', |
| 282 | + ], |
| 283 | + 'incorrect operator' => [ |
| 284 | + 'expression' => 'foo["some_key"] // 2', |
| 285 | + 'names' => ['foo'], |
| 286 | + 'exception' => 'Unexpected token "operator" of value "/" '. |
| 287 | + 'around position 18 for expression `foo["some_key"] // 2`.', |
| 288 | + ], |
| 289 | + 'incorrect array' => [ |
| 290 | + 'expression' => '[value1, value2 value3]', |
| 291 | + 'names' => ['value1', 'value2', 'value3'], |
| 292 | + 'exception' => 'An array element must be followed by a comma. '. |
| 293 | + 'Unexpected token "name" of value "value3" ("punctuation" expected with value ",") '. |
| 294 | + 'around position 17 for expression `[value1, value2 value3]`.', |
| 295 | + ], |
| 296 | + 'incorrect array element' => [ |
| 297 | + 'expression' => 'foo["some_key")', |
| 298 | + 'names' => ['foo'], |
| 299 | + 'exception' => 'Unclosed "[" around position 3 for expression `foo["some_key")`.', |
| 300 | + ], |
| 301 | + 'missed array key' => [ |
| 302 | + 'expression' => 'foo[]', |
| 303 | + 'names' => ['foo'], |
| 304 | + 'exception' => 'Unexpected token "punctuation" of value "]" around position 5 for expression `foo[]`.', |
| 305 | + ], |
| 306 | + 'missed closing bracket in sub expression' => [ |
| 307 | + 'expression' => 'foo[(bar ? bar : "default"]', |
| 308 | + 'names' => ['foo', 'bar'], |
| 309 | + 'exception' => 'Unclosed "(" around position 4 for expression `foo[(bar ? bar : "default"]`.', |
| 310 | + ], |
| 311 | + 'incorrect hash following' => [ |
| 312 | + 'expression' => '{key: foo key2: bar}', |
| 313 | + 'names' => ['foo', 'bar'], |
| 314 | + 'exception' => 'A hash value must be followed by a comma. '. |
| 315 | + 'Unexpected token "name" of value "key2" ("punctuation" expected with value ",") '. |
| 316 | + 'around position 11 for expression `{key: foo key2: bar}`.', |
| 317 | + ], |
| 318 | + 'incorrect hash assign' => [ |
| 319 | + 'expression' => '{key => foo}', |
| 320 | + 'names' => ['foo'], |
| 321 | + 'exception' => 'Unexpected character "=" around position 5 for expression `{key => foo}`.', |
| 322 | + ], |
| 323 | + 'incorrect array as hash using' => [ |
| 324 | + 'expression' => '[foo: foo]', |
| 325 | + 'names' => ['foo'], |
| 326 | + 'exception' => 'An array element must be followed by a comma. '. |
| 327 | + 'Unexpected token "punctuation" of value ":" ("punctuation" expected with value ",") '. |
| 328 | + 'around position 5 for expression `[foo: foo]`.', |
| 329 | + ], |
| 330 | + ]; |
| 331 | + } |
237 | 332 | }
|
0 commit comments