File: test_lexer.py

package info (click to toggle)
python-pyhcl 0.4.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 664 kB
  • sloc: python: 3,833; makefile: 25; sh: 6
file content (551 lines) | stat: -rw-r--r-- 13,893 bytes parent folder | download | duplicates (2)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# -*- coding: UTF-8 -*-
#
# These tests are taken from hcl/lex_test.go
#

from __future__ import print_function, unicode_literals

from os.path import join, dirname
import hcl.lexer

import pytest

class Error:
    pass

LEX_FIXTURE_DIR = join(dirname(__file__), 'lex-fixtures')
LEX_FIXTURES = [
    (
        "array_comment.hcl",
        ["IDENTIFIER", "EQUAL", "LEFTBRACKET",
        "STRING", "COMMA",
        "STRING", "COMMA",
        "RIGHTBRACKET", None]
    ),
    (
        "comment.hcl",
        ["IDENTIFIER", "EQUAL", "STRING", None]
    ),
    (
        "multiple.hcl",
        [
            "IDENTIFIER", "EQUAL", "STRING",
            "IDENTIFIER", "EQUAL", "NUMBER",
            None,
        ],
    ),
    (
        "list.hcl",
        [
            "IDENTIFIER", "EQUAL", "LEFTBRACKET",
            "NUMBER", "COMMA", "NUMBER", "COMMA", "STRING",
            "RIGHTBRACKET", None,
        ],
    ),
    (
        "list_comma.hcl",
        [
            "IDENTIFIER", "EQUAL", "LEFTBRACKET",
            "NUMBER", "COMMA", "NUMBER", "COMMA", "STRING",
            "COMMA", "RIGHTBRACKET", None,
        ],
    ),
    (
        "list_of_maps.hcl",
        [
            "IDENTIFIER", "EQUAL", "LEFTBRACKET",
            "LEFTBRACE", "IDENTIFIER", "EQUAL", "STRING", "RIGHTBRACE",
            "COMMA",
            "LEFTBRACE", "IDENTIFIER", "EQUAL", "STRING", "COMMA",
            "IDENTIFIER", "EQUAL", "STRING", "RIGHTBRACE", "COMMA",
            "RIGHTBRACKET", None,
        ],
    ),
    (
        "structure_basic.hcl",
        [
            "IDENTIFIER", "LEFTBRACE",
            "IDENTIFIER", "EQUAL", "NUMBER",
            "STRING", "EQUAL", "NUMBER",
            "STRING", "EQUAL", "NUMBER",
            "RIGHTBRACE", None,
        ],
    ),
    (
        "structure.hcl",
        [
            "IDENTIFIER", "IDENTIFIER", "STRING", "LEFTBRACE",
            "IDENTIFIER", "EQUAL", "NUMBER",
            "IDENTIFIER", "EQUAL", "STRING",
            "RIGHTBRACE", None,
        ],
    ),
    (
        "windows_heredoc.hcl",
        [
            "IDENTIFIER", "EQUAL", "STRING", None,
        ],
    ),
    (
        "empty_heredoc.hcl",
        [
            "IDENTIFIER", "EQUAL", "STRING", None,
        ],
    ),
    (
        "heredoc_terminator_same_line.hcl",
        [
            "IDENTIFIER", "EQUAL", "STRING", None,
        ],
    ),
    (
        "unterminated_block_comment.hcl",
        ["DIVIDE", "MULTIPLY", "IDENTIFIER"],
    ),
    (
        "nested_comment.hcl",
        ["MULTIPLY", "DIVIDE", None],
    ),
    (
        "/not a comment",
        ["DIVIDE", "IDENTIFIER", "IDENTIFIER", "IDENTIFIER", None],
    ),
]

# The first value in the tuple can be either the file that will be read or just
# a string
LEX_ERROR_FIXTURES = [
    (
        "old.hcl",
        [
            "IDENTIFIER", "EQUAL", "LEFTBRACE", "STRING", "COLON", "STRING"
        ],
        "Line 2, column 15, index 27: Illegal character ':'"
    ),
    (
        "a = <HERE\n"
        "foobar\n"
        "HERE",
        ["IDENTIFIER", "EQUAL", "LT", "IDENTIFIER"],
        "Line 1, column 4, index 4: Heredoc must start with '<<', got '<H'"
    ),
    (
        "a = <<HE RE\n"
        "foobar\n"
        "HERE",
        ["IDENTIFIER", "EQUAL", Error],
        "Line 1, column 4, index 4: Heredoc must have a marker, e.g. '<<FOO'"
    ),
    (
        "a = <<HERE\n"
        "foobar\n",
        ["IDENTIFIER", "EQUAL", Error],
        "Line 3, column 0, index 18: EOF before closing heredoc"
    ),
    (
        'a = "foo',
        ["IDENTIFIER", "EQUAL", Error],
        "Line 1, column 8, index 8: EOF before closing string quote"
    ),
    (
        'a = "${foo"',
        ["IDENTIFIER", "EQUAL", Error],
        "Line 1, column 11, index 11: EOF before closing '${}' expression"
    ),

]

