File: test_sbv.py

package info (click to toggle)
python-webvtt 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 640 kB
  • sloc: python: 3,777; makefile: 29; sh: 6
file content (141 lines) | stat: -rw-r--r-- 4,071 bytes parent folder | download
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
import unittest
import textwrap

from webvtt import sbv
from webvtt.errors import MalformedFileError
from webvtt.models import Caption


class TestSBVCueBlock(unittest.TestCase):

    def test_is_valid(self):
        self.assertTrue(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            00:00:00.500,00:00:07.000
            Caption #1
            ''').strip().split('\n'))
            )

        self.assertTrue(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            0:0:0.500,0:0:7.000
            Caption #1
            ''').strip().split('\n'))
            )

        self.assertTrue(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            00:00:00.500,00:00:07.000
            Caption #1 line 1
            Caption #1 line 2
            ''').strip().split('\n'))
            )

        self.assertFalse(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            00:00:00.500 --> 00:00:07.000
            Caption #1
            ''').strip().split('\n'))
            )

        self.assertFalse(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            1
            00:00:00.500,00:00:07.000
            Caption #1
            ''').strip().split('\n'))
            )

        self.assertFalse(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            1
            00:00:00.500,00:00:07.000
            ''').strip().split('\n'))
            )

        self.assertFalse(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            1
            Caption #1
            ''').strip().split('\n'))
            )

        self.assertFalse(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            Caption #1
            ''').strip().split('\n'))
            )

        self.assertFalse(sbv.SBVCueBlock.is_valid(textwrap.dedent('''
            00:00:00.500,00:00:07.000
            ''').strip().split('\n'))
            )

    def test_from_lines(self):
        cue_block = sbv.SBVCueBlock.from_lines(textwrap.dedent('''
                    00:00:00.500,00:00:07.000
                    Caption #1 line 1
                    Caption #1 line 2
                    ''').strip().split('\n')
                    )
        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']
            )

    def test_from_lines_shorter_timestamps(self):
        cue_block = sbv.SBVCueBlock.from_lines(textwrap.dedent('''
                    0:1:2.500,0:1:03.800
                    Caption #1 line 1
                    Caption #1 line 2
                    ''').strip().split('\n')
                    )
        self.assertEqual(
            cue_block.start,
            '0:1:2.500'
            )
        self.assertEqual(
            cue_block.end,
            '0:1:03.800'
            )


class TestSBVModule(unittest.TestCase):

    def test_parse_invalid_format(self):
        self.assertRaises(
            MalformedFileError,
            sbv.parse,
            textwrap.dedent('''
                1
                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 = sbv.parse(
            textwrap.dedent('''
            00:00:00.500,00:00:07.000
            Caption #1

            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'
            )