|
10 | 10 | class TokenizerTest extends TestCase
|
11 | 11 | {
|
12 | 12 | /**
|
13 |
| - * @covers ::__construct |
14 |
| - * @covers ::tokens |
| 13 | + * @covers ::match |
15 | 14 | */
|
16 |
| - public function testTokens() |
| 15 | + public function testMatch(): void |
17 | 16 | {
|
18 |
| - $string = "user.likes(['(', ')']).drink"; |
19 |
| - $tokenizer = new Tokenizer($string); |
20 |
| - $tokens = $tokenizer->tokens(); |
| 17 | + $string = 'Find ?? a TRUE'; |
21 | 18 |
|
22 |
| - $token = $tokens->current(); |
23 |
| - $this->assertSame(TokenType::T_IDENTIFIER, $token->type); |
24 |
| - $this->assertSame('user', $token->lexeme); |
25 |
| - |
26 |
| - $tokens->next(); |
27 |
| - $token = $tokens->current(); |
28 |
| - $this->assertSame(TokenType::T_DOT, $token->type); |
29 |
| - |
30 |
| - $tokens->next(); |
31 |
| - $token = $tokens->current(); |
32 |
| - $this->assertSame(TokenType::T_IDENTIFIER, $token->type); |
33 |
| - $this->assertSame('likes', $token->lexeme); |
| 19 | + $this->assertNull(Tokenizer::match($string, 0, '\?\?')); |
| 20 | + $this->assertSame('??', Tokenizer::match($string, 5, '\?\?')); |
34 | 21 |
|
35 |
| - $tokens->next(); |
36 |
| - $token = $tokens->current(); |
37 |
| - $this->assertSame(TokenType::T_OPEN_PAREN, $token->type); |
38 |
| - |
39 |
| - $tokens->next(); |
40 |
| - $token = $tokens->current(); |
41 |
| - $this->assertSame(TokenType::T_OPEN_BRACKET, $token->type); |
42 |
| - |
43 |
| - $tokens->next(); |
44 |
| - $token = $tokens->current(); |
45 |
| - $this->assertSame(TokenType::T_STRING, $token->type); |
46 |
| - $this->assertSame("'('", $token->lexeme); |
| 22 | + $this->assertNull(Tokenizer::match($string, 10, 'true')); |
| 23 | + $this->assertSame('TRUE', Tokenizer::match($string, 10, 'true', true)); |
| 24 | + } |
47 | 25 |
|
48 |
| - $tokens->next(); |
49 |
| - $token = $tokens->current(); |
50 |
| - $this->assertSame(TokenType::T_COMMA, $token->type); |
| 26 | + public static function stringProvider(): string |
| 27 | + { |
| 28 | + return 'site?.([\'number\' => 3], null) ? (true ?: 4.1) : ("fox" ?? false)'; |
| 29 | + } |
51 | 30 |
|
52 |
| - $tokens->next(); |
53 |
| - $token = $tokens->current(); |
54 |
| - $this->assertSame(TokenType::T_STRING, $token->type); |
55 |
| - $this->assertSame("')'", $token->lexeme); |
| 31 | + public static function tokenProvider(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + [0, TokenType::T_IDENTIFIER, 'site'], |
| 35 | + [4, TokenType::T_NULLSAFE, '?.'], |
| 36 | + [6, TokenType::T_OPEN_PAREN, '('], |
| 37 | + [7, TokenType::T_OPEN_BRACKET, '['], |
| 38 | + [8, TokenType::T_STRING, '\'number\'', 'number'], |
| 39 | + [16, TokenType::T_WHITESPACE, ' '], |
| 40 | + [17, TokenType::T_ARROW, '=>'], |
| 41 | + [20, TokenType::T_INTEGER, '3', 3], |
| 42 | + [21, TokenType::T_CLOSE_BRACKET, ']'], |
| 43 | + [22, TokenType::T_COMMA, ','], |
| 44 | + [24, TokenType::T_NULL, 'null', null], |
| 45 | + [28, TokenType::T_CLOSE_PAREN, ')'], |
| 46 | + [30, TokenType::T_QUESTION_MARK, '?'], |
| 47 | + [32, TokenType::T_OPEN_PAREN, '('], |
| 48 | + [33, TokenType::T_TRUE, 'true', true], |
| 49 | + [38, TokenType::T_TERNARY_DEFAULT, '?:'], |
| 50 | + [41, TokenType::T_FLOAT, '4.1', 4.1], |
| 51 | + [44, TokenType::T_CLOSE_PAREN, ')'], |
| 52 | + [46, TokenType::T_COLON, ':'], |
| 53 | + [48, TokenType::T_OPEN_PAREN, '('], |
| 54 | + [49, TokenType::T_STRING, '"fox"', 'fox'], |
| 55 | + [55, TokenType::T_COALESCE, '??'], |
| 56 | + [58, TokenType::T_FALSE, 'false', false], |
| 57 | + [63, TokenType::T_CLOSE_PAREN, ')'] |
| 58 | + ]; |
| 59 | + } |
56 | 60 |
|
57 |
| - $tokens->next(); |
58 |
| - $token = $tokens->current(); |
59 |
| - $this->assertSame(TokenType::T_CLOSE_BRACKET, $token->type); |
| 61 | + /** |
| 62 | + * @covers ::token |
| 63 | + * @dataProvider tokenProvider |
| 64 | + */ |
| 65 | + public function testToken( |
| 66 | + int $offset, |
| 67 | + TokenType $type, |
| 68 | + string $lexeme, |
| 69 | + mixed $literal = null |
| 70 | + ): void { |
| 71 | + $string = static::stringProvider(); |
| 72 | + $token = Tokenizer::token($string, $offset); |
| 73 | + $this->assertSame($type, $token->type); |
| 74 | + $this->assertSame($lexeme, $token->lexeme); |
| 75 | + $this->assertSame($literal, $token->literal); |
| 76 | + } |
60 | 77 |
|
61 |
| - $tokens->next(); |
62 |
| - $token = $tokens->current(); |
63 |
| - $this->assertSame(TokenType::T_CLOSE_PAREN, $token->type); |
| 78 | + /** |
| 79 | + * @covers ::__construct |
| 80 | + * @covers ::tokens |
| 81 | + */ |
| 82 | + public function testTokens() |
| 83 | + { |
| 84 | + $string = static::stringProvider(); |
| 85 | + $tokenizer = new Tokenizer($string); |
| 86 | + $tokens = $tokenizer->tokens(); |
64 | 87 |
|
65 |
| - $tokens->next(); |
66 |
| - $token = $tokens->current(); |
67 |
| - $this->assertSame(TokenType::T_DOT, $token->type); |
| 88 | + foreach (static::tokenProvider() as $expected) { |
| 89 | + if ($expected[1] === TokenType::T_WHITESPACE) { |
| 90 | + continue; |
| 91 | + } |
68 | 92 |
|
69 |
| - $tokens->next(); |
70 |
| - $token = $tokens->current(); |
71 |
| - $this->assertSame(TokenType::T_IDENTIFIER, $token->type); |
72 |
| - $this->assertSame('drink', $token->lexeme); |
| 93 | + $token = $tokens->current(); |
| 94 | + $this->assertSame($expected[1], $token->type); |
| 95 | + $this->assertSame($expected[2], $token->lexeme); |
| 96 | + $this->assertSame($expected[3] ?? null, $token->literal); |
| 97 | + $tokens->next(); |
| 98 | + } |
73 | 99 |
|
74 |
| - $tokens->next(); |
75 | 100 | $token = $tokens->current();
|
76 | 101 | $this->assertSame(TokenType::T_EOF, $token->type);
|
77 | 102 | }
|
78 |
| - |
79 | 103 | }
|
0 commit comments