def test_caption_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_save_updated_identifiers(self): os.makedirs(OUTPUT_DIR) copy(self._get_file('using_identifiers.vtt'), OUTPUT_DIR) vtt = webvtt.read(os.path.join(OUTPUT_DIR, 'using_identifiers.vtt')) vtt.captions[0].identifier = 'first caption' vtt.captions[1].identifier = None vtt.captions[3].identifier = '44' last_caption = Caption('00:00:27.280', '00:00:29.200', 'Caption text #7') last_caption.identifier = 'last caption' vtt.captions.append(last_caption) vtt.save(os.path.join(OUTPUT_DIR, 'new_using_identifiers.vtt')) with open(os.path.join(OUTPUT_DIR, 'new_using_identifiers.vtt'), 'r', encoding='utf-8') as f: lines = [line.rstrip() for line in f.readlines()] expected_lines = [ 'WEBVTT', '', 'first caption', '00:00:00.500 --> 00:00:07.000', 'Caption text #1', '', '00:00:07.000 --> 00:00:11.890', 'Caption text #2', '', '00:00:11.890 --> 00:00:16.320', 'Caption text #3', '', '44', '00:00:16.320 --> 00:00:21.580', 'Caption text #4', '', '00:00:21.580 --> 00:00:23.880', 'Caption text #5', '', '00:00:23.880 --> 00:00:27.280', 'Caption text #6', '', 'last caption', '00:00:27.280 --> 00:00:29.200', 'Caption text #7' ] self.assertListEqual(lines, expected_lines)
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_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_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_caption_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_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_write_captions(self): os.makedirs(OUTPUT_DIR) copy(self._get_file('one_caption.vtt'), OUTPUT_DIR) out = io.StringIO() vtt = webvtt.read(os.path.join(OUTPUT_DIR, 'one_caption.vtt')) new_caption = Caption('00:00:07.000', '00:00:11.890', ['New caption text line1', 'New caption text line2']) vtt.captions.append(new_caption) vtt.write(out) out.seek(0) lines = [line.rstrip() for line in out.readlines()] expected_lines = [ 'WEBVTT', '', '00:00:00.500 --> 00:00:07.000', 'Caption text #1', '', '00:00:07.000 --> 00:00:11.890', 'New caption text line1', 'New caption text line2' ] self.assertListEqual(lines, expected_lines)
def test_create_caption(self): caption = Caption('00:00:00.500', '00:00:07.000', ['Caption test line 1', 'Caption test line 2']) self.assertEqual(caption.start, '00:00:00.500') self.assertEqual(caption.start_in_seconds, 0.5) self.assertEqual(caption.end, '00:00:07.000') self.assertEqual(caption.end_in_seconds, 7) self.assertEqual(caption.lines, ['Caption test line 1', 'Caption test line 2'])
def block_to_sbv(b): i_tsr = b.index(' ') tsr = b[:i_tsr] _text = b[i_tsr + 1:] ts1, ts2 = tsr.split(',') text = _text.split("\\n") return Caption(start=ts1, end=ts2, text=text)
def test_update_text_wrong_type(self): c = Caption(text='Caption line #1') self.assertRaises( AttributeError, setattr, c, 'text', 123 )
def test_caption_receive_text(self): c = Caption(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_save_captions(self): os.makedirs(OUTPUT_DIR) copy(self._get_file('one_caption.vtt'), OUTPUT_DIR) vtt = webvtt.read(os.path.join(OUTPUT_DIR, 'one_caption.vtt')) new_caption = Caption( '00:00:07.000', '00:00:11.890', ['New caption text line1', 'New caption text line2']) vtt.captions.append(new_caption) vtt.save() with open(os.path.join(OUTPUT_DIR, 'one_caption.vtt'), 'r', encoding='utf-8') as f: lines = [line.rstrip() for line in f.readlines()] expected_lines = [ 'WEBVTT', '', '00:00:00.500 --> 00:00:07.000', 'Caption text #1', '', '00:00:07.000 --> 00:00:11.890', 'New caption text line1', 'New caption text line2' ] self.assertListEqual(lines, expected_lines)
return webvtt_sr_model # Check whether WebVTTSRModel raise an RequestException if the uri of caption file is # invalid def test_webvtt_sr_model_request_caption_content(example_webvtt_sr_model): with pytest.raises(RequestException): example_webvtt_sr_model._request_caption_content("invalid-caption-uri") @pytest.mark.parametrize( "captions, expected", [ ( [ Caption(text=">> Start of Dialog 1."), Caption(text="End of Dialog 1."), Caption(text=">> [ APPLAUSE ]"), Caption(text=">> Dialog 2."), ], [ ["Start of Dialog 1.", "End of Dialog 1."], ["[ APPLAUSE ]"], ["Dialog 2."], ], ), ( [ Caption(text=">> Dialog 1."), Caption(text=">> [ ROLL BEING CALLED ]"), Caption(text=">> Dialog 2."),
def test_parse_timestamp(self): caption = Caption(start='02:03:11.890') self.assertEqual(caption.start_in_seconds, 7391.89)
def test_caption_text(self): c = Caption(text=['Caption line #1', 'Caption line #2']) self.assertEqual(c.text, 'Caption line #1\nCaption line #2')