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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
|
import unittest
from webvtt.models import Timestamp, Caption, Style
from webvtt.errors import MalformedCaptionError
class TestTimestamp(unittest.TestCase):
def test_instantiation(self):
timestamp = Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
)
self.assertEqual(timestamp.hours, 1)
self.assertEqual(timestamp.minutes, 12)
self.assertEqual(timestamp.seconds, 23)
self.assertEqual(timestamp.milliseconds, 500)
def test_from_string(self):
timestamp = Timestamp.from_string('01:24:11.670')
self.assertEqual(timestamp.hours, 1)
self.assertEqual(timestamp.minutes, 24)
self.assertEqual(timestamp.seconds, 11)
self.assertEqual(timestamp.milliseconds, 670)
def test_from_string_single_digits(self):
timestamp = Timestamp.from_string('1:2:7.670')
self.assertEqual(timestamp.hours, 1)
self.assertEqual(timestamp.minutes, 2)
self.assertEqual(timestamp.seconds, 7)
self.assertEqual(timestamp.milliseconds, 670)
def test_from_string_missing_hours(self):
timestamp = Timestamp.from_string('24:11.670')
self.assertEqual(timestamp.hours, 0)
self.assertEqual(timestamp.minutes, 24)
self.assertEqual(timestamp.seconds, 11)
self.assertEqual(timestamp.milliseconds, 670)
def test_from_string_wrong_minutes(self):
with self.assertRaises(MalformedCaptionError):
Timestamp.from_string('01:76:11.670')
def test_from_string_wrong_seconds(self):
with self.assertRaises(MalformedCaptionError):
Timestamp.from_string('01:24:87.670')
def test_from_string_wrong_type(self):
with self.assertRaises(MalformedCaptionError):
Timestamp.from_string(1234)
def test_from_string_wrong_value(self):
with self.assertRaises(MalformedCaptionError):
Timestamp.from_string('01:24:11:670')
def test_to_tuple(self):
self.assertEqual(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
).to_tuple(),
(1, 12, 23, 500)
)
def test_equality(self):
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
) ==
Timestamp.from_string('01:12:23.500')
)
def test_not_equality(self):
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
) !=
Timestamp.from_string('01:12:23.600')
)
def test_greater_than(self):
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=600
) >
Timestamp.from_string('01:12:23.500')
)
def test_less_than(self):
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
) <
Timestamp.from_string('01:12:23.600')
)
def test_greater_or_equal_than(self):
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
) >=
Timestamp.from_string('01:12:23.500')
)
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=600
) >=
Timestamp.from_string('01:12:23.500')
)
def test_less_or_equal_than(self):
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
) <=
Timestamp.from_string('01:12:23.500')
)
self.assertTrue(
Timestamp(
hours=1, minutes=12, seconds=23, milliseconds=500
) <=
Timestamp.from_string('01:12:23.600')
)
def test_repr(self):
timestamp = Timestamp(
hours=1,
minutes=12,
seconds=45,
milliseconds=320
)
self.assertEqual(
repr(timestamp),
'<Timestamp hours=1 minutes=12 seconds=45 milliseconds=320>'
)
class TestCaption(unittest.TestCase):
def test_instantiation(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
self.assertEqual(caption.start, '00:00:07.000')
self.assertEqual(caption.end, '00:00:11.890')
self.assertEqual(caption.text, 'Hello test!')
self.assertEqual(caption.identifier, 'A test caption')
def test_timestamp_wrong_type(self):
with self.assertRaises(MalformedCaptionError):
Caption(
start=1234,
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
def test_identifier_is_optional(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
)
self.assertIsNone(caption.identifier)
def test_multi_lines(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!\nThis is the second line',
identifier='A test caption'
)
self.assertEqual(caption.text, 'Hello test!\nThis is the second line')
self.assertListEqual(
caption.lines,
['Hello test!', 'This is the second line']
)
def test_multi_lines_accepts_list(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text=['Hello test!', 'This is the second line'],
identifier='A test caption'
)
self.assertEqual(caption.text, 'Hello test!\nThis is the second line')
self.assertListEqual(
caption.lines,
['Hello test!', 'This is the second line']
)
def test_cuetags(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text=[
'Hello <c.colorE5E5E5>test</c>!',
'This is the <c.colorE5E5E5>second</c> line'
],
identifier='A test caption'
)
self.assertEqual(caption.text, 'Hello test!\nThis is the second line')
self.assertEqual(
caption.raw_text,
'Hello <c.colorE5E5E5>test</c>!\n'
'This is the <c.colorE5E5E5>second</c> line'
)
def test_in_seconds(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text=['Hello test!', 'This is the second line'],
identifier='A test caption'
)
self.assertEqual(caption.start_in_seconds, 7)
self.assertEqual(caption.end_in_seconds, 11)
def test_wrong_start_timestamp(self):
self.assertRaises(
MalformedCaptionError,
Caption,
start='1234',
end='00:00:11.890',
text='Hello Test!'
)
def test_wrong_type_start_timestamp(self):
self.assertRaises(
MalformedCaptionError,
Caption,
start=1234,
end='00:00:11.890',
text='Hello Test!'
)
def test_wrong_end_timestamp(self):
self.assertRaises(
MalformedCaptionError,
Caption,
start='00:00:07.000',
end='1234',
text='Hello Test!'
)
def test_wrong_type_end_timestamp(self):
self.assertRaises(
MalformedCaptionError,
Caption,
start='00:00:07.000',
end=1234,
text='Hello Test!'
)
def test_equality(self):
caption1 = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
caption2 = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
self.assertTrue(caption1 == caption2)
caption1 = Caption(
start='00:00:02.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
caption2 = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
self.assertFalse(caption1 == caption2)
self.assertFalse(
Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
) == 1234
)
def test_repr(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
self.assertEqual(
repr(caption),
"<Caption start='00:00:07.000' end='00:00:11.890' "
"text='Hello test!' identifier='A test caption'>"
)
def test_str(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
self.assertEqual(
str(caption),
'00:00:07.000 00:00:11.890 Hello test!'
)
def test_accept_comments(self):
caption = Caption(
start='00:00:07.000',
end='00:00:11.890',
text='Hello test!',
identifier='A test caption'
)
caption.comments.append('One comment')
caption.comments.append('Another comment')
self.assertListEqual(
caption.comments,
['One comment', 'Another comment']
)
def test_timestamp_update(self):
c = Caption('00:00:00.500', '00:00:07.000')
c.start = '00:00:01.750'
c.end = '00:00:08.250'
self.assertEqual(c.start, '00:00:01.750')
self.assertEqual(c.end, '00:00:08.250')
def test_timestamp_format(self):
c = Caption('01:02:03.400', '02:03:04.500')
self.assertEqual(c.start, '01:02:03.400')
self.assertEqual(c.end, '02:03:04.500')
c = Caption('02:03.400', '03:04.500')
self.assertEqual(c.start, '00:02:03.400')
self.assertEqual(c.end, '00:03:04.500')
def test_update_text(self):
c = Caption(text='Caption line #1')
c.text = 'Caption line #1 updated'
self.assertEqual(
c.text,
'Caption line #1 updated'
)
def test_update_text_multiline(self):
c = Caption(text='Caption line #1')
c.text = 'Caption line #1\nCaption line #2'
self.assertEqual(
len(c.lines),
2
)
self.assertEqual(
c.text,
'Caption line #1\nCaption line #2'
)
def test_update_text_wrong_type(self):
c = Caption(text='Caption line #1')
self.assertRaises(
AttributeError,
setattr,
c,
'text',
123
)
def test_manipulate_lines(self):
c = Caption(text=['Caption line #1', 'Caption line #2'])
c.lines[0] = 'Caption line #1 updated'
self.assertEqual(
c.lines[0],
'Caption line #1 updated'
)
def test_malformed_start_timestamp(self):
self.assertRaises(
MalformedCaptionError,
Caption,
'01:00'
)
def test_voice_span(self):
caption = Caption(text='<v Homer Simpson>Hello there!</v>')
self.assertEqual(caption.text, 'Hello there!')
self.assertEqual(caption.raw_text, '<v Homer Simpson>Hello there!</v>')
self.assertEqual(caption.voice, 'Homer Simpson')
def test_voice_span_with_classes(self):
caption = Caption(text='<v.quiet.slow Lisa Simpson>I am Lisa</v>')
self.assertEqual(caption.text, 'I am Lisa')
self.assertEqual(
caption.raw_text,
'<v.quiet.slow Lisa Simpson>I am Lisa</v>'
)
self.assertEqual(caption.voice, 'Lisa Simpson')
def test_voice_span_is_invalid(self):
caption = Caption(text='<v Lets eat donuts')
self.assertEqual(caption.text, '<v Lets eat donuts')
self.assertEqual(
caption.raw_text,
'<v Lets eat donuts'
)
self.assertIsNone(caption.voice)
def test_voice_span_injected(self):
caption = Caption(text='This is a test')
self.assertEqual(caption.text, 'This is a test')
self.assertEqual(caption.raw_text, 'This is a test')
self.assertIsNone(caption.voice)
caption.text = '<v Homer Simpson>I like tests</v>'
self.assertEqual(caption.text, 'I like tests')
self.assertEqual(caption.raw_text, '<v Homer Simpson>I like tests</v>')
self.assertEqual(caption.voice, 'Homer Simpson')
def test_voice_span_removed(self):
caption = Caption(text='<v Homer Simpson>I like tests</v>')
self.assertEqual(caption.text, 'I like tests')
self.assertEqual(caption.raw_text, '<v Homer Simpson>I like tests</v>')
self.assertEqual(caption.voice, 'Homer Simpson')
caption.text = 'This is a test'
self.assertEqual(caption.text, 'This is a test')
self.assertEqual(caption.raw_text, 'This is a test')
self.assertIsNone(caption.voice)
class TestStyle(unittest.TestCase):
def test_instantiation(self):
style = Style(text='::cue(b) {\ncolor: peachpuff;\n}')
self.assertEqual(style.text, '::cue(b) {\ncolor: peachpuff;\n}')
self.assertListEqual(
style.lines,
['::cue(b) {', 'color: peachpuff;', '}']
)
def test_text_accept_list_of_strings(self):
style = Style(text=['::cue(b) {', 'color: peachpuff;', '}'])
self.assertEqual(style.text, '::cue(b) {\ncolor: peachpuff;\n}')
self.assertListEqual(
style.lines,
['::cue(b) {', 'color: peachpuff;', '}']
)
def test_accept_comments(self):
style = Style(text='::cue(b) {\ncolor: peachpuff;\n}')
style.comments.append('One comment')
style.comments.append('Another comment')
self.assertListEqual(
style.comments,
['One comment', 'Another comment']
)
def test_get_text(self):
style = Style(['::cue(b) {', ' color: peachpuff;', '}'])
self.assertEqual(
style.text,
'::cue(b) {\n color: peachpuff;\n}'
)
|