File: TokenTest.php

package info (click to toggle)
phpmyadmin-sql-parser 5.10.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,244 kB
  • sloc: php: 52,958; makefile: 13; sh: 8
file content (92 lines) | stat: -rw-r--r-- 3,115 bytes parent folder | download | duplicates (3)
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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\SqlParser\Tests\Lexer;

use PhpMyAdmin\SqlParser\Tests\TestCase;
use PhpMyAdmin\SqlParser\Token;

class TokenTest extends TestCase
{
    public function testExtractKeyword(): void
    {
        $tok = new Token('SelecT', Token::TYPE_KEYWORD, Token::FLAG_KEYWORD_RESERVED);
        $this->assertEquals($tok->value, 'SELECT');

        $tok = new Token('aS', Token::TYPE_KEYWORD, Token::FLAG_KEYWORD_RESERVED);
        $this->assertEquals($tok->value, 'AS');
    }

    public function testExtractWhitespace(): void
    {
        $tok = new Token(" \t \r \n ", Token::TYPE_WHITESPACE);
        $this->assertEquals($tok->value, ' ');
    }

    public function testExtractBool(): void
    {
        $tok = new Token('false', Token::TYPE_BOOL);
        $this->assertFalse($tok->value);

        $tok = new Token('True', Token::TYPE_BOOL);
        $this->assertTrue($tok->value);
    }

    public function testExtractNumber(): void
    {
        $tok = new Token('--42', Token::TYPE_NUMBER, Token::FLAG_NUMBER_NEGATIVE);
        $this->assertEquals($tok->value, 42);

        $tok = new Token('---42', Token::TYPE_NUMBER, Token::FLAG_NUMBER_NEGATIVE);
        $this->assertEquals($tok->value, -42);

        $tok = new Token('0xFE', Token::TYPE_NUMBER, Token::FLAG_NUMBER_HEX);
        $this->assertEquals($tok->value, 0xFE);

        $tok = new Token('-0xEF', Token::TYPE_NUMBER, Token::FLAG_NUMBER_NEGATIVE | Token::FLAG_NUMBER_HEX);
        $this->assertEquals($tok->value, -0xEF);

        $tok = new Token('3.14', Token::TYPE_NUMBER, Token::FLAG_NUMBER_FLOAT);
        $this->assertEquals($tok->value, 3.14);
    }

    public function testExtractString(): void
    {
        $tok = new Token('"foo bar "', Token::TYPE_STRING);
        $this->assertEquals($tok->value, 'foo bar ');

        $tok = new Token("' bar foo '", Token::TYPE_STRING);
        $this->assertEquals($tok->value, ' bar foo ');

        $tok = new Token("'\''", Token::TYPE_STRING);
        $this->assertEquals($tok->value, '\'');

        $tok = new Token('"\c\d\e\f\g\h\i\j\k\l\m\p\q\s\u\v\w\x\y\z"', Token::TYPE_STRING);
        $this->assertEquals($tok->value, 'cdefghijklmpqsuvwxyz');
    }

    public function testExtractSymbol(): void
    {
        $tok = new Token('@foo', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_VARIABLE);
        $this->assertEquals($tok->value, 'foo');

        $tok = new Token('`foo`', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_BACKTICK);
        $this->assertEquals($tok->value, 'foo');

        $tok = new Token('@`foo`', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_VARIABLE);
        $this->assertEquals($tok->value, 'foo');

        $tok = new Token(':foo', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_PARAMETER);
        $this->assertEquals($tok->value, 'foo');

        $tok = new Token('?', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_PARAMETER);
        $this->assertEquals($tok->value, '?');
    }

    public function testInlineToken(): void
    {
        $token = new Token(" \r \n \t ");
        $this->assertEquals($token->getInlineToken(), ' \r \n \t ');
    }
}