Debian Codegraph @

tests/test_token.py

raw
1 """
2 Test suite for the token module
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
6 :license: BSD, see LICENSE for details.
7 """
8
9 import copy
10
11 import pytest
12
13 from pygments import token
14
15
16 def test_tokentype():
17 t = token.String
18 assert t.split() == [token.Token, token.Literal, token.String]
19 assert t.__class__ is token._TokenType
20
21
22 def test_functions():
23 assert token.is_token_subtype(token.String, token.String)
24 assert token.is_token_subtype(token.String, token.Literal)
25 assert not token.is_token_subtype(token.Literal, token.String)
26
27 assert token.string_to_tokentype(token.String) is token.String
28 assert token.string_to_tokentype('') is token.Token
29 assert token.string_to_tokentype('String') is token.String
30
31
32 def test_sanity_check():
33 stp = token.STANDARD_TYPES.copy()
34 stp[token.Token] = '---' # Token and Text do conflict, that is okay
35 t = {}
36 for k, v in stp.items():
37 t.setdefault(v, []).append(k)
38 if len(t) == len(stp):
39 return # Okay
40
41 for k, v in t.items():
42 if len(v) > 1:
43 pytest.fail(f"{k!r} has more than one key: {v!r}")
44
45
46 def test_copying():
47 # Token instances are supposed to be singletons, so copying or even
48 # deepcopying should return themselves
49 t = token.String
50 assert t is copy.copy(t)
51 assert t is copy.deepcopy(t)