Skip to content

Commit 2c11b04

Browse files
committed
Add some tests for sections and attribute access
Also switch to pytest only, since we're using fixtures.
1 parent 2a83e88 commit 2c11b04

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

tests/test_ass.py

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env python
22

3-
import ass
4-
import unittest
3+
from io import StringIO
54
from pathlib import Path
5+
from textwrap import dedent
66

7-
from io import StringIO
7+
import pytest
8+
9+
import ass
810

911
folder = Path(__file__).parent
1012

1113

12-
class TestEverything(unittest.TestCase):
14+
class TestDocument:
1315

1416
test_ass = Path(folder, "test.ass")
1517

@@ -25,11 +27,11 @@ def test_parse_dump(self):
2527

2628
def test_parse_encoding(self):
2729
with self.test_ass.open("r", encoding='utf_8') as f:
28-
with self.assertRaises(ValueError):
30+
with pytest.raises(ValueError):
2931
ass.parse(f)
3032

3133
with self.test_ass.open("r", encoding='ascii') as f:
32-
with self.assertRaises(ValueError):
34+
with pytest.raises(ValueError):
3335
ass.parse(f)
3436

3537
def test_dump_encoding(self):
@@ -38,9 +40,79 @@ def test_dump_encoding(self):
3840

3941
import tempfile
4042
with tempfile.TemporaryFile(mode='w', encoding='utf_8') as f:
41-
with self.assertWarns(UserWarning):
43+
with pytest.warns(UserWarning):
4244
doc.dump_file(f)
4345

4446

45-
if __name__ == "__main__":
46-
unittest.main()
47+
class TestSections:
48+
49+
def test_default_sections(self):
50+
doc = ass.Document()
51+
assert len(doc.sections) == 3
52+
assert doc.fields is doc.info
53+
assert len(doc.fields) == 0
54+
assert len(doc.styles) == 0
55+
assert len(doc.events) == 0
56+
57+
def test_scipt_info(self):
58+
TEST_SCRIPT_INFO = dedent("""\
59+
[Script Info]
60+
ScriptType: v4.00+
61+
PlayResX: 500
62+
PlayResY: 500""")
63+
64+
doc = ass.Document.parse_string(TEST_SCRIPT_INFO)
65+
doc.info["PlayResY"] = 50
66+
doc.info["Arbitrary Field"] = "hi"
67+
68+
assert len(doc.sections) == 3
69+
assert doc.info["PlayResX"] == 500
70+
assert doc.play_res_y == 50
71+
doc.play_res_y = 5
72+
assert doc.play_res_y == 5
73+
74+
copy = doc.info.copy()
75+
copy["PlayResX"] = 1
76+
assert copy["Arbitrary Field"] == "hi"
77+
assert doc.play_res_x == 500
78+
79+
def test_styles(self):
80+
pass
81+
82+
def test_events(self):
83+
pass
84+
85+
TEST_CUSTOM = dedent("""\
86+
[Custom Section]
87+
Line: 1
88+
Line: 2
89+
Line: 3
90+
Another Line: 20""")
91+
92+
@pytest.fixture
93+
def line_section(self):
94+
doc = ass.Document.parse_string(self.TEST_CUSTOM)
95+
return doc.sections["Custom Section"]
96+
97+
def test_custom_line_section_parse(self):
98+
doc = ass.Document.parse_string(self.TEST_CUSTOM)
99+
assert "custom section" in doc.sections
100+
assert "Custom Section" in doc.sections
101+
assert doc.sections["Custom Section"].name == "Custom Section"
102+
103+
def test_custom_line_section_read(self, line_section):
104+
assert len(line_section) == 4
105+
line = line_section[0]
106+
assert isinstance(line, ass.Unknown)
107+
assert line.TYPE == "Line"
108+
assert line.value == "1"
109+
assert line_section[-1].value == "20"
110+
111+
def test_custom_line_section_write(self, line_section):
112+
line_section.add_line("Test", "test")
113+
assert len(line_section) == 5
114+
line_section[:3] = line_section[:2] # remove 3rd line with a slice
115+
assert [x.value for x in line_section] == ["1", "2", "20", "test"]
116+
117+
def test_custom_line_section_dump(self, line_section):
118+
assert "\n".join(line_section.dump()) == self.TEST_CUSTOM

0 commit comments

Comments
 (0)