Debian Codegraph @

pypy/module/unicodedata/test/test_hyp.py

raw symbols SCIP
1 import sys
2 import pytest
3 try:
4 from hypothesis import given, strategies as st, example, settings, assume
5 except ImportError:
6 pytest.skip("hypothesis required")
7
8 from pypy.module.unicodedata.interp_ucd import ucd
9 from rpython.rlib.rutf8 import codepoints_in_utf8
10
11 def make_normalization(space, NF_code):
12 def normalize(s):
13 u = s.encode('utf8')
14 w_s = space.newutf8(u, codepoints_in_utf8(u))
15 w_res = ucd.normalize(space, NF_code, w_s)
16 return space.utf8_w(w_res).decode('utf8')
17 return normalize
18
19 all_forms = ['NFC', 'NFD', 'NFKC', 'NFKD']
20
21 # For every (n1, n2, n3) triple, applying n1 then n2 must be the same
22 # as applying n3.
23 # Reference: http://unicode.org/reports/tr15/#Design_Goals
24 compositions = [
25 ('NFC', 'NFC', 'NFC'),
26 ('NFC', 'NFD', 'NFD'),
27 ('NFC', 'NFKC', 'NFKC'),
28 ('NFC', 'NFKD', 'NFKD'),
29 ('NFD', 'NFC', 'NFC'),
30 ('NFD', 'NFD', 'NFD'),
31 ('NFD', 'NFKC', 'NFKC'),
32 ('NFD', 'NFKD', 'NFKD'),
33 ('NFKC', 'NFC', 'NFKC'),
34 ('NFKC', 'NFD', 'NFKD'),
35 ('NFKC', 'NFKC', 'NFKC'),
36 ('NFKC', 'NFKD', 'NFKD'),
37 ('NFKD', 'NFC', 'NFKC'),
38 ('NFKD', 'NFD', 'NFKD'),
39 ('NFKD', 'NFKC', 'NFKC'),
40 ('NFKD', 'NFKD', 'NFKD'),
41 ]
42
43
44 @pytest.mark.parametrize('NF1, NF2, NF3', compositions)
45 @example(s=u"\u0128")
46 @example(s=u'---\uafb8\u11a7---') # issue 2289
47 @settings(max_examples=1000)
48 @given(s=st.text())
49 def test_composition(s, space, NF1, NF2, NF3):
50 # 'chr(0xfacf) normalizes to chr(0x2284a), which is too big')
51 assume(not (s == u'\ufacf' and sys.maxunicode == 65535))
52 norm1, norm2, norm3 = [make_normalization(space, form) for form in [NF1, NF2, NF3]]
53 assert norm2(norm1(s)) == norm3(s)
54
55 if sys.maxunicode != 65535:
56 # conditionally generate the example via an unwrapped decorator
57 test_composition = example(s=u'\ufacf')(test_composition)