diff options
author | James E. Blair <corvus@gnu.org> | 2019-04-27 09:35:10 -0700 |
---|---|---|
committer | James E. Blair <corvus@gnu.org> | 2019-04-27 09:35:10 -0700 |
commit | f166277b69e07a942a70101a8d79032aac6be4d1 (patch) | |
tree | de4a4a8e631cbb3c3d36f2ccd9b1c75fb2dd9cb8 /tests |
Diffstat (limited to 'tests')
-rw-r--r-- | tests/__init__.py | 0 | ||||
-rw-r--r-- | tests/test.py | 191 |
2 files changed, 191 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/__init__.py | |||
diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 0000000..9cf45f7 --- /dev/null +++ b/tests/test.py | |||
@@ -0,0 +1,191 @@ | |||
1 | # -*- coding: utf-8 -*- | ||
2 | # Copyright (C) 2019 James E. Blair <corvus@gnu.org> | ||
3 | # | ||
4 | # This program is free software: you can redistribute it and/or modify | ||
5 | # it under the terms of the GNU General Public License as published by | ||
6 | # the Free Software Foundation, either version 3 of the License, or | ||
7 | # (at your option) any later version. | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, | ||
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | # GNU General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License | ||
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | |||
17 | import logging | ||
18 | import struct | ||
19 | import tempfile | ||
20 | |||
21 | import testtools | ||
22 | import fixtures | ||
23 | |||
24 | from editty.segment import * | ||
25 | from editty.program import * | ||
26 | import editty.source | ||
27 | |||
28 | class BaseTestCase(testtools.TestCase): | ||
29 | def setUp(self): | ||
30 | super().setUp() | ||
31 | fs = '%(asctime)s %(levelname)s [%(name)s] %(message)s' | ||
32 | self.useFixture(fixtures.FakeLogger(level=logging.DEBUG, | ||
33 | format=fs)) | ||
34 | self.log = logging.getLogger("test") | ||
35 | |||
36 | class FileTypeTests: | ||
37 | def get_frames(self, source): | ||
38 | ret = [] | ||
39 | for (timecode, frame) in source.getFrames(0, source.length): | ||
40 | ret.append((timecode, b''.join([x[2] for x in frame.content[0]]).strip())) | ||
41 | return ret | ||
42 | |||
43 | def test_load(self): | ||
44 | source = self.setup() | ||
45 | self.assertEqual( | ||
46 | [(1.0, b'a'), (2.0, b'ab'), (3.0, b'abc'), (4.0, b'abcd')], | ||
47 | self.get_frames(source)) | ||
48 | |||
49 | class TestScriptFile(BaseTestCase, FileTypeTests): | ||
50 | def setup(self): | ||
51 | size = (80, 24) | ||
52 | with tempfile.NamedTemporaryFile() as stream: | ||
53 | with tempfile.NamedTemporaryFile() as timing: | ||
54 | stream.write(b"\nabcd") | ||
55 | for x in range(4): | ||
56 | timing.write(b"1.0 1\n") | ||
57 | stream.flush() | ||
58 | timing.flush() | ||
59 | source = editty.source.ScriptFile().load( | ||
60 | size, stream.name, timing.name, 'color') | ||
61 | return source | ||
62 | |||
63 | class TestTtyrecFile(BaseTestCase, FileTypeTests): | ||
64 | def setup(self): | ||
65 | size = (80, 24) | ||
66 | frames = [(1.0, b'a'), (2.0, b'b'), (3.0, b'c'), (4.0, b'd')] | ||
67 | with tempfile.NamedTemporaryFile() as stream: | ||
68 | for (timecode, data) in frames: | ||
69 | tc_secs, tc_usecs = map(int, ('%0.6f' % timecode).split('.')) | ||
70 | stream.write(struct.pack('<III', | ||
71 | tc_secs, tc_usecs, len(data))) | ||
72 | stream.write(data) | ||
73 | stream.flush() | ||
74 | source = editty.source.TtyrecFile().load( | ||
75 | size, stream.name, None, 'color') | ||
76 | return source | ||
77 | |||
78 | class TestProgram(BaseTestCase): | ||
79 | size = (80, 24) | ||
80 | |||
81 | def setup(self, color='1'): | ||
82 | with tempfile.NamedTemporaryFile() as stream: | ||
83 | with tempfile.NamedTemporaryFile() as timing: | ||
84 | stream.write(b"\nabcd") | ||
85 | for x in range(4): | ||
86 | timing.write(b"1.0 1\n") | ||
87 | stream.flush() | ||
88 | timing.flush() | ||
89 | source = editty.source.ScriptFile().load( | ||
90 | self.size, stream.name, timing.name, color) | ||
91 | program = Program(source.title) | ||
92 | program.append(Clip(source, 0.0, source.length)) | ||
93 | return program | ||
94 | |||
95 | def get_frames(self, program, start, end, color=False): | ||
96 | ret = [] | ||
97 | for fi in program.getFrames(start, end): | ||
98 | if color: | ||
99 | ret.append((fi.timecode, b''.join([x[2] for x in fi.frame.content[0]]).strip(), fi.frame.timeline_color)) | ||
100 | else: | ||
101 | ret.append((fi.timecode, b''.join([x[2] for x in fi.frame.content[0]]).strip())) | ||
102 | return ret | ||
103 | |||
104 | def test_playback(self): | ||
105 | program = self.setup() | ||
106 | frames = self.get_frames(program, 0, program.length) | ||
107 | self.assertEqual( | ||
108 | [(1.0, b'a'), (2.0, b'ab'), (3.0, b'abc'), (4.0, b'abcd')], | ||
109 | frames) | ||
110 | |||
111 | def test_cut(self): | ||
112 | p1 = self.setup('1') | ||
113 | self.assertEqual( | ||
114 | [(1.0, b'a'), (2.0, b'ab'), (3.0, b'abc'), (4.0, b'abcd')], | ||
115 | self.get_frames(p1, None, None)) | ||
116 | |||
117 | cut = p1.cut(2.0, 3.0) | ||
118 | self.assertEqual( | ||
119 | [(1.0, b'a'), (2.0, b'ab'), (2.0, b'abc'), (3.0, b'abcd')], | ||
120 | self.get_frames(p1, None, None)) | ||
121 | self.assertEqual( | ||
122 | [(0.0, b'ab'), (1.0, b'abc')], | ||
123 | self.get_frames(cut, None, None)) | ||
124 | |||
125 | p2 = self.setup('2') | ||
126 | p2.insert(2.5, cut) | ||
127 | self.log.debug(self.get_frames(p2, None, None, True)) | ||
128 | |||
129 | cut = p2.cut(2.0, 4.0) | ||
130 | p3 = self.setup('3') | ||
131 | self.log.debug("cut") | ||
132 | for s in cut.segments: | ||
133 | self.log.debug(s) | ||
134 | self.log.debug(self.get_frames(cut, None, None, True)) | ||
135 | self.assertEqual( | ||
136 | [(0.0, b'ab', '2'), (0.5, b'ab', '1'), (1.5, b'abc', '1'), | ||
137 | (1.5, b'ab', '2'), (2.0, b'abc', '2')], | ||
138 | self.get_frames(cut, None, None, True)) | ||
139 | p3.insert(2.5, cut) | ||
140 | self.log.debug(self.get_frames(p3, None, None, True)) | ||
141 | self.assertEqual( | ||
142 | [(1.0, b'a', '3'), (2.0, b'ab', '3'), | ||
143 | (2.5, b'ab', '2'), | ||
144 | (3.0, b'ab', '1'), (4.0, b'abc', '1'), | ||
145 | (4.0, b'ab', '2'), (4.5, b'abc', '2'), | ||
146 | (4.5, b'ab', '3'), (5.0, b'abc', '3'), (6.0, b'abcd', '3')], | ||
147 | self.get_frames(p3, None, None, True)) | ||
148 | |||
149 | def test_repeated_cut(self): | ||
150 | p1 = self.setup('1') | ||
151 | self.assertEqual( | ||
152 | [(1.0, b'a'), (2.0, b'ab'), (3.0, b'abc'), (4.0, b'abcd')], | ||
153 | self.get_frames(p1, None, None)) | ||
154 | |||
155 | cut = p1.cut(2.0, 3.0) | ||
156 | self.assertEqual( | ||
157 | [(1.0, b'a'), (2.0, b'ab'), (2.0, b'abc'), (3.0, b'abcd')], | ||
158 | self.get_frames(p1, None, None)) | ||
159 | self.assertEqual( | ||
160 | [(0.0, b'ab'), (1.0, b'abc')], | ||
161 | self.get_frames(cut, None, None)) | ||
162 | p2 = Program('2') | ||
163 | self.log.debug('append') | ||
164 | for s in cut.segments: | ||
165 | self.log.debug(s) | ||
166 | p2.append(cut) | ||
167 | self.log.debug('appended') | ||
168 | for s in p2.segments: | ||
169 | self.log.debug(s) | ||
170 | self.log.debug(self.get_frames(p2, None, None, True)) | ||
171 | |||
172 | cut = p1.cut(2.2, 2.7) # 3.2 - 3.7 | ||
173 | self.log.debug(self.get_frames(p1, None, None)) | ||
174 | self.assertEqual( | ||
175 | [(1.0, b'a'), (2.0, b'ab'), (2.0, b'abc'), (2.2, b'abc'), (2.5, b'abcd')], | ||
176 | self.get_frames(p1, None, None)) | ||
177 | self.log.debug(self.get_frames(cut, None, None)) | ||
178 | self.assertEqual( | ||
179 | [(0.0, b'abc')], | ||
180 | self.get_frames(cut, None, None)) | ||
181 | self.log.debug('append') | ||
182 | for s in cut.segments: | ||
183 | self.log.debug(s) | ||
184 | p2.append(cut) | ||
185 | self.log.debug('appended') | ||
186 | for s in p2.segments: | ||
187 | self.log.debug(s) | ||
188 | self.log.debug(self.get_frames(p2, None, None, True)) | ||
189 | self.assertEqual( | ||
190 | [(0.0, b'ab', '1'), (1.0, b'abc', '1'), (1.0, b'abc', '1')], | ||
191 | self.get_frames(p2, None, None, True)) | ||