File: test_webvtt.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 (1414 lines) | stat: -rw-r--r-- 46,224 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
import unittest
import os
import io
import textwrap
import warnings
import tempfile
import pathlib

import webvtt
from webvtt.models import Caption, Style
from webvtt.utils import CODEC_BOMS
from webvtt.errors import MalformedFileError

PATH_TO_SAMPLES = pathlib.Path(__file__).resolve().parent / 'samples'


class TestWebVTT(unittest.TestCase):

    def test_from_string(self):
        vtt = webvtt.from_string(textwrap.dedent("""
            WEBVTT

            00:00:00.500 --> 00:00:07.000
            Caption text #1

            00:00:07.000 --> 00:00:11.890
            Caption text #2 line 1
            Caption text #2 line 2

            00:00:11.890 --> 00:00:16.320
            Caption text #3

            00:00:16.320 --> 00:00:21.580
            Caption text #4
            """).strip()
            )
        self.assertEqual(len(vtt), 4)
        self.assertEqual(
            str(vtt[0]),
            '00:00:00.500 00:00:07.000 Caption text #1'
            )
        self.assertEqual(
            str(vtt[1]),
            r'00:00:07.000 00:00:11.890 Caption text #2 line 1\n'
            'Caption text #2 line 2'
            )
        self.assertEqual(
            str(vtt[2]),
            '00:00:11.890 00:00:16.320 Caption text #3'
            )
        self.assertEqual(
            str(vtt[3]),
            '00:00:16.320 00:00:21.580 Caption text #4'
            )

    def test_write_captions(self):
        out = io.StringIO()
        vtt = webvtt.read(PATH_TO_SAMPLES / 'one_caption.vtt')
        new_caption = Caption(start='00:00:07.000',
                              end='00:00:11.890',
                              text=['New caption text line1',
                                    'New caption text line2'
                                    ]
                              )
        vtt.captions.append(new_caption)
        vtt.write(out)

        out.seek(0)

        self.assertEqual(
            out.read(),
            textwrap.dedent('''
                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
            ''').strip() + '\n'
            )

    def test_write_captions_in_srt(self):
        out = io.StringIO()
        vtt = webvtt.read(PATH_TO_SAMPLES / 'one_caption.vtt')
        new_caption = Caption(start='00:00:07.000',
                              end='00:00:11.890',
                              text=['New caption text line1',
                                    'New caption text line2'
                                    ]
                              )
        vtt.captions.append(new_caption)
        vtt.write(out, format='srt')

        out.seek(0)
        self.assertEqual(
            out.read(),
            textwrap.dedent('''
                1
                00:00:00,500 --> 00:00:07,000
                Caption text #1

                2
                00:00:07,000 --> 00:00:11,890
                New caption text line1
                New caption text line2
            ''').strip()
            )

    def test_write_captions_in_srt_no_cuetags(self):
        """https://github.com/glut23/webvtt-py/issues/56"""
        out = io.StringIO()
        vtt = webvtt.read(PATH_TO_SAMPLES / 'cue_tags.vtt')
        vtt.write(out, format='srt')

        out.seek(0)
        self.assertEqual(
            out.read(),
            textwrap.dedent('''
            1
            00:00:16,500 --> 00:00:18,500
            When the moon hits your eye

            2
            00:00:18,500 --> 00:00:20,500
            Like a big-a pizza pie

            3
            00:00:20,500 --> 00:00:21,500
            That's amore
            ''').strip()
            )

    def test_write_captions_in_unsupported_format(self):
        self.assertRaises(
            ValueError,
            webvtt.WebVTT().write,
            io.StringIO(),
            format='ttt'
            )

    def test_save_captions(self):
        with tempfile.NamedTemporaryFile('w', suffix='.vtt') as f:
            f.write((PATH_TO_SAMPLES / 'one_caption.vtt').read_text())
            f.flush()

            vtt = webvtt.read(f.name)
            new_caption = Caption(start='00:00:07.000',
                                  end='00:00:11.890',
                                  text=['New caption text line1',
                                        'New caption text line2'
                                        ]
                                  )
            vtt.captions.append(new_caption)
            vtt.save()
            f.flush()

            self.assertEqual(
                pathlib.Path(f.name).read_text(),
                textwrap.dedent('''
                    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
                    ''').strip() + '\n'
                )

    def test_srt_conversion(self):
        with tempfile.TemporaryDirectory() as td:
            with open(pathlib.Path(td) / 'one_caption.srt', 'w') as f:
                f.write((PATH_TO_SAMPLES / 'one_caption.srt').read_text())

            webvtt.from_srt(
                pathlib.Path(td) / 'one_caption.srt'
                ).save()

            self.assertTrue(
                os.path.exists(pathlib.Path(td) / 'one_caption.vtt')
                )
            self.assertEqual(
                (pathlib.Path(td) / 'one_caption.vtt').read_text(),
                textwrap.dedent('''
                    WEBVTT

                    00:00:00.500 --> 00:00:07.000
                    Caption text #1
                    ''').strip() + '\n'
                )

    def test_sbv_conversion(self):
        with tempfile.TemporaryDirectory() as td:
            with open(pathlib.Path(td) / 'two_captions.sbv', 'w') as f:
                f.write(
                    (PATH_TO_SAMPLES / 'two_captions.sbv').read_text()
                    )

            webvtt.from_sbv(
                pathlib.Path(td) / 'two_captions.sbv'
                ).save()

            self.assertTrue(
                os.path.exists(pathlib.Path(td) / 'two_captions.vtt')
                )
            self.assertEqual(
                (pathlib.Path(td) / 'two_captions.vtt').read_text(),
                textwrap.dedent('''
                    WEBVTT

                    00:00:00.378 --> 00:00:11.378
                    Caption text #1

                    00:00:11.378 --> 00:00:12.305
                    Caption text #2 (line 1)
                    Caption text #2 (line 2)
                    ''').strip() + '\n'
                )

    def test_save_to_other_location(self):
        with tempfile.TemporaryDirectory() as td:
            webvtt.read(
                PATH_TO_SAMPLES / 'one_caption.vtt'
                ).save(td)

            self.assertTrue(
                os.path.exists(pathlib.Path(td) / 'one_caption.vtt')
                )

    def test_save_specific_filename(self):
        with tempfile.TemporaryDirectory() as td:
            webvtt.read(
                PATH_TO_SAMPLES / 'one_caption.vtt'
                ).save(
                    pathlib.Path(td) / 'one_caption_new.vtt'
                )

            self.assertTrue(
                os.path.exists(pathlib.Path(td) / 'one_caption_new.vtt')
                )

    def test_save_specific_filename_no_extension(self):
        with tempfile.TemporaryDirectory() as td:
            webvtt.read(
                PATH_TO_SAMPLES / 'one_caption.vtt'
                ).save(
                    pathlib.Path(td) / 'one_caption_new'
                )

            self.assertTrue(
                os.path.exists(pathlib.Path(td) / 'one_caption_new.vtt')
                )

    def test_from_buffer(self):
        with open(PATH_TO_SAMPLES / 'sample.vtt', 'r', encoding='utf-8') as f:
            vtt = webvtt.from_buffer(f)
            self.assertEqual(len(vtt), 16)
            self.assertEqual(
                str(vtt),
                textwrap.dedent('''
                    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
                    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
                    00:00:27.280 00:00:30.280 Caption text #7
                    00:00:30.280 00:00:36.510 Caption text #8
                    00:00:36.510 00:00:38.870 Caption text #9
                    00:00:38.870 00:00:45.000 Caption text #10
                    00:00:45.000 00:00:47.000 Caption text #11
                    00:00:47.000 00:00:50.970 Caption text #12
                    00:00:50.970 00:00:54.440 Caption text #13
                    00:00:54.440 00:00:58.600 Caption text #14
                    00:00:58.600 00:01:01.350 Caption text #15
                    00:01:01.350 00:01:04.300 Caption text #16
                    '''
                    ).strip())

    def test_deprecated_read_buffer(self):
        with open(PATH_TO_SAMPLES / 'sample.vtt', 'r', encoding='utf-8') as f:
            with warnings.catch_warnings(record=True) as warns:
                warnings.simplefilter('always')
                vtt = webvtt.read_buffer(f)

                self.assertIsInstance(vtt.captions, list)
                self.assertEqual(
                    'Deprecated: use from_buffer instead.',
                    str(warns[-1].message)
                    )

    def test_read_memory_buffer(self):
        buffer = io.StringIO(
            (PATH_TO_SAMPLES / 'sample.vtt').read_text()
            )

        self.assertIsInstance(
            webvtt.from_buffer(buffer).captions,
            list
            )

    def test_read_memory_buffer_carriage_return(self):
        """https://github.com/glut23/webvtt-py/issues/29"""
        buffer = io.StringIO(textwrap.dedent('''\
            WEBVTT\r
            \r
            00:00:00.500 --> 00:00:07.000\r
            Caption text #1\r
            \r
            00:00:07.000 --> 00:00:11.890\r
            Caption text #2\r
            \r
            00:00:11.890 --> 00:00:16.320\r
            Caption text #3\r
        ''')
        )

        self.assertEqual(
            len(webvtt.from_buffer(buffer).captions),
            3
            )

    def test_read_malformed_buffer(self):
        malformed_payloads = ['', 'MOCK MALFORMED CONTENT']
        for payload in malformed_payloads:
            buffer = io.StringIO(payload)
            with self.assertRaises(MalformedFileError):
                webvtt.from_buffer(buffer)

    def test_read_buffer_for_vtt_content(self):
        buffer = io.StringIO(textwrap.dedent('''\
            WEBVTT\r
            \r
            00:00:00.500 --> 00:00:07.000\r
            Caption text #1\r
            \r
            00:00:07.000 --> 00:00:11.890\r
            Caption text #2\r
            \r
            00:00:11.890 --> 00:00:16.320\r
            Caption text #3\r
            ''')
            )
        vtt = webvtt.from_buffer(buffer, format='vtt')
        self.assertEqual(len(vtt), 3)

        self.assertEqual(
            str(vtt[0]),
            '00:00:00.500 00:00:07.000 Caption text #1'
            )
        self.assertEqual(
            str(vtt[1]),
            '00:00:07.000 00:00:11.890 Caption text #2'
            )
        self.assertEqual(
            str(vtt[2]),
            '00:00:11.890 00:00:16.320 Caption text #3'
            )

    def test_read_buffer_for_srt_content(self):
        buffer = io.StringIO(textwrap.dedent('''\
            0\r
            00:00:00,500 --> 00:00:07,000\r
            Caption text #1\r
            \r
            1\r
            00:00:07,000 --> 00:00:11,890\r
            Caption text #2\r
            \r
            2\r
            00:00:11,890 --> 00:00:16,320\r
            Caption text #3\r
            ''')
            )
        vtt = webvtt.from_buffer(buffer, format='srt')
        self.assertEqual(len(vtt), 3)

        self.assertEqual(
            str(vtt[0]),
            '00:00:00.500 00:00:07.000 Caption text #1'
            )
        self.assertEqual(
            str(vtt[1]),
            '00:00:07.000 00:00:11.890 Caption text #2'
            )
        self.assertEqual(
            str(vtt[2]),
            '00:00:11.890 00:00:16.320 Caption text #3'
            )

    def test_read_buffer_for_sbv_content(self):
        buffer = io.StringIO(textwrap.dedent('''\
            00:00:00.500,00:00:07.000\r
            Caption text #1\r
            \r
            00:00:07.000,00:00:11.890\r
            Caption text #2\r
            \r
            00:00:11.890,00:00:16.320\r
            Caption text #3\r
            ''')
            )
        vtt = webvtt.from_buffer(buffer, format='sbv')
        self.assertEqual(len(vtt), 3)

        self.assertEqual(
            str(vtt[0]),
            '00:00:00.500 00:00:07.000 Caption text #1'
            )
        self.assertEqual(
            str(vtt[1]),
            '00:00:07.000 00:00:11.890 Caption text #2'
            )
        self.assertEqual(
            str(vtt[2]),
            '00:00:11.890 00:00:16.320 Caption text #3'
            )

    def test_read_buffer_unsupported_format(self):
        self.assertRaises(
            ValueError,
            webvtt.from_buffer,
            io.StringIO(),
            format='ttt'
            )

    def test_read_bytesio_buffer_for_srt_content(self):
        buffer = io.BytesIO(textwrap.dedent('''\
            0\r
            00:00:00,500 --> 00:00:07,000\r
            Caption text #1\r
            \r
            1\r
            00:00:07,000 --> 00:00:11,890\r
            Caption text #2\r
            \r
            2\r
            00:00:11,890 --> 00:00:16,320\r
            Caption text #3\r
            ''').encode('utf-8')
            )
        vtt = webvtt.from_buffer(buffer, format='srt')
        self.assertEqual(len(vtt), 3)

        self.assertEqual(
            str(vtt[0]),
            '00:00:00.500 00:00:07.000 Caption text #1'
            )
        self.assertEqual(
            str(vtt[1]),
            '00:00:07.000 00:00:11.890 Caption text #2'
            )
        self.assertEqual(
            str(vtt[2]),
            '00:00:11.890 00:00:16.320 Caption text #3'
            )

    def test_captions(self):
        captions = webvtt.read(PATH_TO_SAMPLES / 'sample.vtt').captions
        self.assertIsInstance(
            captions,
            list
            )
        self.assertEqual(len(captions), 16)

    def test_sequence_iteration(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'sample.vtt')
        self.assertIsInstance(vtt[0], Caption)
        self.assertEqual(len(vtt), len(vtt.captions))

    def test_save_no_filename(self):
        self.assertRaises(
            webvtt.errors.MissingFilenameError,
            webvtt.WebVTT().save
            )

    def test_save_with_path_to_dir_no_filename(self):
        with tempfile.TemporaryDirectory() as td:
            self.assertRaises(
                webvtt.errors.MissingFilenameError,
                webvtt.WebVTT().save,
                td
                )

    def test_set_styles_from_text(self):
        style = Style('::cue(b) {\n  color: peachpuff;\n}')
        self.assertListEqual(
            style.lines,
            ['::cue(b) {', '  color: peachpuff;', '}']
            )

    def test_save_identifiers(self):
        with tempfile.NamedTemporaryFile('w', suffix='.vtt') as f:
            webvtt.read(
                PATH_TO_SAMPLES / 'using_identifiers.vtt'
                ).save(
                    f.name
                    )

            self.assertListEqual(
                pathlib.Path(f.name).read_text().splitlines(),
                [
                    'WEBVTT',
                    '',
                    '00:00:00.500 --> 00:00:07.000',
                    'Caption text #1',
                    '',
                    'second caption',
                    '00:00:07.000 --> 00:00:11.890',
                    'Caption text #2',
                    '',
                    '00:00:11.890 --> 00:00:16.320',
                    'Caption text #3',
                    '',
                    '4',
                    '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'
                    ]
                )

    def test_save_updated_identifiers(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'using_identifiers.vtt')
        vtt.captions[0].identifier = 'first caption'
        vtt.captions[1].identifier = None
        vtt.captions[3].identifier = '44'
        last_caption = Caption(start='00:00:27.280',
                               end='00:00:29.200',
                               text='Caption text #7'
                               )
        last_caption.identifier = 'last caption'
        vtt.captions.append(last_caption)

        with tempfile.NamedTemporaryFile('w', suffix='.vtt') as f:
            vtt.save(f.name)

            self.assertListEqual(
                pathlib.Path(f.name).read_text().splitlines(),
                [
                    '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'
                    ]
                )

    def test_content_formatting(self):
        """
        Verify that content property returns the correctly formatted webvtt.
        """
        captions = [
            Caption(start='00:00:00.500',
                    end='00:00:07.000',
                    text=['Caption test line 1', 'Caption test line 2']
                    ),
            Caption(start='00:00:08.000',
                    end='00:00:15.000',
                    text=['Caption test line 3', 'Caption test line 4']
                    ),
            ]

        self.assertEqual(
            webvtt.WebVTT(captions=captions).content,
            textwrap.dedent("""
                    WEBVTT

                    00:00:00.500 --> 00:00:07.000
                    Caption test line 1
                    Caption test line 2

                    00:00:08.000 --> 00:00:15.000
                    Caption test line 3
                    Caption test line 4
                    """).strip() + '\n'
            )

    def test_repr(self):
        test_file = PATH_TO_SAMPLES / 'sample.vtt'
        self.assertEqual(
            repr(webvtt.read(test_file)),
            f"<WebVTT file='{test_file}' encoding='utf-8'>"
            )

    def test_str(self):
        self.assertEqual(
            str(webvtt.read(PATH_TO_SAMPLES / 'sample.vtt')),
            textwrap.dedent("""
                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
                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
                00:00:27.280 00:00:30.280 Caption text #7
                00:00:30.280 00:00:36.510 Caption text #8
                00:00:36.510 00:00:38.870 Caption text #9
                00:00:38.870 00:00:45.000 Caption text #10
                00:00:45.000 00:00:47.000 Caption text #11
                00:00:47.000 00:00:50.970 Caption text #12
                00:00:50.970 00:00:54.440 Caption text #13
                00:00:54.440 00:00:58.600 Caption text #14
                00:00:58.600 00:01:01.350 Caption text #15
                00:01:01.350 00:01:04.300 Caption text #16
            """).strip()
            )

    def test_parse_invalid_file(self):
        self.assertRaises(
            MalformedFileError,
            webvtt.read,
            PATH_TO_SAMPLES / 'invalid.vtt'
            )

    def test_file_not_found(self):
        self.assertRaises(
            FileNotFoundError,
            webvtt.read,
            'nowhere'
            )

    def test_total_length(self):
        self.assertEqual(
            webvtt.read(PATH_TO_SAMPLES / 'sample.vtt').total_length,
            64
            )

    def test_total_length_no_captions(self):
        self.assertEqual(
            webvtt.WebVTT().total_length,
            0
            )

    def test_parse_empty_file(self):
        self.assertRaises(
            MalformedFileError,
            webvtt.read,
            PATH_TO_SAMPLES / 'empty.vtt'
            )

    def test_parse_invalid_timeframe_line(self):
        good_captions = len(
            webvtt.read(PATH_TO_SAMPLES / 'invalid_timeframe.vtt').captions
            )
        self.assertEqual(good_captions, 6)

    def test_parse_invalid_timeframe_in_cue_text(self):
        vtt = webvtt.read(
            PATH_TO_SAMPLES / 'invalid_timeframe_in_cue_text.vtt'
            )
        self.assertEqual(2, len(vtt.captions))
        self.assertEqual('Caption text #3', vtt.captions[1].text)

    def test_parse_get_caption_data(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'one_caption.vtt')
        self.assertEqual(vtt.captions[0].start_in_seconds, 0)
        self.assertEqual(vtt.captions[0].start, '00:00:00.500')
        self.assertEqual(vtt.captions[0].end_in_seconds, 7)
        self.assertEqual(vtt.captions[0].end, '00:00:07.000')
        self.assertEqual(vtt.captions[0].lines[0], 'Caption text #1')
        self.assertEqual(len(vtt.captions[0].lines), 1)

    def test_caption_without_timeframe(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'missing_timeframe.vtt')
        self.assertEqual(len(vtt.captions), 6)

    def test_caption_without_cue_text(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'missing_caption_text.vtt')
        self.assertEqual(len(vtt.captions), 4)

    def test_timestamps_format(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'sample.vtt')
        self.assertEqual(vtt.captions[2].start, '00:00:11.890')
        self.assertEqual(vtt.captions[2].end, '00:00:16.320')

    def test_parse_timestamp(self):
        self.assertEqual(
            Caption(start='02:03:11.890').start_in_seconds,
            7391
            )

    def test_captions_attribute(self):
        self.assertListEqual(webvtt.WebVTT().captions, [])

    def test_metadata_headers(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'metadata_headers.vtt')
        self.assertEqual(len(vtt.captions), 2)

    def test_metadata_headers_multiline(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'metadata_headers_multiline.vtt')
        self.assertEqual(len(vtt.captions), 2)

    def test_parse_identifiers(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'using_identifiers.vtt')
        self.assertEqual(len(vtt.captions), 6)

        self.assertEqual(vtt.captions[1].identifier, 'second caption')
        self.assertEqual(vtt.captions[2].identifier, None)
        self.assertEqual(vtt.captions[3].identifier, '4')

    def test_parse_comments(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'comments.vtt')
        self.assertEqual(len(vtt.captions), 3)
        self.assertListEqual(
            vtt.captions[0].lines,
            ['- Ta en kopp varmt te.',
             '- Det är inte varmt.']
            )
        self.assertListEqual(
            vtt.captions[0].comments,
            []
            )
        self.assertListEqual(
            vtt.captions[1].comments,
            []
            )
        self.assertEqual(
            vtt.captions[2].text,
            '- Ta en kopp'
            )
        self.assertListEqual(
            vtt.captions[2].comments,
            ['This last line may not translate well.']
            )
        self.assertListEqual(
            vtt.header_comments,
            ['This translation was done by Kyle so that\n'
             'some friends can watch it with their parents.'
             ]
            )
        self.assertListEqual(
            vtt.footer_comments,
            ['end of file']
            )

    def test_parse_styles(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'styles.vtt')
        self.assertEqual(len(vtt.captions), 1)
        self.assertEqual(
            vtt.styles[0].text,
            '::cue {\n  background-image: linear-gradient(to bottom, '
            'dimgray, lightgray);\n  color: papayawhip;\n}'
            )

    def test_parse_styles_with_comments(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'styles_with_comments.vtt')
        self.assertEqual(len(vtt.captions), 1)
        self.assertEqual(len(vtt.styles), 2)
        self.assertEqual(
            vtt.styles[0].comments,
            []
            )
        self.assertEqual(
            vtt.styles[0].text,
            '::cue {\n'
            '  background-image: linear-gradient(to bottom, dimgray, '
            'lightgray);\n'
            '  color: papayawhip;\n'
            '}'
            )
        self.assertEqual(
            vtt.styles[1].comments,
            ['This is the second block of styles',
             'Multiline comment for the same\nsecond block of styles'
             ]
            )
        self.assertEqual(
            vtt.styles[1].text,
            '::cue(b) {\n'
            '  color: peachpuff;\n'
            '}'
            )
        self.assertListEqual(
            vtt.header_comments,
            ['Sample of comments with styles']
            )
        self.assertListEqual(
            vtt.footer_comments,
            []
            )

    def test_multiple_comments_everywhere(self):
        vtt = webvtt.WebVTT.from_string(textwrap.dedent("""
            WEBVTT

            NOTE Test file

            NOTE this file is testing multiple comments
            in different places

            STYLE
            ::cue {
              background-image: linear-gradient(to bottom, dimgray, lightgray);
              color: papayawhip;
            }

            NOTE this style uses nice color

            NOTE check how it looks before proceeding

            STYLE
            ::cue(b) {
              color: peachpuff;
            }

            00:00:00.500 --> 00:00:07.000
            Caption text #1

            NOTE next caption has two lines

            NOTE needs review

            00:00:07.000 --> 00:00:11.890
            Caption text #2 line 1
            Caption text #2 line 2

            00:00:11.890 --> 00:00:16.320
            Caption text #3

            00:00:16.320 --> 00:00:21.580
            Caption text #4

            NOTE Copyright 2024

            NOTE this is the end of the file
            """).strip()
            )

        self.assertListEqual(
            vtt.header_comments,
            ['Test file',
             'this file is testing multiple comments\nin different places'
             ]
            )
        self.assertListEqual(
            vtt.footer_comments,
            ['Copyright 2024',
             'this is the end of the file'
             ]
            )
        self.assertListEqual(
            vtt.captions[0].comments,
            []
            )
        self.assertListEqual(
            vtt.captions[1].comments,
            ['next caption has two lines',
             'needs review'
             ]
            )
        self.assertListEqual(
            vtt.captions[2].comments,
            []
            )
        self.assertListEqual(
            vtt.captions[3].comments,
            []
            )
        self.assertListEqual(
            vtt.styles[0].comments,
            []
            )
        self.assertListEqual(
            vtt.styles[1].comments,
            ['this style uses nice color',
             'check how it looks before proceeding'
             ]
            )

    def test_comments_in_new_file(self):
        with tempfile.NamedTemporaryFile('r', suffix='.vtt') as f:
            vtt = webvtt.WebVTT()
            vtt.header_comments.append('This is a header comment')
            vtt.header_comments.append(
                'where we can see a\ntwo line comment'
                )
            vtt.styles.append(
                Style('::cue(b) {\n  color: peachpuff;\n}')
                )
            style = Style('::cue {\n  color: papayawhip;\n}')
            style.comments.append('Another style to test\nthe look and feel')
            style.comments.append('Please check')
            vtt.styles.append(style)
            vtt.captions.append(
                Caption(start='00:00:00.500',
                        end='00:00:07.000',
                        text='Caption #1',
                        )
                )
            caption = Caption(start='00:00:07.000',
                              end='00:00:11.890',
                              text='Caption #2'
                              )
            caption.comments.append(
                'Second caption may be a bit off\nand needs checking'
                )
            caption.comments.append('Confirm if it displays correctly')
            vtt.captions.append(caption)
            vtt.footer_comments.append('This is a footer comment')
            vtt.footer_comments.append(
                'where we can also see a\ntwo line comment'
                )

            vtt.save(f.name)
            self.assertEqual(
                f.read(),
                textwrap.dedent('''
                    WEBVTT

                    NOTE This is a header comment

                    NOTE
                    where we can see a
                    two line comment

                    STYLE
                    ::cue(b) {
                      color: peachpuff;
                    }

                    NOTE
                    Another style to test
                    the look and feel

                    NOTE Please check

                    STYLE
                    ::cue {
                      color: papayawhip;
                    }

                    00:00:00.500 --> 00:00:07.000
                    Caption #1

                    NOTE
                    Second caption may be a bit off
                    and needs checking

                    NOTE Confirm if it displays correctly

                    00:00:07.000 --> 00:00:11.890
                    Caption #2

                    NOTE This is a footer comment

                    NOTE
                    where we can also see a
                    two line comment
                    ''').strip()
                )

    def test_clean_cue_tags(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'cue_tags.vtt')
        self.assertEqual(
            vtt.captions[1].text,
            'Like a big-a pizza pie'
            )
        self.assertEqual(
            vtt.captions[2].text,
            'That\'s amore'
            )

    def test_parse_captions_with_bom(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'captions_with_bom.vtt')
        self.assertEqual(len(vtt.captions), 4)

    def test_empty_lines_are_not_included_in_result(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'netflix_chicas_del_cable.vtt')
        self.assertEqual(vtt.captions[0].text, "[Alba] En 1928,")
        self.assertEqual(
            vtt.captions[-2].text,
            "Diez años no son suficientes\npara olvidarte..."
            )

    def test_can_parse_youtube_dl_files(self):
        vtt = webvtt.read(PATH_TO_SAMPLES / 'youtube_dl.vtt')
        self.assertEqual(
            "this will happen is I'm telling\n ",
            vtt.captions[2].text
            )

    def test_parse_voice_spans(self):
        vtt = webvtt.from_string(textwrap.dedent("""
            WEBVTT

            00:00:00.000 --> 00:00:00.800
            <v.quiet.slow Lisa Simpson>Knock knock</v>

            00:00:02.100 --> 00:00:06.500
            <v Homer Simpson>Who's there?</v>

            00:00:10.530 --> 00:00:11.090
            <v.loud Lisa Simpson>Atish</v>
            """).strip()
            )
        self.assertEqual(len(vtt), 3)
        self.assertEqual(
            str(vtt[0]),
            '00:00:00.000 00:00:00.800 Knock knock'
            )
        self.assertEqual(
            vtt[0].voice,
            'Lisa Simpson'
            )
        self.assertEqual(
            str(vtt[1]),
            '00:00:02.100 00:00:06.500 Who\'s there?'
            )
        self.assertEqual(
            vtt[1].voice,
            'Homer Simpson'
            )
        self.assertEqual(
            str(vtt[2]),
            '00:00:10.530 00:00:11.090 Atish'
            )
        self.assertEqual(
            vtt[2].voice,
            'Lisa Simpson'
            )

    def test_parse_caption_not_a_voice_span(self):
        vtt = webvtt.from_string(textwrap.dedent("""
            WEBVTT

            00:00:00.000 --> 00:00:00.800
            <v Not an actual voice span here
            """).strip()
            )
        self.assertEqual(len(vtt), 1)
        self.assertEqual(
            str(vtt[0]),
            '00:00:00.000 00:00:00.800 <v Not an actual voice span here'
            )
        self.assertIsNone(vtt[0].voice)


