Passed
Pull Request — master (#311)
by William
12:43
created

IsMethodsTest::testisSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace PhpMyAdmin\SqlParser\Tests\Lexer;
4
5
use PhpMyAdmin\SqlParser\Context;
6
use PhpMyAdmin\SqlParser\Tests\TestCase;
7
use PhpMyAdmin\SqlParser\Token;
8
9
class IsMethodsTest extends TestCase
10
{
11
    public function testIsKeyword()
12
    {
13
        $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('SELECT'));
14
        $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('ALL'));
15
        $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('DISTINCT'));
16
17
        $this->assertEquals(
18
            1 | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED | Token::FLAG_KEYWORD_KEY,
19
            Context::isKeyword('PRIMARY KEY')
20
        );
21
        $this->assertEquals(
22
            1 | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED,
23
            Context::isKeyword('CHARACTER SET')
24
        );
25
26
        $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('FROM', true));
27
        $this->assertNull(Context::isKeyword('MODIFY', true));
28
29
        $this->assertNull(Context::isKeyword('foo'));
30
        $this->assertNull(Context::isKeyword('bar baz'));
31
    }
32
33
    public function testIsOperator()
34
    {
35
        $this->assertEquals(Token::FLAG_OPERATOR_ARITHMETIC, Context::isOperator('%'));
36
        $this->assertEquals(Token::FLAG_OPERATOR_LOGICAL, Context::isOperator('!'));
37
        $this->assertEquals(Token::FLAG_OPERATOR_LOGICAL, Context::isOperator('&&'));
38
        $this->assertEquals(Token::FLAG_OPERATOR_LOGICAL, Context::isOperator('<=>'));
39
        $this->assertEquals(Token::FLAG_OPERATOR_BITWISE, Context::isOperator('&'));
40
        $this->assertEquals(Token::FLAG_OPERATOR_ASSIGNMENT, Context::isOperator(':='));
41
        $this->assertEquals(Token::FLAG_OPERATOR_SQL, Context::isOperator(','));
42
43
        $this->assertNull(Context::isOperator('a'));
44
    }
45
46
    public function testIsWhitespace()
47
    {
48
        $this->assertTrue(Context::isWhitespace(' '));
49
        $this->assertTrue(Context::isWhitespace("\r"));
50
        $this->assertTrue(Context::isWhitespace("\n"));
51
        $this->assertTrue(Context::isWhitespace("\t"));
52
53
        $this->assertFalse(Context::isWhitespace('a'));
54
        $this->assertFalse(Context::isWhitespace("\b"));
55
        $this->assertFalse(Context::isWhitespace("\u1000"));
56
    }
57
58
    public function testIsComment()
59
    {
60
        $this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('#'));
61
        $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*'));
62
        $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('*/'));
63
        $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- '));
64
        $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\t"));
65
        $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\n"));
66
67
        $this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('# a comment'));
68
        $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*comment */'));
69
        $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- my comment'));
70
71
        $this->assertNull(Context::isComment(''));
72
        $this->assertNull(Context::isComment('--not a comment'));
73
    }
74
75
    public function testIsBool()
76
    {
77
        $this->assertTrue(Context::isBool('true'));
78
        $this->assertTrue(Context::isBool('false'));
79
80
        $this->assertFalse(Context::isBool('tru'));
81
        $this->assertFalse(Context::isBool('falsee'));
82
    }
83
84
    public function testIsNumber()
85
    {
86
        $this->assertTrue(Context::isNumber('+'));
87
        $this->assertTrue(Context::isNumber('-'));
88
        $this->assertTrue(Context::isNumber('.'));
89
        $this->assertTrue(Context::isNumber('0'));
90
        $this->assertTrue(Context::isNumber('1'));
91
        $this->assertTrue(Context::isNumber('2'));
92
        $this->assertTrue(Context::isNumber('3'));
93
        $this->assertTrue(Context::isNumber('4'));
94
        $this->assertTrue(Context::isNumber('5'));
95
        $this->assertTrue(Context::isNumber('6'));
96
        $this->assertTrue(Context::isNumber('7'));
97
        $this->assertTrue(Context::isNumber('8'));
98
        $this->assertTrue(Context::isNumber('9'));
99
        $this->assertTrue(Context::isNumber('e'));
100
        $this->assertTrue(Context::isNumber('E'));
101
    }
102
103
    public function testIsString()
104
    {
105
        $this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'"));
106
        $this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"'));
107
108
        $this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'foo bar'"));
109
        $this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"foo bar"'));
110
111
        $this->assertNull(Context::isString(''));
112
        $this->assertNull(Context::isString('foo bar'));
113
    }
114
115
    public function testIsSymbol()
116
    {
117
        $this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@'));
118
        $this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`'));
119
120
        $this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@id'));
121
        $this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`id`'));
122
123
        $this->assertNull(Context::isSymbol(''));
124
        $this->assertNull(Context::isSymbol('id'));
125
    }
126
127
    public function testisSeparator()
128
    {
129
        $this->assertTrue(Context::isSeparator('+'));
130
        $this->assertTrue(Context::isSeparator('.'));
131
132
        $this->assertFalse(Context::isSeparator('1'));
133
        $this->assertFalse(Context::isSeparator('E'));
134
        $this->assertFalse(Context::isSeparator('_'));
135
    }
136
}
137