@pytest.mark.parametrize("hcl_fname,tokens", LEX_FIXTURES)
def test_lexer(hcl_fname, tokens):

    if hcl_fname.endswith('.hcl'):
        with open(join(LEX_FIXTURE_DIR, hcl_fname), 'r') as fp:
            input = fp.read()
    else:
        input = hcl_fname

    print(input)

    lexer = hcl.lexer.Lexer()
    lexer.input(input)

    for expectedToken in tokens:
        lex_tok = lexer.token()

        if lex_tok is None:
            assert expectedToken is None
        else:
            assert expectedToken == lex_tok.type
        print(lex_tok)

    assert lexer.token() is None

@pytest.mark.parametrize("hcl_fname,tokens,error_loc", LEX_ERROR_FIXTURES)
def test_lexer_errors(hcl_fname, tokens, error_loc):

    if hcl_fname.endswith('.hcl'):
        with open(join(LEX_FIXTURE_DIR, hcl_fname), 'r') as fp:
            input = fp.read()
    else:
        input = hcl_fname

    print(input)

    lexer = hcl.lexer.Lexer()
    lexer.input(input)

    for expectedToken in tokens:
        try:
            lex_tok = lexer.token()
        except ValueError as e:
            assert expectedToken is Error
            assert error_loc == str(e)
            return

        if lex_tok is None:
            assert expectedToken is None
        else:
            assert expectedToken == lex_tok.type
        print(lex_tok)

f100 = 'f' * 100