class TestParseSRT(unittest.TestCase):

    def test_parse_empty_file(self):
        self.assertRaises(
            webvtt.errors.MalformedFileError,
            webvtt.from_srt,
            # We reuse this file as it is empty and serves the purpose.
            PATH_TO_SAMPLES / 'empty.vtt'
            )

    def test_invalid_format(self):
        for i in range(1, 5):
            self.assertRaises(
                MalformedFileError,
                webvtt.from_srt,
                PATH_TO_SAMPLES / f'invalid_format{i}.srt'
                )

    def test_total_length(self):
        self.assertEqual(
            webvtt.from_srt(PATH_TO_SAMPLES / 'sample.srt').total_length,
            23
            )

    def test_parse_captions(self):
        self.assertTrue(
            webvtt.from_srt(PATH_TO_SAMPLES / 'sample.srt').captions
            )

    def test_missing_timeframe_line(self):
        self.assertEqual(
            len(webvtt.from_srt(
                PATH_TO_SAMPLES / 'missing_timeframe.srt').captions
                ),
            4
            )

    def test_empty_caption_text(self):
        self.assertTrue(
            webvtt.from_srt(
                PATH_TO_SAMPLES / 'missing_caption_text.srt').captions
            )

    def test_empty_gets_removed(self):
        captions = webvtt.from_srt(
            PATH_TO_SAMPLES / 'missing_caption_text.srt'
            ).captions
        self.assertEqual(len(captions), 4)

    def test_invalid_timestamp(self):
        self.assertEqual(
            len(webvtt.from_srt(
                PATH_TO_SAMPLES / 'invalid_timeframe.srt'
                ).captions),
            4
            )

    def test_timestamps_format(self):
        vtt = webvtt.from_srt(PATH_TO_SAMPLES / 'sample.srt')
        self.assertEqual(vtt.captions[2].start, '00:00:11.890')
        self.assertEqual(vtt.captions[2].end, '00:00:16.320')

    def test_parse_get_caption_data(self):
        vtt = webvtt.from_srt(PATH_TO_SAMPLES / 'one_caption.srt')
        self.assertEqual(vtt.captions[0].start_in_seconds, 0)
        self.assertEqual(vtt.captions[0].start, '00:00:00.500')
        self.assertEqual(vtt.captions[0].end_in_seconds, 7)
        self.assertEqual(vtt.captions[0].end, '00:00:07.000')
        self.assertEqual(vtt.captions[0].lines[0], 'Caption text #1')
        self.assertEqual(len(vtt.captions[0].lines), 1)


