forked from justvanrossum/fontgoggles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_segmenting.py
152 lines (140 loc) · 4.95 KB
/
test_segmenting.py
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
from collections import deque
import pytest
from fontgoggles.misc.segmenting import getBiDiInfo, detectScript, textSegments
testData = [
("Abc",
{'base_dir': 'L',
'base_level': 0,
'chars': [{'ch': 'A',
'index': 0,
'level': 0,
'orig': 'L',
'type': 'L'},
{'ch': 'b',
'index': 1,
'level': 0,
'orig': 'L',
'type': 'L'},
{'ch': 'c',
'index': 2,
'level': 0,
'orig': 'L',
'type': 'L'}],
'runs': deque([{'eor': 'L',
'length': 3,
'sor': 'L',
'start': 0,
'type': 'L'}])}),
("\u062D\u062A\u0649",
{'base_dir': 'R',
'base_level': 1,
'chars': [{'ch': '\u0649',
'index': 2,
'level': 1,
'orig': 'AL',
'type': 'R'},
{'ch': '\u062A',
'index': 1,
'level': 1,
'orig': 'AL',
'type': 'R'},
{'ch': '\u062D',
'index': 0,
'level': 1,
'orig': 'AL',
'type': 'R'}],
'runs': deque([{'eor': 'R',
'length': 3,
'sor': 'R',
'start': 0,
'type': 'AL'}])}),
("\u062D\u062A\u064912",
{'base_dir': 'R',
'base_level': 1,
'chars': [{'ch': '1',
'index': 3,
'level': 2,
'orig': 'EN',
'type': 'AN'},
{'ch': '2',
'index': 4,
'level': 2,
'orig': 'EN',
'type': 'AN'},
{'ch': '\u0649',
'index': 2,
'level': 1,
'orig': 'AL',
'type': 'R'},
{'ch': '\u062A',
'index': 1,
'level': 1,
'orig': 'AL',
'type': 'R'},
{'ch': '\u062D',
'index': 0,
'level': 1,
'orig': 'AL',
'type': 'R'}],
'runs': deque([{'eor': 'R',
'length': 5,
'sor': 'R',
'start': 0,
'type': 'EN'}])}),
("\u0627\u064f\u0633",
{'base_dir': 'R',
'base_level': 1,
'chars': [{'ch': 'س',
'index': 2,
'level': 1,
'orig': 'AL',
'type': 'R'},
{'ch': 'ُ',
'index': 1,
'level': 1,
'orig': 'NSM',
'type': 'R'},
{'ch': 'ا',
'index': 0,
'level': 1,
'orig': 'AL',
'type': 'R'}],
'runs': deque([{'eor': 'R',
'length': 3,
'sor': 'R',
'start': 0,
'type': 'AL'}])}),
]
@pytest.mark.parametrize("testString,expectedInfo", testData)
def test_getBiDiInfo_ltr(testString, expectedInfo):
info = getBiDiInfo(testString)
assert info == expectedInfo
testDataDetectScript = [
(" ", ['Zxxx']),
("abc", ['Latn', 'Latn', 'Latn']),
("(abc)", ['Latn', 'Latn', 'Latn', 'Latn', 'Latn']),
("\u0627\u064f\u0633", ['Arab', 'Arab', 'Arab']),
("(\u0627\u064f\u0633)", ['Arab', 'Arab', 'Arab', 'Arab', 'Arab']),
("a(\u0627\u064f\u0633)", ['Latn', 'Latn', 'Arab', 'Arab', 'Arab', 'Arab']),
("a(\u0627\u064f\u0633)a", ['Latn', 'Latn', 'Arab', 'Arab', 'Arab', 'Latn', 'Latn']),
("\u0627\u064f(a)\u0633", ['Arab', 'Arab', 'Arab', 'Latn', 'Arab', 'Arab']),
]
@pytest.mark.parametrize("testString,expectedScripts", testDataDetectScript)
def test_detectScript(testString, expectedScripts):
assert detectScript(testString) == expectedScripts
testDataTextSegments = [
("a", 0, [("a", "Latn", 0, 0)]),
("\u0627", 1, [("\u0627", "Arab", 1, 0)]),
("a\u0627", 0, [("a", "Latn", 0, 0), ("\u0627", "Arab", 1, 1)]),
("\u0627123", 1, [("\u0627", "Arab", 1, 0), ("123", "Arab", 2, 1)]),
("\u0627123\u0627", 1, [("\u0627", "Arab", 1, 0), ("123", "Arab", 2, 1), ("\u0627", "Arab", 1, 4)]),
("123\u0627", 1, [("123", "Arab", 2, 0), ("\u0627", "Arab", 1, 3)]),
("a123\u0627", 0, [("a123", "Latn", 0, 0), ("\u0627", "Arab", 1, 4)]),
]
@pytest.mark.parametrize("testString,expectedBaseLevel,expectedSegments", testDataTextSegments)
def test_textSegments(testString, expectedBaseLevel, expectedSegments):
segments, baseLevel = textSegments(testString)
assert baseLevel == expectedBaseLevel
assert len(segments) == len(expectedSegments)
for segment, expectedSegment in zip(segments, expectedSegments):
assert segment == expectedSegment