-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_lexer.py
74 lines (65 loc) · 3.15 KB
/
test_lexer.py
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
import unittest
from lexer import Lexer, TokenType, Token
class TestLexer(unittest.TestCase):
def test_tokenize_keywords(self):
lexer = Lexer('SELECT * FROM my_table WHERE id = 1')
tokens = lexer.tokenize()
expected_tokens = [
Token(TokenType.KEYWORD, 'SELECT'),
Token(TokenType.OPERATOR, '*'),
Token(TokenType.KEYWORD, 'FROM'),
Token(TokenType.IDENTIFIER, 'my_table'),
Token(TokenType.KEYWORD, 'WHERE'),
Token(TokenType.IDENTIFIER, 'id'),
Token(TokenType.OPERATOR, '='),
Token(TokenType.LITERAL, '1')
]
self.assertEqual([token.type for token in tokens], [token.type for token in expected_tokens])
self.assertEqual([token.value for token in tokens], [token.value for token in expected_tokens])
def test_tokenize_identifiers(self):
lexer = Lexer('SELECT column1, column2 FROM my_table')
tokens = lexer.tokenize()
expected_tokens = [
Token(TokenType.KEYWORD, 'SELECT'),
Token(TokenType.IDENTIFIER, 'column1'),
Token(TokenType.OPERATOR, ','),
Token(TokenType.IDENTIFIER, 'column2'),
Token(TokenType.KEYWORD, 'FROM'),
Token(TokenType.IDENTIFIER, 'my_table')
]
self.assertEqual([token.type for token in tokens], [token.type for token in expected_tokens])
self.assertEqual([token.value for token in tokens], [token.value for token in expected_tokens])
def test_tokenize_literals(self):
lexer = Lexer('SELECT * FROM my_table WHERE id = 1.23')
tokens = lexer.tokenize()
expected_tokens = [
Token(TokenType.KEYWORD, 'SELECT'),
Token(TokenType.OPERATOR, '*'),
Token(TokenType.KEYWORD, 'FROM'),
Token(TokenType.IDENTIFIER, 'my_table'),
Token(TokenType.KEYWORD, 'WHERE'),
Token(TokenType.IDENTIFIER, 'id'),
Token(TokenType.OPERATOR, '='),
Token(TokenType.LITERAL, '1.23')
]
self.assertEqual([token.type for token in tokens], [token.type for token in expected_tokens])
self.assertEqual([token.value for token in tokens], [token.value for token in expected_tokens])
def test_tokenize_operators(self):
lexer = Lexer('SELECT column1 + column2 FROM my_table')
tokens = lexer.tokenize()
expected_tokens = [
Token(TokenType.KEYWORD, 'SELECT'),
Token(TokenType.IDENTIFIER, 'column1'),
Token(TokenType.OPERATOR, '+'),
Token(TokenType.IDENTIFIER, 'column2'),
Token(TokenType.KEYWORD, 'FROM'),
Token(TokenType.IDENTIFIER, 'my_table')
]
self.assertEqual([token.type for token in tokens], [token.type for token in expected_tokens])
self.assertEqual([token.value for token in tokens], [token.value for token in expected_tokens])
def test_tokenize_invalid_character(self):
lexer = Lexer('SELECT column1 + column2 FROM my_table WHERE id = 1 AND name = "John"!')
with self.assertRaises(Exception):
tokens = lexer.tokenize()
if __name__ == '__main__':
unittest.main()