-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
414 lines (347 loc) · 12.3 KB
/
tests.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import unittest
import re
from subtitle_parser import SubtitleError, Subtitle, \
SrtParser, WebVttParser, \
render_html, render_csv, render_srt
def ts(hour, minute, second, milli):
return ((hour * 60 + minute) * 60 + second) * 1000 + milli
class TestSrtSubtitles(unittest.TestCase):
def test_valid(self):
import io
import textwrap
parser = SrtParser(io.StringIO(textwrap.dedent('''\
1
00:00:00,123 --> 00:00:03,456
Hi there
2
00:01:04,843 --> 00:01:05,428
This is an example of a
subtitle file in SRT format
''')))
parser.parse()
self.assertEqual(parser.subtitles, [
Subtitle(1, ts(0, 0, 0, 123), ts(0, 0, 3, 456), 'Hi there'),
Subtitle(
2, ts(0, 1, 4, 843), ts(0, 1, 5, 428),
'This is an example of a\nsubtitle file in SRT format',
),
])
def test_warnings(self):
import io
import textwrap
parser = SrtParser(io.StringIO(textwrap.dedent('''\
2
00:00:00,123 --> 00:00:03,456
Hi there
5
00:01:04,843 --> 00:01:05,428
This is an example of a
subtitle file in SRT format
''')))
parser.parse()
self.assertEqual(parser.subtitles, [
Subtitle(2, ts(0, 0, 0, 123), ts(0, 0, 3, 456), 'Hi there'),
Subtitle(
5, ts(0, 1, 4, 843), ts(0, 1, 5, 428),
'This is an example of a\nsubtitle file in SRT format',
),
])
self.assertEqual(parser.warnings, [
(1, 'Subtitle number is 2, expected 1'),
(7, 'Subtitle number is 5, expected 3'),
])
def test_wrong(self):
import io
with self.assertRaises(SubtitleError) as err:
SrtParser(io.StringIO('1\ntest\n')).parse()
self.assertEqual(err.exception.args[0], 'Invalid timestamps line 2')
with self.assertRaises(SubtitleError) as err:
SrtParser(
io.StringIO('1\n00:00:00,123 --> 00:00:03,456\n\n'),
).parse()
self.assertEqual(
err.exception.args[0],
'No content in subtitle line 2',
)
with self.assertRaises(SubtitleError) as err:
SrtParser(io.StringIO('1\n')).parse()
self.assertEqual(err.exception.args[0], 'Missing timestamps line 1')
with self.assertRaises(SubtitleError) as err:
SrtParser(
io.StringIO('00:00:00,123 --> 00:00:03,456\nHi there\n'),
).parse()
self.assertEqual(
err.exception.args[0],
'Missing subtitle number line 1',
)
def test_invalid_unicode(self):
import codecs
import io
with self.assertRaises(SubtitleError) as err:
SrtParser(codecs.getreader('utf-8')(io.BytesIO(
b'1\n00:00:00,123 --> 00:00:03,456\nHi there\n\n' * 100
+ b'\xE9\n'
+ b'1\n00:00:00,123 --> 00:00:03,456\nHi there\n\n' * 100
))).parse()
m = re.match(
'^Invalid unicode in subtitles near line ([0-9]+)$',
err.exception.args[0],
)
self.assertTrue(m)
# Can't assert exact line number, codec will raise before you get to
# the exact line because it decodes big chunks at a time
self.assertTrue(350 < int(m.group(1), 10) < 400)
class TestRender(unittest.TestCase):
subtitles = [
Subtitle(
2, ts(0, 0, 0, 123), ts(0, 0, 3, 456),
'Hi there', name='Remi',
),
Subtitle(
5, ts(0, 1, 4, 843), ts(0, 1, 5, 428),
'This is an example of a\nsubtitle file in SRT format',
),
]
@classmethod
def call_render(cls, func, show_name):
import io
out = io.StringIO()
func(cls.subtitles, out, show_name=show_name)
return out.getvalue()
def test_html(self):
nonames = (
'<p>00:00:00.123 Hi there</p>\n'
+ '<p>00:01:04.843 This is an example of a<br>'
+ 'subtitle file in SRT format</p>\n'
)
names = (
'<p>00:00:00.123 Remi: Hi there</p>\n'
+ '<p>00:01:04.843 This is an example of a'
+ '<br>subtitle file in SRT format</p>\n'
)
self.assertEqual(
self.call_render(render_html, True),
names,
)
self.assertEqual(
self.call_render(render_html, False),
nonames,
)
self.assertEqual(
self.call_render(render_html, None),
names,
)
def test_csv(self):
import textwrap
nonames = textwrap.dedent('''\
start,end,text\r
00:00:00.123,00:00:03.456,Hi there\r
00:01:04.843,00:01:05.428,"This is an example of a
subtitle file in SRT format"\r
''')
names = textwrap.dedent('''\
start,end,name,text\r
00:00:00.123,00:00:03.456,Remi,Hi there\r
00:01:04.843,00:01:05.428,,"This is an example of a
subtitle file in SRT format"\r
''')
self.assertEqual(
self.call_render(render_csv, True),
names,
)
self.assertEqual(
self.call_render(render_csv, False),
nonames,
)
self.assertEqual(
self.call_render(render_csv, None),
nonames,
)
def test_srt(self):
import textwrap
nonames = textwrap.dedent('''\
1
00:00:00,123 --> 00:00:03,456
Hi there
2
00:01:04,843 --> 00:01:05,428
This is an example of a
subtitle file in SRT format
''')
names = textwrap.dedent('''\
1
00:00:00,123 --> 00:00:03,456
[Remi]
Hi there
2
00:01:04,843 --> 00:01:05,428
This is an example of a
subtitle file in SRT format
''')
self.assertEqual(
self.call_render(render_srt, True),
names,
)
self.assertEqual(
self.call_render(render_srt, False),
nonames,
)
self.assertEqual(
self.call_render(render_srt, None),
names,
)
class TestWebVttSubtitles(unittest.TestCase):
def test_valid(self):
import io
import textwrap
# From https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API
parser = WebVttParser(io.StringIO(textwrap.dedent('''\
WEBVTT
STYLE
::cue {
background-image: linear-gradient(to bottom, dimgray, lightgray);
color: papayawhip;
}
/* Style blocks cannot use blank lines nor arrows */
NOTE comment blocks can be used between style blocks.
STYLE
::cue(b) {
color: peachpuff;
}
1
00:00:00,123 --> 00:00:03,456
Hi there
00:01:04,843 --> 00:01:05,428
This is an example of a
subtitle file in SRT format
NOTE style blocks cannot appear after the first cue.
''')))
parser.parse()
self.assertEqual(parser.subtitles, [
Subtitle(1, ts(0, 0, 0, 123), ts(0, 0, 3, 456), 'Hi there'),
Subtitle(
None, ts(0, 1, 4, 843), ts(0, 1, 5, 428),
'This is an example of a\nsubtitle file in SRT format',
),
])
def test_wrong(self):
import io
with self.assertRaises(SubtitleError) as err:
WebVttParser(
io.StringIO('1\n00:00:00,123 --> 00:00:03,456\ntest\n'),
).parse()
self.assertEqual(
err.exception.args[0],
"First line doesn't start with 'WEBVTT'",
)
def test_numbering_check(self):
import io
import textwrap
parser = WebVttParser(io.StringIO(textwrap.dedent('''\
WEBVTT
1
00:00:00,123 --> 00:00:01,456
number
00:00:02,000 --> 00:00:03,000
no number
00:00:04,000 --> 00:00:05,000
no number
2
00:00:06,000 --> 00:00:07,000
number
4
00:00:08,000 --> 00:00:09,000
wrong number
00:00:10,000 --> 00:00:11,000
no number
''')))
parser.parse()
self.assertEqual(parser.subtitles, [
Subtitle(1, ts(0, 0, 0, 123), ts(0, 0, 1, 456), 'number'),
Subtitle(None, ts(0, 0, 2, 0), ts(0, 0, 3, 0), 'no number'),
Subtitle(None, ts(0, 0, 4, 0), ts(0, 0, 5, 0), 'no number'),
Subtitle(2, ts(0, 0, 6, 0), ts(0, 0, 7, 0), 'number'),
Subtitle(4, ts(0, 0, 8, 0), ts(0, 0, 9, 0), 'wrong number'),
Subtitle(None, ts(0, 0, 10, 0), ts(0, 0, 11, 0), 'no number'),
])
self.assertEqual(parser.warnings, [
(6, 'Subtitle numbers stop line 7'),
(13, 'Subtitle numbers (re)starts line 14'),
(17, 'Subtitle number is 4, expected 3'),
(20, 'Subtitle numbers stop line 21'),
])
def test_webex_check(self):
import io
import textwrap
parser = WebVttParser(io.StringIO(textwrap.dedent('''\
WEBVTT
1 "Shmo, Jonathan" (1838608384)
00:11:03.989 --> 00:11:07.169
Another option is just to pop the tool tip.
2 "Doe, Kevin" (3768348160)
00:11:07.169 --> 00:11:16.619
Right. That's okay. That's I'm okay with that.
3 "Shmo, Jonathan" (1838608384)
00:11:16.619 --> 00:11:23.369
Yep, yeah, we can just defaulted on for the 1st time.
4 "Doe, Kevin" (3768348160)
00:11:23.369 --> 00:11:28.679
Um, Paul, I think Paul had the hands up.
5 "Conf WA HQ B5 3A" (1129774080)
00:11:28.679 --> 00:11:34.649
What is what is the difference for the user?
6 "Conf WA HQ B5 3A" (1129774080)
00:11:34.649 --> 00:11:38.429
Whether there's Bluetooth connection.
7 "Conf WA HQ B5 3A" (1129774080)
00:11:38.429 --> 00:11:42.929
Or there's no Bluetooth connection because.
8 "Conf WA HQ B5 3A" (1129774080)
00:11:42.929 --> 00:11:47.309
If you lose Bluetooth connection, you still have your GPS.
''')))
parser.parse()
self.assertEqual(parser.subtitles, [
Subtitle(
1, ts(0, 11, 3, 989), ts(0, 11, 7, 169),
"Another option is just to pop the tool tip.",
name="Shmo, Jonathan",
),
Subtitle(
2, ts(0, 11, 7, 169), ts(0, 11, 16, 619),
"Right. That's okay. That's I'm okay with that.",
name="Doe, Kevin",
),
Subtitle(
3, ts(0, 11, 16, 619), ts(0, 11, 23, 369),
"Yep, yeah, we can just defaulted on for the 1st time.",
name="Shmo, Jonathan",
),
Subtitle(
4, ts(0, 11, 23, 369), ts(0, 11, 28, 679),
"Um, Paul, I think Paul had the hands up.",
name="Doe, Kevin",
),
Subtitle(
5, ts(0, 11, 28, 679), ts(0, 11, 34, 649),
"What is what is the difference for the user?",
name="Conf WA HQ B5 3A",
),
Subtitle(
6, ts(0, 11, 34, 649), ts(0, 11, 38, 429),
"Whether there's Bluetooth connection.",
name="Conf WA HQ B5 3A",
),
Subtitle(
7, ts(0, 11, 38, 429), ts(0, 11, 42, 929),
"Or there's no Bluetooth connection because.",
name="Conf WA HQ B5 3A",
),
Subtitle(
8, ts(0, 11, 42, 929), ts(0, 11, 47, 309),
"If you lose Bluetooth connection, you still have your GPS.",
name="Conf WA HQ B5 3A",
),
])
if __name__ == '__main__':
unittest.main()