Debian Codegraph @

tests/test_mysql.py

raw
1 """
2 Pygments MySQL lexer tests
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
6 :license: BSD, see LICENSE for details.
7 """
8
9 import pytest
10
11 from pygments.lexers.sql import MySqlLexer
12
13 from pygments.token import Comment, Keyword, Literal, Name, Number, Operator, \
14 Punctuation, String, Whitespace
15
16
17 @pytest.fixture(scope='module')
18 def lexer():
19 yield MySqlLexer()
20
21
22 @pytest.mark.parametrize('text', ('1', '22', '22 333', '22 a', '22+', '22)',
23 '22\n333', '22\r\n333'))
24 def test_integer_literals_positive_match(lexer, text):
25 """Validate that integer literals are tokenized as integers."""
26 token = list(lexer.get_tokens(text))[0]
27 assert token[0] == Number.Integer
28 assert token[1] in {'1', '22'}
29
30
31 @pytest.mark.parametrize('text', ('1a', '1A', '1.', '1ひ', '1$', '1_',
32 '1\u0080', '1\uffff'))
33 def test_integer_literals_negative_match(lexer, text):
34 """Validate that non-integer texts are not matched as integers."""
35 assert list(lexer.get_tokens(text))[0][0] != Number.Integer
36
37
38 @pytest.mark.parametrize(
39 'text',
40 (
41 '.123', '1.23', '123.',
42 '1e10', '1.0e10', '1.e-10', '.1e+10',
43 ),
44 )
45 def test_float_literals(lexer, text):
46 assert list(lexer.get_tokens(text))[0] == (Number.Float, text)
47
48
49 @pytest.mark.parametrize('text', ("X'0af019'", "x'0AF019'", "0xaf019"))
50 def test_hexadecimal_literals(lexer, text):
51 assert list(lexer.get_tokens(text))[0] == (Number.Hex, text)
52
53
54 @pytest.mark.parametrize('text', ("B'010'", "b'010'", "0b010"))
55 def test_binary_literals(lexer, text):
56 assert list(lexer.get_tokens(text))[0] == (Number.Bin, text)
57
58
59 @pytest.mark.parametrize(
60 'text',
61 (
62 "{d'2020-01-01'}", "{ d ' 2020^01@01 ' }",
63 "{t'8 9:10:11'}", "{ t ' 09:10:11.12 ' }", "{ t ' 091011 ' }",
64 '{ts"2020-01-01 09:10:11"}', "{ ts ' 2020@01/01 09:10:11 ' }",
65 ),
66 )
67 def test_temporal_literals(lexer, text):
68 assert list(lexer.get_tokens(text))[0] == (Literal.Date, text)
69
70
71 @pytest.mark.parametrize(
72 'text, expected_types',
73 (
74 (r"'a'", (String.Single,) * 3),
75 (r"""'""'""", (String.Single,) * 3),
76 (r"''''", (String.Single, String.Escape, String.Single)),
77 (r"'\''", (String.Single, String.Escape, String.Single)),
78 (r'"a"', (String.Double,) * 3),
79 (r'''"''"''', (String.Double,) * 3),
80 (r'""""', (String.Double, String.Escape, String.Double)),
81 (r'"\""', (String.Double, String.Escape, String.Double)),
82 ),
83 )
84 def test_string_literals(lexer, text, expected_types):
85 tokens = list(lexer.get_tokens(text))[:len(expected_types)]
86 assert all(t[0] == e for t, e in zip(tokens, expected_types))
87
88
89 @pytest.mark.parametrize(
90 'text',
91 (
92 "@a", "@1", "@._.$",
93 "@'?'", """@'abc''def"`ghi'""",
94 '@"#"', '''@"abc""def'`ghi"''',
95 '@`^`', """@`abc``def'"ghi`""",
96 "@@timestamp",
97 "@@session.auto_increment_offset",
98 "@@global.auto_increment_offset",
99 "@@persist.auto_increment_offset",
100 "@@persist_only.auto_increment_offset",
101 '?',
102 ),
103 )
104 def test_variables(lexer, text):
105 tokens = list(lexer.get_tokens(text))
106 assert all(t[0] == Name.Variable for t in tokens[:-1])
107 assert ''.join([t[1] for t in tokens]).strip() == text.strip()
108
109
110 @pytest.mark.parametrize('text', ('true', 'false', 'null', 'unknown'))
111 def test_constants(lexer, text):
112 assert list(lexer.get_tokens(text))[0] == (Name.Constant, text)
113
114
115 @pytest.mark.parametrize('text', ('-- abc', '--\tabc', '#abc'))
116 def test_comments_single_line(lexer, text):
117 # Test the standalone comment.
118 tokens = list(lexer.get_tokens(text))
119 assert tokens[0] == (Comment.Single, text)
120
121 # Test the comment with mixed tokens.
122 tokens = list(lexer.get_tokens('select' + text + '\nselect'))
123 assert tokens[0] == (Keyword, 'select')
124 assert tokens[1] == (Comment.Single, text)
125 assert tokens[-2] == (Keyword, 'select')
126
127
128 @pytest.mark.parametrize(
129 'text',
130 (
131 '/**/a', '/*a*b/c*/a', '/*\nabc\n*/a',
132 '/* /* */a'
133 )
134 )
135 def test_comments_multi_line(lexer, text):
136 tokens = list(lexer.get_tokens(text))
137 assert all(token[0] == Comment.Multiline for token in tokens[:-2])
138 assert ''.join(token[1] for token in tokens).strip() == text.strip()
139
140 # Validate nested comments are not supported.
141 assert tokens[-2][0] != Comment.Multiline
142
143
144 @pytest.mark.parametrize(
145 'text', ('BKA', 'SEMIJOIN'))
146 def test_optimizer_hints(lexer, text):
147 good = '/*+ ' + text + '(), */'
148 ignore = '/* ' + text + ' */'
149 bad1 = '/*+ a' + text + '() */'
150 bad2 = '/*+ ' + text + 'a */'
151 assert (Comment.Preproc, text) in lexer.get_tokens(good)
152 assert (Comment.Preproc, text) not in lexer.get_tokens(ignore)
153 assert (Comment.Preproc, text) not in lexer.get_tokens(bad1)
154 assert (Comment.Preproc, text) not in lexer.get_tokens(bad2)
155
156
157 @pytest.mark.parametrize(
158 'text, expected_types',
159 (
160 # SET exceptions
161 ('SET', (Keyword,)),
162 ('SET abc = 1;', (Keyword,)),
163 ('SET @abc = 1;', (Keyword,)),
164 ('CHARACTER SET latin1', (Keyword, Whitespace, Keyword,)),
165 ('SET("r", "g", "b")', (Keyword.Type, Punctuation)),
166 ('SET ("r", "g", "b")', (Keyword.Type, Whitespace, Punctuation)),
167 ),
168 )
169 def test_exceptions(lexer, text, expected_types):
170 tokens = list(lexer.get_tokens(text))[:len(expected_types)]
171 assert all(t[0] == e for t, e in zip(tokens, expected_types))
172
173
174 @pytest.mark.parametrize(
175 'text',
176 (
177 'SHOW', 'CREATE', 'ALTER', 'DROP',
178 'SELECT', 'INSERT', 'UPDATE', 'DELETE',
179 'WHERE', 'GROUP', 'ORDER', 'BY', 'AS',
180 'DISTINCT', 'JOIN', 'WITH', 'RECURSIVE',
181 'PARTITION', 'NTILE', 'MASTER_PASSWORD', 'XA',
182 'REQUIRE_TABLE_PRIMARY_KEY_CHECK', 'STREAM',
183 ),
184 )
185 def test_keywords(lexer, text):
186 assert list(lexer.get_tokens(text))[0] == (Keyword, text)
187
188
189 @pytest.mark.parametrize(
190 'text',
191 (
192 # Standard
193 'INT(', 'VARCHAR(', 'ENUM(', 'DATETIME', 'GEOMETRY', 'POINT', 'JSON',
194 # Aliases and compatibility
195 'FIXED', 'MEDIUMINT', 'INT3', 'REAL', 'SERIAL',
196 'LONG', 'NATIONAL', 'PRECISION', 'VARYING',
197 ),
198 )
199 def test_data_types(lexer, text):
200 assert list(lexer.get_tokens(text))[0] == (Keyword.Type, text.strip('('))
201
202
203 @pytest.mark.parametrize(
204 'text',
205 (
206 # Common
207 'CAST', 'CONCAT_WS', 'DAYNAME', 'IFNULL', 'NOW', 'SUBSTR',
208 # Less common
209 'CAN_ACCESS_COLUMN', 'JSON_CONTAINS_PATH', 'ST_GEOMFROMGEOJSON',
210 ),
211 )
212 def test_functions(lexer, text):
213 assert list(lexer.get_tokens(text + '('))[0] == (Name.Function, text)
214 assert list(lexer.get_tokens(text + ' ('))[0] == (Name.Function, text)
215
216
217 @pytest.mark.parametrize(
218 'text',
219 (
220 'abc_$123', '上市年限', 'ひらがな', '123_$abc', '123ひらがな',
221 ),
222 )
223 def test_schema_object_names_unquoted(lexer, text):
224 tokens = list(lexer.get_tokens(text))[:-1]
225 assert all(token[0] == Name for token in tokens)
226 assert ''.join(token[1] for token in tokens) == text
227
228
229 @pytest.mark.parametrize(
230 'text',
231 (
232 '`a`', '`1`', '`上市年限`', '`ひらがな`', '`select`', '`concat(`',
233 '`-- `', '`/*`', '`#`',
234 ),
235 )
236 def test_schema_object_names_quoted(lexer, text):
237 tokens = list(lexer.get_tokens(text))[:-1]
238 assert tokens[0] == (Name.Quoted, '`')
239 assert tokens[1] == (Name.Quoted, text[1:-1])
240 assert tokens[2] == (Name.Quoted, '`')
241 assert ''.join(token[1] for token in tokens) == text
242
243
244 @pytest.mark.parametrize('text', ('````', ))
245 def test_schema_object_names_quoted_escaped(lexer, text):
246 """Test quoted schema object names with escape sequences."""
247 tokens = list(lexer.get_tokens(text))[:-1]
248 assert tokens[0] == (Name.Quoted, '`')
249 assert tokens[1] == (Name.Quoted.Escape, text[1:-1])
250 assert tokens[2] == (Name.Quoted, '`')
251 assert ''.join(token[1] for token in tokens) == text
252
253
254 @pytest.mark.parametrize(
255 'text',
256 ('+', '*', '/', '%', '&&', ':=', '!', '<', '->>', '^', '|', '~'),
257 )
258 def test_operators(lexer, text):
259 assert list(lexer.get_tokens(text))[0] == (Operator, text)
260
261
262 @pytest.mark.parametrize(
263 'text, expected_types',
264 (
265 ('abc.efg', (Name, Punctuation, Name)),
266 ('abc,efg', (Name, Punctuation, Name)),
267 ('MAX(abc)', (Name.Function, Punctuation, Name, Punctuation)),
268 ('efg;', (Name, Punctuation)),
269 ),
270 )
271 def test_punctuation(lexer, text, expected_types):
272 tokens = list(lexer.get_tokens(text))[:len(expected_types)]
273 assert all(t[0] == e for t, e in zip(tokens, expected_types))