TOKEN_FIXTURES = [
    # Comments
    (None, "//"),
    (None, "////"),
    (None, "// comment"),
    (None, "// /* comment */"),
    (None, "// // comment //"),
    (None, "//" + f100),
    (None, "#"),
    (None, "##"),
    (None, "# comment"),
    (None, "# /* comment */"),
    (None, "# # comment #"),
    (None, "#" + f100),
    (None, "/**/"),
    (None, "/***/"),
    (None, "/* comment */"),
    (None, "/* // comment */"),
    (None, "/* /* comment */"),
    (None, "/*\n comment\n*/"),
    (None, "/*" + f100 + "*/"),

    # Operators
    ("LEFTPAREN", "("),
    ("LEFTBRACKET", "["),
    ("LEFTBRACE", "{"),
    ("COMMA", ","),
    ("PERIOD", "."),
    ("RIGHTPAREN", ")"),
    ("RIGHTBRACKET", "]"),
    ("RIGHTBRACE", "}"),
    ("EQUAL", "="),
    ("ADD", "+"),
    ("MINUS", "-"),
    ("MULTIPLY", "*"),
    ("DIVIDE", "/"),
    ("QMARK", "?"),
    ("COLON", ":"),
    ("GT", ">"),
    ("LT", "<"),
    ("EQ", "=="),
    ("NE", "!="),
    ("LE", "<="),
    ("GE", ">="),
    ("ASTERISK_PERIOD", "*."),

    # Bools
    ("BOOL", "true"),
    ("BOOL", "false"),

    # Identifier
    ("IDENTIFIER", "a"),
    ("IDENTIFIER", "a0"),
    ("IDENTIFIER", "foobar"),
    ("IDENTIFIER", "foo-bar"),
    ("IDENTIFIER", "abc123"),
    ("IDENTIFIER", "LGTM"),
    ("IDENTIFIER", "_"),
    ("IDENTIFIER", "_abc123"),
    ("IDENTIFIER", "abc123_"),
    ("IDENTIFIER", "_abc_123_"),
    ("IDENTIFIER", "_äöü"),
    ("IDENTIFIER", "_本"),
    ("IDENTIFIER", "äöü"),
    ("IDENTIFIER", "本"),
    ("IDENTIFIER", "a۰۱۸"),
    ("IDENTIFIER", "foo६४"),
    ("IDENTIFIER", "bar9876"),

    # Heredoc
    ("STRING", "<<EOF\nhello\nworld\nEOF"),
    ("STRING", "<<EOF\nhello world\nEOF"),

    # Strings
    ("STRING", '" "'),
    ("STRING", '"a"'),
    ("STRING", '"本"'),
    ("STRING", '"{f}"'),
    ("STRING", '"${file("foo")}"'),
    ("STRING", r'"${file(\"foo\")}"'),
    ("STRING", r'"\a"'),
    ("STRING", r'"\b"'),
    ("STRING", r'"\f"'),
    ("STRING", r'"\n"'),
    ("STRING", r'"\r"'),
    ("STRING", r'"\t"'),
    ("STRING", r'"\v"'),
    ("STRING", r'"\""'),
    ("STRING", r'"\000"'),
    ("STRING", r'"\777"'),
    ("STRING", r'"\x00"'),
    ("STRING", r'"\xff"'),
    ("STRING", r'"\u0000"'),
    ("STRING", r'"\ufA16"'),
    ("STRING", r'"\U00000000"'),
    ("STRING", r'"\U0000ffAB"'),
    ("STRING", '"' + f100 + '"'),

    # Numbers
    ("NUMBER", "0"),
    ("NUMBER", "1"),
    ("NUMBER", "9"),
    ("NUMBER", "42"),
    ("NUMBER", "1234567890"),
    ("NUMBER", "00"),
    ("NUMBER", "01"),
    ("NUMBER", "07"),
    ("NUMBER", "042"),
    ("NUMBER", "01234567"),
    ("NUMBER", "0x0"),
    ("NUMBER", "0x1"),
    ("NUMBER", "0xf"),
    ("NUMBER", "0x42"),
    ("NUMBER", "0x123456789abcDEF"),
    ("NUMBER", "0x" + f100),
    ("NUMBER", "0X0"),
    ("NUMBER", "0X1"),
    ("NUMBER", "0XF"),
    ("NUMBER", "0X42"),
    ("NUMBER", "0X123456789abcDEF"),
    ("NUMBER", "0X" + f100),
    ("NUMBER", "-0"),
    ("NUMBER", "-1"),
    ("NUMBER", "-9"),
    ("NUMBER", "-42"),
    ("NUMBER", "-1234567890"),
    ("NUMBER", "-00"),
    ("NUMBER", "-01"),
    ("NUMBER", "-07"),
    ("NUMBER", "-29"),
    ("NUMBER", "-042"),
    ("NUMBER", "-01234567"),
    ("NUMBER", "-0x0"),
    ("NUMBER", "-0x1"),
    ("NUMBER", "-0xf"),
    ("NUMBER", "-0x42"),
    ("NUMBER", "-0x123456789abcDEF"),
    ("NUMBER", "-0x" + f100),
    ("NUMBER", "-0X0"),
    ("NUMBER", "-0X1"),
    ("NUMBER", "-0XF"),
    ("NUMBER", "-0X42"),
    ("NUMBER", "-0X123456789abcDEF"),
    ("NUMBER", "-0X" + f100),

    # Floats
    ("FLOAT", "0."),
    ("FLOAT", "1."),
    ("FLOAT", "42."),
    ("FLOAT", "01234567890."),
    ("FLOAT", ".0"),
    ("FLOAT", ".1"),
    ("FLOAT", ".42"),
    ("FLOAT", ".0123456789"),
    ("FLOAT", "0.0"),
    ("FLOAT", "1.0"),
    ("FLOAT", "42.0"),
    ("FLOAT", "01234567890.0"),
    ("FLOAT", "-0.0"),
    ("FLOAT", "-1.0"),
    ("FLOAT", "-42.0"),
    ("FLOAT", "-01234567890.0"),

]

@pytest.mark.parametrize("token,input_string", TOKEN_FIXTURES)
def test_tokens(token, input_string):

    print(input_string)

    lexer = hcl.lexer.Lexer()
    lexer.input(input_string)

    lex_tok = lexer.token()

    if lex_tok is None:
        assert token is None
    else:
        assert token == lex_tok.type
        assert lexer.token() is None

def test_export_comments_wrong_parameter():
    with pytest.raises(ValueError):
        lexer = hcl.lexer.Lexer(export_comments="WRONG")

ONE_LINE_COMMENT_FIXTURES = [
    ("COMMENT", "//"),
    ("COMMENT", "////"),
    ("COMMENT", "// comment"),
    ("COMMENT", "// /* comment */"),
    ("COMMENT", "// // comment //"),
    ("COMMENT", "//" + f100),
    ("COMMENT", "#"),
    ("COMMENT", "##"),
    ("COMMENT", "# comment"),
    ("COMMENT", "# /* comment */"),
    ("COMMENT", "# # comment #"),
    ("COMMENT", "#" + f100),
    (None, "/**/"),
    (None, "/***/"),
    (None, "/* comment */"),
    (None, "/* // comment */"),
    (None, "/* /* comment */"),
    (None, "/*\n comment\n*/"),
    (None, "/*" + f100 + "*/")
]