class TestParseSBV(unittest.TestCase):

    def test_parse_empty_file(self):
        self.assertRaises(
            MalformedFileError,
            webvtt.from_sbv,
            # We reuse this file as it is empty and serves the purpose.
            PATH_TO_SAMPLES / 'empty.vtt'
            )

    def test_invalid_format(self):
        self.assertRaises(
            MalformedFileError,
            webvtt.from_sbv,
            PATH_TO_SAMPLES / 'invalid_format.sbv'
            )

    def test_total_length(self):
        self.assertEqual(
            webvtt.from_sbv(PATH_TO_SAMPLES / 'sample.sbv').total_length,
            16
            )

    def test_parse_captions(self):
        self.assertEqual(
            len(webvtt.from_sbv(PATH_TO_SAMPLES / 'sample.sbv').captions),
            5
            )

    def test_missing_timeframe_line(self):
        self.assertEqual(
            len(webvtt.from_sbv(
                PATH_TO_SAMPLES / 'missing_timeframe.sbv'
                ).captions),
            4
            )

    def test_missing_caption_text(self):
        self.assertTrue(
            webvtt.from_sbv(
                PATH_TO_SAMPLES / 'missing_caption_text.sbv'
                ).captions
            )

    def test_invalid_timestamp(self):
        self.assertEqual(
            len(webvtt.from_sbv(
                PATH_TO_SAMPLES / 'invalid_timeframe.sbv'
                ).captions),
            4
            )

    def test_timestamps_format(self):
        vtt = webvtt.from_sbv(PATH_TO_SAMPLES / 'sample.sbv')
        self.assertEqual(vtt.captions[1].start, '00:00:11.378')
        self.assertEqual(vtt.captions[1].end, '00:00:12.305')

    def test_timestamps_in_seconds(self):
        vtt = webvtt.from_sbv(PATH_TO_SAMPLES / 'sample.sbv')
        self.assertEqual(vtt.captions[1].start_in_seconds, 11)
        self.assertEqual(vtt.captions[1].end_in_seconds, 12)

    def test_get_caption_text(self):
        vtt = webvtt.from_sbv(PATH_TO_SAMPLES / 'sample.sbv')
        self.assertEqual(vtt.captions[1].text, 'Caption text #2')

    def test_get_caption_text_multiline(self):
        vtt = webvtt.from_sbv(PATH_TO_SAMPLES / 'sample.sbv')
        self.assertEqual(
            vtt.captions[2].text,
            'Caption text #3 (line 1)\nCaption text #3 (line 2)'
            )
        self.assertListEqual(
            vtt.captions[2].lines,
            ['Caption text #3 (line 1)', 'Caption text #3 (line 2)']
            )

    def test_convert_from_srt_to_vtt_and_back_gives_same_file(self):
        with tempfile.NamedTemporaryFile('w', suffix='.srt') as f:
            webvtt.from_srt(
                PATH_TO_SAMPLES / 'sample.srt'
                ).save_as_srt(f.name)

            self.assertEqual(
                pathlib.Path(PATH_TO_SAMPLES / 'sample.srt').read_text(),
                pathlib.Path(f.name).read_text()
                )

    def test_save_file_with_bom(self):
        with tempfile.NamedTemporaryFile('r', suffix='.vtt') as f:
            webvtt.read(
                PATH_TO_SAMPLES / 'one_caption.vtt'
                ).save(f.name, add_bom=True)
            self.assertEqual(
                f.read(),
                textwrap.dedent(f'''
                    {CODEC_BOMS['utf-8'].decode()}WEBVTT

                    00:00:00.500 --> 00:00:07.000
                    Caption text #1
                    ''').strip() + '\n'
                )

    def test_save_file_with_bom_keeps_bom(self):
        with tempfile.NamedTemporaryFile('r', suffix='.vtt') as f:
            webvtt.read(
                PATH_TO_SAMPLES / 'captions_with_bom.vtt'
            ).save(f.name)
            self.assertEqual(
                f.read(),
                textwrap.dedent(f'''
                    {CODEC_BOMS['utf-8'].decode()}WEBVTT

                    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

                    00:00:16.320 --> 00:00:21.580
                    Caption text #4
                    ''').strip() + '\n'
                )

    def test_save_file_with_bom_removes_bom_if_requested(self):
        with tempfile.NamedTemporaryFile('r', suffix='.vtt') as f:
            webvtt.read(
                PATH_TO_SAMPLES / 'captions_with_bom.vtt'
            ).save(f.name, add_bom=False)
            self.assertEqual(
                f.read(),
                textwrap.dedent(f'''
                    WEBVTT

                    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

                    00:00:16.320 --> 00:00:21.580
                    Caption text #4
                    ''').strip() + '\n'
                )

    def test_save_file_with_encoding(self):
        with tempfile.NamedTemporaryFile('rb', suffix='.vtt') as f:
            webvtt.read(
                PATH_TO_SAMPLES / 'one_caption.vtt'
            ).save(f.name,
                   encoding='utf-32-le'
                   )
            self.assertEqual(
                f.read().decode('utf-32-le'),
                textwrap.dedent('''
                    WEBVTT

                    00:00:00.500 --> 00:00:07.000
                    Caption text #1
                    ''').strip() + '\n'
                )

    def test_save_file_with_encoding_and_bom(self):
        with tempfile.NamedTemporaryFile('rb', suffix='.vtt') as f:
            webvtt.read(
                PATH_TO_SAMPLES / 'one_caption.vtt'
            ).save(f.name,
                   encoding='utf-32-le',
                   add_bom=True
                   )
            self.assertEqual(
                f.read().decode('utf-32-le'),
                textwrap.dedent(f'''
                    {CODEC_BOMS['utf-32-le'].decode('utf-32-le')}WEBVTT

                    00:00:00.500 --> 00:00:07.000
                    Caption text #1
                    ''').strip() + '\n'
                )

    def test_save_new_file_utf_8_default_encoding_no_bom(self):
        with tempfile.NamedTemporaryFile('r', suffix='.vtt') as f:
            vtt = webvtt.WebVTT()
            vtt.captions.append(
                Caption(start='00:00:00.500',
                        end='00:00:07.000',
                        text='Caption text #1'
                        )
                )
            vtt.save(f.name)
            self.assertEqual(vtt.encoding, 'utf-8')
            self.assertEqual(
                f.read(),
                textwrap.dedent(f'''
                    WEBVTT

                    00:00:00.500 --> 00:00:07.000
                    Caption text #1
                    ''').strip() + '\n'
                )

    def test_save_new_file_utf_8_default_encoding_with_bom(self):
        with tempfile.NamedTemporaryFile('r', suffix='.vtt') as f:
            vtt = webvtt.WebVTT()
            vtt.captions.append(
                Caption(start='00:00:00.500',
                        end='00:00:07.000',
                        text='Caption text #1'
                        )
                )
            vtt.save(f.name,
                     add_bom=True
                     )
            self.assertEqual(vtt.encoding, 'utf-8')
            self.assertEqual(
                f.read(),
                textwrap.dedent(f'''
                    {CODEC_BOMS['utf-8'].decode()}WEBVTT

                    00:00:00.500 --> 00:00:07.000
                    Caption text #1
                    ''').strip() + '\n'
                )

    def test_iter_slice(self):
        vtt = webvtt.read(
                PATH_TO_SAMPLES / 'sample.vtt'
                )
        slice_of_captions = vtt.iter_slice(start='00:00:11.000',
                                           end='00:00:27.000'
                                           )
        for expected_caption in (vtt.captions[2],
                                 vtt.captions[3],
                                 vtt.captions[4]
                                 ):
            self.assertIs(expected_caption, next(slice_of_captions))

        with self.assertRaises(StopIteration):
            next(slice_of_captions)

    def test_iter_slice_no_start_time(self):
        vtt = webvtt.read(
                PATH_TO_SAMPLES / 'sample.vtt'
                )
        slice_of_captions = vtt.iter_slice(end='00:00:27.000')
        for expected_caption in (vtt.captions[0],
                                 vtt.captions[1],
                                 vtt.captions[2],
                                 vtt.captions[3],
                                 vtt.captions[4]
                                 ):
            self.assertIs(expected_caption, next(slice_of_captions))

        with self.assertRaises(StopIteration):
            next(slice_of_captions)

    def test_iter_slice_no_end_time(self):
        vtt = webvtt.read(
                PATH_TO_SAMPLES / 'sample.vtt'
                )
        slice_of_captions = vtt.iter_slice(start='00:00:47.000')
        for expected_caption in (vtt.captions[11],
                                 vtt.captions[12],
                                 vtt.captions[13],
                                 vtt.captions[14],
                                 vtt.captions[15]
                                 ):
            self.assertIs(expected_caption, next(slice_of_captions))

        with self.assertRaises(StopIteration):
            next(slice_of_captions)