Debian Codegraph @

tests/test_unistring.py

raw
1 """
2 Test suite for the unistring 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 re
10 import random
11
12 from pygments import unistring as uni
13
14
15 def test_cats_exist_and_compilable():
16 for cat in uni.cats:
17 s = getattr(uni, cat)
18 if s == '': # Probably Cs on Jython
19 continue
20 print(f"{cat} {s!r}")
21 re.compile(f'[{s}]')
22
23
24 def _cats_that_match(c):
25 matching_cats = []
26 for cat in uni.cats:
27 s = getattr(uni, cat)
28 if s == '': # Probably Cs on Jython
29 continue
30 if re.compile(f'[{s}]').match(c):
31 matching_cats.append(cat)
32 return matching_cats
33
34
35 def test_spot_check_types():
36 # Each char should match one, and precisely one, category
37 random.seed(0)
38 for i in range(1000):
39 o = random.randint(0, 65535)
40 c = chr(o)
41 if o > 0xd800 and o <= 0xdfff and not uni.Cs:
42 continue # Bah, Jython.
43 print(hex(o))
44 cats = _cats_that_match(c)
45 assert len(cats) == 1, "%d (%s): %s" % (o, c, cats)