@pytest.mark.parametrize("token,input_string", ONE_LINE_COMMENT_FIXTURES)
def test_one_line_comments_extract(token, input_string):

    print(input_string)

    lexer = hcl.lexer.Lexer(export_comments='LINE')
    lexer.input(input_string)

    lex_tok = lexer.token()

    if lex_tok is None:
        assert token is None
    else:
        assert token == lex_tok.type
        assert lexer.token() is None

MULTI_LINE_COMMENT_FIXTURES = [
    (None, "//"),
    (None, "////"),
    (None, "// comment"),
    (None, "// /* comment */"),
    (None, "// // comment //"),
    (None, "//" + f100),
    (None, "#"),
    (None, "##"),
    (None, "# comment"),
    (None, "# /* comment */"),
    (None, "# # comment #"),
    (None, "#" + f100),
    ("MULTICOMMENT", "/**/"),
    ("MULTICOMMENT", "/***/"),
    ("MULTICOMMENT", "/* comment */"),
    ("MULTICOMMENT", "/* // comment */"),
    ("MULTICOMMENT", "/* /* comment */"),
    ("MULTICOMMENT", "/*\n comment\n*/"),
    ("MULTICOMMENT", "/*" + f100 + "*/")
]

@pytest.mark.parametrize("token,input_string", MULTI_LINE_COMMENT_FIXTURES)
def test_multi_line_comments_extract(token, input_string):

    print(input_string)

    lexer = hcl.lexer.Lexer(export_comments='MULTILINE')
    lexer.input(input_string)

    lex_tok = lexer.token()

    if lex_tok is None:
        assert token is None
    else:
        assert token == lex_tok.type
        assert lexer.token() is None

COMMENT_FIXTURES = [
    ("COMMENT", "//"),
    ("COMMENT", "////"),
    ("COMMENT", "// comment"),
    ("COMMENT", "// /* comment */"),
    ("COMMENT", "// // comment //"),
    ("COMMENT", "//" + f100),
    ("COMMENT", "#"),
    ("COMMENT", "##"),
    ("COMMENT", "# comment"),
    ("COMMENT", "# /* comment */"),
    ("COMMENT", "# # comment #"),
    ("COMMENT", "#" + f100),
    ("MULTICOMMENT", "/**/"),
    ("MULTICOMMENT", "/***/"),
    ("MULTICOMMENT", "/* comment */"),
    ("MULTICOMMENT", "/* // comment */"),
    ("MULTICOMMENT", "/* /* comment */"),
    ("MULTICOMMENT", "/*\n comment\n*/"),
    ("MULTICOMMENT", "/*" + f100 + "*/")
]

@pytest.mark.parametrize("token,input_string", COMMENT_FIXTURES)
def test_multi_line_comments_extract(token, input_string):

    print(input_string)

    lexer = hcl.lexer.Lexer(export_comments='ALL')
    lexer.input(input_string)

    lex_tok = lexer.token()

    if lex_tok is None:
        assert token is None
    else:
        assert token == lex_tok.type
        assert lexer.token() is None

# Testing EPLUS and EMINUS can't be done on their own since they
# require positive lookbehinds and therefore the lexer will find at least one
# other token
COMPLEX_TOKEN_FIXTURES = [
    # EPLUS
    (["IDENTIFIER", "EQUAL", "LEFTBRACKET", "IDENTIFIER", "LEFTBRACKET", "NUMBER", "RIGHTBRACKET", "PERIOD", "IDENTIFIER", "RIGHTBRACKET", ], "records = [aws_elasticsearch_domain.elasticsearch[0].endpoint]"),
    (["FLOAT", "EPLUS"], "0.e"),
    (["FLOAT", "EPLUS"], "1.e+"),
    (["NUMBER", "EPLUS"], "0e"),
    (["NUMBER", "EPLUS"], "0e+"),
    (["FLOAT", "EPLUS"], "1.E"),
    (["FLOAT", "EPLUS"], "1.E+"),
    (["NUMBER", "EPLUS"], "0E"),
    (["NUMBER", "EPLUS"], "1E+"),

    # EMINUS
    (["FLOAT", "EMINUS"], "0.e-"),
    (["NUMBER", "EMINUS"], "1e-"),
    (["FLOAT", "EMINUS"], "0.E-"),
    (["NUMBER", "EMINUS"], "1E-"),
]

@pytest.mark.parametrize("tokens,input_string", COMPLEX_TOKEN_FIXTURES)
def test_complex_tokens(tokens, input_string):

    print(input_string)

    lexer = hcl.lexer.Lexer()
    lexer.input(input_string)

    for tok in tokens:
        lex_tok = lexer.token()

        if lex_tok is None:
            assert tok is None
        else:
            assert tok == lex_tok.type
        print(lex_tok)

    assert lexer.token() is None