def test_no_space_after_colon_in_metadata_section(): # see issue #14 ref = SSAFile.from_string(AEGISUB_PROJECT_GARBAGE_FILE) subs = SSAFile.from_string(AEGISUB_PROJECT_GARBAGE_FILE_WITHOUT_SPACE_AFTER_COLON) assert ref.equals(subs) assert ref.aegisub_project == subs.aegisub_project
def test_simple_parsing(): test_input1 = "[123][456] Line 1" subs1 = SSAFile.from_string(test_input1) assert len(subs1) == 1 assert subs1[0] == SSAEvent(start=make_time(ms=12300), end=make_time(ms=45600), text="Line 1") test_input2 = "[123][456] / Line 1| Line 2/2" subs2 = SSAFile.from_string(test_input2) assert len(subs2) == 1 assert subs2[0] == SSAEvent(start=make_time(ms=12300), end=make_time(ms=45600), text="{\i1}Line 1{\i0}\\NLine2/2") test_input3 = dedent(""" [123][456] Line 1 [321][456] / Line 2| Line 3 (123)(456)This line should not be parsed This line should also not be parsed [789][1234] /Line 4""") subs3 = SSAFile.from_string(test_input3) assert len(subs3) == 3 assert subs3[0] == SSAEvent(start=make_time(ms=12300), end=make_time(ms=45600), text="Line 1") assert subs3[1] == SSAEvent(start=make_time(ms=32100), end=make_time(ms=45600), text="{\i1}Line 2{\i0}\\NLine 3") assert subs3[2] == SSAEvent(start=make_time(ms=78900), end=make_time(ms=123400), text="{\i1}Line 4{\i0}")
def test_simple_read(): ref = build_ref() subs1 = SSAFile.from_string(SIMPLE_ASS_REF) subs2 = SSAFile.from_string(SIMPLE_SSA_REF) assert ref.equals(subs1) assert ref.equals(subs2)
def test_keep_unknown_html_tags(): # see issue #26 text = dedent("""\ 1 00:00:10,500 --> 00:00:13,000 <i>Elephant's <sub>Little</sub> Dream</i> 2 00:00:15,000 --> 00:00:18,000 <font color="cyan">At the left we can see...</font> """) ref_default = SSAFile() ref_default.append(SSAEvent(start=make_time(s=10.5), end=make_time(s=13), text="{\\i1}Elephant's Little Dream{\\i0}")) ref_default.append(SSAEvent(start=make_time(s=15), end=make_time(s=18), text="At the left we can see...")) ref_keep = SSAFile() ref_keep.append(SSAEvent(start=make_time(s=10.5), end=make_time(s=13), text="{\\i1}Elephant's <sub>Little</sub> Dream{\\i0}")) ref_keep.append(SSAEvent(start=make_time(s=15), end=make_time(s=18), text="<font color=\"cyan\">At the left we can see...</font>")) subs_default = SSAFile.from_string(text) subs_keep = SSAFile.from_string(text, keep_unknown_html_tags=True) assert subs_default.equals(ref_default) assert subs_keep.equals(ref_keep) assert subs_keep.to_string("srt") == ref_keep.to_string("srt")
def test_read_position_styling(): """position is ignored, italic is converted, color is ignored""" text = dedent("""\ 1 00:00:10,500 --> 00:00:13,000 X1:63 X2:223 Y1:43 Y2:58 <i>Elephant's Dream</i> 2 00:00:15,000 --> 00:00:18,000 X1:53 X2:303 Y1:438 Y2:453 <font color="cyan">At the left we can see...</font> """) ref = SSAFile() ref.append( SSAEvent(start=make_time(s=10.5), end=make_time(s=13), text="{\\i1}Elephant's Dream{\\i0}")) ref.append( SSAEvent(start=make_time(s=15), end=make_time(s=18), text="At the left we can see...")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_read_complex(): # regression test for #30 text = dedent("""\ WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:0 00:50.099 --> 00:53.299 line:85% align:middle Cuidem do seu grupo. Cuidem de suas fileiras. 01:54.255 --> 01:55.455 line:85% align:middle Parem! 01:58.155 --> 01:59.555 line:85% align:middle E, parem! """) ref = SSAFile() ref.append( SSAEvent(start=make_time(s=50, ms=99), end=make_time(s=53, ms=299), text=r"Cuidem do seu grupo.\NCuidem de suas fileiras.")) ref.append( SSAEvent(start=make_time(m=1, s=54, ms=255), end=make_time(m=1, s=55, ms=455), text="Parem!")) ref.append( SSAEvent(start=make_time(m=1, s=58, ms=155), end=make_time(m=1, s=59, ms=555), text="E, parem!")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_empty_subtitles(): # regression test for issue #11 text = dedent(""" 392 00:29:27,46 --> 00:29:29,83 I'm Liza Minnelli.. 393 00:00:00,00 --> 00:00:00,00 394 00:00:00,00 --> 00:00:00,00 """) ref = SSAFile() ref.append( SSAEvent(start=make_time(m=29, s=27, ms=460), end=make_time(m=29, s=29, ms=830), text="I'm Liza Minnelli..")) ref.append(SSAEvent(start=0, end=0, text="")) ref.append(SSAEvent(start=0, end=0, text="")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_negative_timestamp_read(): ref = build_ref() subs = SSAFile.from_string(NEGATIVE_TIMESTAMP_ASS_REF) # negative timestamp is read correctly assert subs[0].start == -make_time(1, 59, 54, 930) # negative times are flushed to zero on output assert ref.to_string("ass") == subs.to_string("ass")
def get_empty_sub(): text = ''' 1 00:00:00,000 --> 00:00:05,000 An example SubRip file. ''' subs = SSAFile.from_string(text) subs.__delitem__(0) return subs
def test_extra_whitespace_parsing(): f = dedent("""\ { 0 } { 0 } 1000.0 { 10 }{ 20} Hello! """) subs = SSAFile.from_string(f) assert subs[0] == SSAEvent(start=10, end=20, text="Hello!")
def test_aegisub_project_garbage(): subs = SSAFile.from_string(AEGISUB_PROJECT_GARBAGE_FILE) garbage_section = dedent(""" [Aegisub Project Garbage] Last Style Storage: Default Video File: ?dummy:23.976000:40000:640:480:47:163:254: Video AR Value: 1.333333 Video Zoom Percent: 0.500000 Active Line: 2""") assert garbage_section in subs.to_string("ass")
def test_tags_parsing(): f1 = "{10}{20}{Y:i,u}Hello!" subs1 = SSAFile.from_string(f1, fps=1000) assert subs1[0].text == "{\\i1\\u1}Hello!" f2 = "{10}{20}Hello, {f:Comic Sans MS}world!" subs2 = SSAFile.from_string(f2, fps=1000) assert subs2[0].text == r"Hello, {\fnComic Sans MS}world!" # the C tag is not supported, unsupported tags are copied verbatim f3 = "{10}{20}Hello, {c:$0000FF}world!" subs3 = SSAFile.from_string(f3, fps=1000) assert subs3[0].text == r"Hello, {c:$0000FF}world!" f4 = "{10}{20}Hello, {P:100,200}world!" subs4 = SSAFile.from_string(f4, fps=1000) assert subs4[0].text == r"Hello, {\pos(100,200)}world!" f5 = "{10}{20}Hello, {s:72}world!" subs5 = SSAFile.from_string(f5, fps=1000) assert subs5[0].text == r"Hello, {\fs72}world!"
def test_simple_read(): text = dedent("""\ 00:00:00:ten--chars 00:01:00:ten--chars-ten-chars """) #calculate endtime from starttime + 500 miliseconds + 67 miliseconds per each character (15 chars per second) ref = SSAFile() ref.append(SSAEvent(start=0, end=make_time(ms=1840), text="ten--chars")) ref.append(SSAEvent(start=make_time(m=1), end=make_time(ms=62510), text="ten--chars-ten-chars")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_overlapping_read(): # see issue #35 text = dedent("""\ 00:00:12:I ... this is some long text ... ... this is some long text ... 00:00:14:observing ... ... this is some long text ... ... this is some long text ... 00:00:18:and ... ... this is some long text ... ... this is some long text ... 00:00:22:You ... ... this is some long text ... ... this is some long text ... """) subs = SSAFile.from_string(text) assert subs[0].start == make_time(s=12) assert subs[0].end == subs[1].start == make_time(s=14) assert subs[1].end == subs[2].start == make_time(s=18) assert subs[2].end == subs[3].start == make_time(s=22)
def test_read_bad_tags(): """missing opening/closing tag, bad nesting, extra whitespace""" text = dedent("""\ 1 00:00:10,500 --> 00:00:13,000 < u><i><font color="red" >Elephant's < s>Dream< / i > Is Long</s> And Badly Nested</xyz> """) ref = SSAFile() ref.append(SSAEvent(start=make_time(s=10.5), end=make_time(s=13), text="{\\u1}{\\i1}Elephant's {\\s1}Dream{\\i0} Is Long{\\s0} And Badly Nested")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_write_read(): subs = SSAFile() e1 = SSAEvent(text="Hello, world!") e2 = SSAEvent(text="The other subtitle.\\NWith two lines.", style="custom style") s1 = SSAStyle(italic=True, primarycolor=Color(r=255, g=0, b=0, a=0)) subs.append(e1) subs.append(e2) subs.styles["custom style"] = s1 json_text = subs.to_string("json") subs2 = SSAFile.from_string(json_text, "json") assert subs2.equals(subs)
def test_read_malformed(): """no line number, no empty line, leading whitespace, bad timestamp format""" text = dedent("""\ 00:00:00.000 ->00:01:00.000 An example subtitle. 0:01:00,00 --> 0:02:00,00 Subtitle number two. """) ref = SSAFile() ref.append(SSAEvent(start=0, end=make_time(m=1), text="An example subtitle.")) ref.append(SSAEvent(start=make_time(m=1), end=make_time(m=2), text="Subtitle number\\Ntwo.")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_parser_skipping_lines(): f = dedent("""\ Ook! {0}{0}23.976 > Hi! {10}{20}Hello! 1 00:00:00,000 --> 00:00:05,000 When editing their SRT files, some people get so careless as to paste them in their MicroDVD files! """) subs = SSAFile.from_string(f, format_="microdvd") assert len(subs) == 1 assert subs[0].text == "Hello!"
def test_simple_read(): text = dedent("""\ 1 00:00:00,000 --> 00:01:00,000 An example subtitle. 2 00:01:00,000 --> 00:02:00,000 Subtitle number two. """) ref = SSAFile() ref.append(SSAEvent(start=0, end=make_time(m=1), text="An example subtitle.")) ref.append(SSAEvent(start=make_time(m=1), end=make_time(m=2), text="Subtitle number\\Ntwo.")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_read_position_styling(): """position is ignored, italic is converted, color is ignored""" text = dedent("""\ 1 00:00:10,500 --> 00:00:13,000 X1:63 X2:223 Y1:43 Y2:58 <i>Elephant's Dream</i> 2 00:00:15,000 --> 00:00:18,000 X1:53 X2:303 Y1:438 Y2:453 <font color="cyan">At the left we can see...</font> """) ref = SSAFile() ref.append(SSAEvent(start=make_time(s=10.5), end=make_time(s=13), text="{\\i1}Elephant's Dream{\\i0}")) ref.append(SSAEvent(start=make_time(s=15), end=make_time(s=18), text="At the left we can see...")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_framerate_inference(): fps = 1000.0 has_fps = dedent("""\ {0}{0}1000.0 {10}{20}Hello! """) no_fps = dedent("""\ {10}{20}Hello! """) ignored_fps = dedent("""\ {0}{0}23.976 {10}{20}Hello! """) illegal_fps = dedent("""\ {0}{0}-23.976 {10}{20}Hello! """) subs1 = SSAFile.from_string(has_fps) assert subs1.fps == fps assert len(subs1) == 1 assert subs1[0] == SSAEvent(start=10, end=20, text="Hello!") subs2 = SSAFile.from_string(no_fps, fps=fps) assert subs2.fps == fps assert len(subs2) == 1 assert subs2[0] == SSAEvent(start=10, end=20, text="Hello!") # fps argument takes preference over what the file says, first line is kept subs3 = SSAFile.from_string(ignored_fps, fps=fps) assert subs3.fps == fps assert len(subs3) == 2 assert subs3[0] == SSAEvent(start=0, end=0, text="23.976") assert subs3[1] == SSAEvent(start=10, end=20, text="Hello!") with assert_raises(UnknownFPSError): SSAFile.from_string(no_fps) with assert_raises(ValueError): SSAFile.from_string(illegal_fps)
def test_empty_subtitles(): # regression test for issue #11 text = dedent(""" 392 00:29:27,46 --> 00:29:29,83 I'm Liza Minnelli.. 393 00:00:00,00 --> 00:00:00,00 394 00:00:00,00 --> 00:00:00,00 """) ref = SSAFile() ref.append(SSAEvent(start=make_time(m=29, s=27, ms=460), end=make_time(m=29, s=29, ms=830), text="I'm Liza Minnelli..")) ref.append(SSAEvent(start=0, end=0, text="")) ref.append(SSAEvent(start=0, end=0, text="")) subs = SSAFile.from_string(text) assert subs.equals(ref)
def test_newlines_parsing(): f = "{10}{20} So|Many||Newlines ||| " subs = SSAFile.from_string(f, fps=1000) assert subs[0].text == r"So\NMany\N\NNewlines \N\N\N"
def test_hex_color_in_ssa(): # see issue #32 subs = SSAFile.from_string(HEX_COLOR_IN_SSA) style = subs.styles["Default"] assert style.primarycolor == Color(r=0xff, g=0xff, b=0xff) assert style.secondarycolor == Color(r=0xff, g=0xff, b=0x00)