-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathg_dsl_test.py
246 lines (211 loc) · 6.58 KB
/
g_dsl_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import sys
import tempfile
import unittest
from os.path import abspath, dirname, join
rootDir = dirname(dirname(abspath(__file__)))
sys.path.insert(0, rootDir)
from glossary_v2_test import TestGlossaryBase
from pyglossary.glossary_v2 import ConvertArgs, Glossary
class TestGlossaryDSL(TestGlossaryBase):
def __init__(self, *args, **kwargs):
TestGlossaryBase.__init__(self, *args, **kwargs)
self.dataFileCRC32.update(
{
"dsl/100-RussianAmericanEnglish-ru-en.dsl": "c24491e0",
"dsl/100-RussianAmericanEnglish-ru-en-v3.txt": "43b6d58e",
"dsl/001-empty-lines-br.dsl": "6f2fca1a",
"dsl/001-empty-lines-br.txt": "74e578ff",
"dsl/002-m-tag_multiline-paragraph.dsl": "334079e3",
"dsl/002-m-tag_multiline-paragraph-v2.txt": "d5001afd",
"dsl/003-ref-target-c.dsl": "9c1396c4",
"dsl/003-ref-target-c.txt": "ab41cedf",
"dsl/006-include/included.dsl": "10ef1018",
"dsl/006-include/main.dsl": "8325d538",
"dsl/006-include/main.txt": "d7ed9f2b",
},
)
def convert_string_dsl_txt(
self,
dsl: str,
txtExpected: str,
removeInfo: bool = True,
**convertArgs,
):
prefix = join(self.tempDir, "")
dsl_fname = tempfile.mktemp(suffix=".dsl", prefix=prefix)
txt_fname = tempfile.mktemp(suffix=".txt", prefix=prefix)
with open(dsl_fname, "w", encoding="utf-8") as _file:
_file.write(dsl)
glos = self.glos = Glossary()
# glos.config = config
res = glos.convert(
ConvertArgs(
inputFilename=dsl_fname,
outputFilename=txt_fname,
**convertArgs,
),
)
self.assertEqual(txt_fname, res)
with open(txt_fname, encoding="utf-8") as _file:
txtActual = _file.read()
if removeInfo:
txtActual = "\n".join(
[
line
for line in txtActual.split("\n")
if line and not line.startswith("#")
],
)
self.assertEqual(txtExpected, txtActual)
def convert_dsl_txt(self, fname, fname2, **convertArgs):
self.convert(
f"dsl/{fname}.dsl",
f"{fname}-2.txt",
compareText=f"dsl/{fname2}.txt",
**convertArgs,
)
def test_include(self):
dirName = "dsl/006-include"
fname = "main"
files = ["included.dsl", "main.dsl"]
inputDirPath = self.downloadDir(dirName, files)
inputFilePath = join(inputDirPath, f"{fname}.dsl")
outputFilePath = self.newTempFilePath(f"{fname}-2.txt")
expectedOutputFilePath = self.downloadFile(join(dirName, f"{fname}.txt"))
self.glos = Glossary()
result = self.glos.convert(
ConvertArgs(
inputFilename=inputFilePath,
outputFilename=outputFilePath,
)
)
self.assertIsNotNone(result)
self.assertEqual(result, outputFilePath)
self.compareTextFiles(
outputFilePath,
expectedOutputFilePath,
)
def test_russianAmericanEnglish(self):
self.convert_dsl_txt(
"100-RussianAmericanEnglish-ru-en",
"100-RussianAmericanEnglish-ru-en-v3",
)
def test_empty_lines_br(self):
self.convert_dsl_txt(
"001-empty-lines-br",
"001-empty-lines-br",
)
def test_m_tag_multiline_paragraph(self):
self.convert_dsl_txt(
"002-m-tag_multiline-paragraph",
"002-m-tag_multiline-paragraph-v2",
)
def test_ref_target_c(self):
self.convert_dsl_txt(
"003-ref-target-c",
"003-ref-target-c",
)
def test_headword_formatting_bashkir_basque(self):
# from Bashkir -> Basque dict (001-headword-with-formatting.dsl)
dsl = (
"{[c slategray]}{to }{[/c]}tell "
"{[c slategray]}smb{[/c]} how to do "
"{[c slategray]}smth{[/c]}\n [m1][trn]"
"рассказать кому-либо, как что-либо делать[/trn][/m]"
)
txt = (
"tell smb how to do smth\t"
'<b><font color="slategray">to </font>tell '
'<font color="slategray">smb</font> how to do '
'<font color="slategray">smth</font></b><br/>'
'<p style="padding-left:1em;margin:0">'
"рассказать кому-либо, как что-либо делать</p>"
)
self.convert_string_dsl_txt(dsl, txt)
def test_headword_formatting_english(self):
dsl = (
"{[c slategray]}{to }{[/c]}tell"
" {[c violet]}smb{[/c]} {[u]}how{[/u]}"
" to do {[c violet]}smth{[/c]} {[sub]subscript[/sub]}\n"
" [m1]1. main meaning[/m]\n"
" [m2]a. first submeaning[/m]\n"
" [m2]b. second submeaning[/m]\n"
)
txt = (
"tell smb how to do smth\t"
'<b><font color="slategray">to </font>tell'
' <font color="violet">smb</font> <u>how</u>'
' to do <font color="violet">smth</font> <sub>subscript</sub></b><br/>'
'<p style="padding-left:1em;margin:0">1. main meaning</p>'
'<p style="padding-left:2em;margin:0">a. first submeaning</p>'
'<p style="padding-left:2em;margin:0">b. second submeaning</p>'
)
self.convert_string_dsl_txt(dsl, txt)
def test_p_unclosed(self):
dsl = "headword\n [m1][p]test\n"
txt = (
"headword\t"
'<p style="padding-left:1em;margin:0"><i class="p"><font color="green">test\\n</font></i>'
)
self.convert_string_dsl_txt(dsl, txt)
def test_headword_paran(self):
self.convert_string_dsl_txt(
"headword with (parenthesis)\n test",
"headword with parenthesis|headword with\ttest",
)
def test_headword_paran_2(self):
self.convert_string_dsl_txt(
"(headword with) parenthesis\n test",
"headword with parenthesis|parenthesis\ttest",
)
def test_headword_paran_escaped(self):
self.convert_string_dsl_txt(
"headword \\(with escaped parenthesis\\)\n test",
"headword (with escaped parenthesis)\ttest",
)
def test_headword_paran_escaped_2(self):
self.convert_string_dsl_txt(
"headword (with escaped right \\) parenthesis)\n test",
"headword with escaped right \\\\) parenthesis|headword\ttest",
)
def test_headword_curly(self):
txt = (
"headword with curly brackets\t"
"<b>headword with <b>curly brackets</b></b><br/>test"
)
self.convert_string_dsl_txt(
"headword with {[b]}curly brackets{[/b]}\n test",
txt,
)
def test_headword_curly_escaped(self):
self.convert_string_dsl_txt(
"headword with escaped \\{\\}curly brackets\\{\n test",
"headword with escaped {}curly brackets{\ttest",
)
def test_double_brackets_1(self):
self.convert_string_dsl_txt(
"test\n hello [[world]]",
"test\thello [world]",
)
def test_double_brackets_2(self):
self.convert_string_dsl_txt(
"test\n hello [[",
"test\thello [",
)
def test_double_brackets_3(self):
self.convert_string_dsl_txt(
"test\n hello ]]",
"test\thello ]",
)
def test_ref_double_ltgt(self):
self.convert_string_dsl_txt(
"test\n hello <<world>>",
'test\thello <a href="bword://world">world</a>',
)
def test_ref_double_ltgt_escaped(self):
self.convert_string_dsl_txt(
"test\n hello \\<<world\\>>",
"test\thello <<world>>",
)
if __name__ == "__main__":
unittest.main()