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
|
import unittest
import io
import textwrap
from webvtt import srt
from webvtt.errors import MalformedFileError
from webvtt.models import Caption
class TestSRTCueBlock(unittest.TestCase):
def test_is_valid(self):
self.assertTrue(srt.SRTCueBlock.is_valid(textwrap.dedent('''
1
00:00:00,500 --> 00:00:07,000
Caption #1
''').strip().split('\n'))
)
self.assertTrue(srt.SRTCueBlock.is_valid(textwrap.dedent('''
1
00:00:00,500 --> 00:00:07,000
Caption #1 line 1
Caption #1 line 2
''').strip().split('\n'))
)
self.assertFalse(srt.SRTCueBlock.is_valid(textwrap.dedent('''
00:00:00,500 --> 00:00:07,000
Caption #1
''').strip().split('\n'))
)
self.assertFalse(srt.SRTCueBlock.is_valid(textwrap.dedent('''
1
00:00:00.500 --> 00:00:07.000
Caption #1
''').strip().split('\n'))
)
self.assertFalse(srt.SRTCueBlock.is_valid(textwrap.dedent('''
1
00:00:00,500 --> 00:00:07,000
''').strip().split('\n'))
)
self.assertFalse(srt.SRTCueBlock.is_valid(textwrap.dedent('''
1
Caption #1
''').strip().split('\n'))
)
self.assertFalse(srt.SRTCueBlock.is_valid(textwrap.dedent('''
Caption #1
''').strip().split('\n'))
)
self.assertFalse(srt.SRTCueBlock.is_valid(textwrap.dedent('''
00:00:00,500 --> 00:00:07,000
''').strip().split('\n'))
)
def test_from_lines(self):
cue_block = srt.SRTCueBlock.from_lines(textwrap.dedent('''
1
00:00:00,500 --> 00:00:07,000
Caption #1 line 1
Caption #1 line 2
''').strip().split('\n')
)
self.assertEqual(cue_block.index, '1')
self.assertEqual(
cue_block.start,
'00:00:00,500'
)
self.assertEqual(
cue_block.end,
'00:00:07,000'
)
self.assertEqual(
cue_block.payload,
['Caption #1 line 1', 'Caption #1 line 2']
)
class TestSRTModule(unittest.TestCase):
def test_parse_invalid_format(self):
self.assertRaises(
MalformedFileError,
srt.parse,
textwrap.dedent('''
00:00:00,500 --> 00:00:07,000
Caption text #1
00:00:07,000 --> 00:00:11,890
Caption text #2
''').strip().split('\n')
)
def test_parse_captions(self):
captions = srt.parse(
textwrap.dedent('''
1
00:00:00,500 --> 00:00:07,000
Caption #1
2
00:00:07,000 --> 00:00:11,890
Caption #2 line 1
Caption #2 line 2
''').strip().split('\n')
)
self.assertEqual(len(captions), 2)
self.assertIsInstance(captions[0], Caption)
self.assertIsInstance(captions[1], Caption)
self.assertEqual(
str(captions[0]),
'00:00:00.500 00:00:07.000 Caption #1'
)
self.assertEqual(
str(captions[1]),
r'00:00:07.000 00:00:11.890 Caption #2 line 1\n'
'Caption #2 line 2'
)
def test_write(self):
out = io.StringIO()
captions = [
Caption(start='00:00:00.500',
end='00:00:07.000',
text='Caption #1'
),
Caption(start='00:00:07.000',
end='00:00:11.890',
text=['Caption #2 line 1',
'Caption #2 line 2'
]
)
]
captions[0].comments.append('Comment for the first caption')
captions[1].comments.append('Comment for the second caption')
srt.write(out, captions)
out.seek(0)
self.assertEqual(
out.read(),
textwrap.dedent('''
1
00:00:00,500 --> 00:00:07,000
Caption #1
2
00:00:07,000 --> 00:00:11,890
Caption #2 line 1
Caption #2 line 2
''').strip()
)
|