File: test_tokenizer.py

package info (click to toggle)
blueprint-compiler 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,140 kB
  • sloc: python: 8,504; sh: 31; makefile: 6
file content (88 lines) | stat: -rw-r--r-- 3,079 bytes parent folder | download
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
# tokenizer.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later


import unittest

from blueprintcompiler.errors import PrintableError
from blueprintcompiler.tokenizer import Token, TokenType, tokenize


class TestTokenizer(unittest.TestCase):
    def assert_tokenize(self, string: str, expect: list[Token]):
        try:
            tokens = tokenize(string)
            self.assertEqual(len(tokens), len(expect))
            for token, (type, token_str) in zip(tokens, expect):
                self.assertEqual(token.type, type)
                self.assertEqual(str(token), token_str)
        except PrintableError as e:  # pragma: no cover
            e.pretty_print("<test input>", string)
            raise e

    def test_basic(self):
        self.assert_tokenize(
            "ident(){}; \n <<+>>*/=",
            [
                (TokenType.IDENT, "ident"),
                (TokenType.PUNCTUATION, "("),
                (TokenType.PUNCTUATION, ")"),
                (TokenType.PUNCTUATION, "{"),
                (TokenType.PUNCTUATION, "}"),
                (TokenType.PUNCTUATION, ";"),
                (TokenType.WHITESPACE, " \n "),
                (TokenType.OP, "<<"),
                (TokenType.OP, "+"),
                (TokenType.OP, ">>"),
                (TokenType.OP, "*"),
                (TokenType.OP, "/"),
                (TokenType.OP, "="),
                (TokenType.EOF, ""),
            ],
        )

    def test_quotes(self):
        self.assert_tokenize(
            r'"this is a \n string""this is \\another \"string\""',
            [
                (TokenType.QUOTED, r'"this is a \n string"'),
                (TokenType.QUOTED, r'"this is \\another \"string\""'),
                (TokenType.EOF, ""),
            ],
        )

    def test_comments(self):
        self.assert_tokenize(
            "/* \n \\n COMMENT /* */",
            [
                (TokenType.COMMENT, "/* \n \\n COMMENT /* */"),
                (TokenType.EOF, ""),
            ],
        )
        self.assert_tokenize(
            "line // comment\nline",
            [
                (TokenType.IDENT, "line"),
                (TokenType.WHITESPACE, " "),
                (TokenType.COMMENT, "// comment"),
                (TokenType.WHITESPACE, "\n"),
                (TokenType.IDENT, "line"),
                (TokenType.EOF, ""),
            ],
        )