Add tests
This commit is contained in:
6
tests/__init__.py
Normal file
6
tests/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from tests.unit import *
|
||||
from tests.integration import *
|
||||
import unittest
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
62
tests/integration.py
Normal file
62
tests/integration.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import unittest
|
||||
import src.fsub.fsub as fsub
|
||||
import shutil
|
||||
import os
|
||||
import inspect
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TestFsub(unittest.TestCase):
|
||||
samples = Path('tests/samples')
|
||||
maxDiff = None
|
||||
|
||||
def run_on(self, args, sample, ofile):
|
||||
ifile = inspect.stack()[1][3] + '.srt'
|
||||
|
||||
sample = str(self.samples / sample) + '.srt'
|
||||
shutil.copy(sample, ifile)
|
||||
args.append(ifile)
|
||||
|
||||
fsub.run(args)
|
||||
|
||||
out = open(ifile)
|
||||
result = out.read()
|
||||
out.close()
|
||||
|
||||
ofile = str(self.samples / ofile) + '.srt'
|
||||
cmp_file = open(ofile)
|
||||
cmp = cmp_file.read()
|
||||
cmp_file.close()
|
||||
|
||||
self.assertEqual(result, cmp)
|
||||
os.remove(ifile)
|
||||
|
||||
def test_cleaned(self):
|
||||
args = ['-f', str(self.samples / 'blacklist')]
|
||||
self.run_on(args, 'sample1', 'sample1-cleaned')
|
||||
|
||||
def test_stripped(self):
|
||||
self.run_on(['-n'], 'sample1', 'sample1-stripped')
|
||||
|
||||
def test_cleaned_stripped(self):
|
||||
args = ['-c', '-f', str(self.samples / 'blacklist'), '-n']
|
||||
self.run_on(args, 'sample1', 'sample1-cleaned-stripped')
|
||||
|
||||
def test_cleaned_stripped_shifted_1h(self):
|
||||
args = ['-c',
|
||||
'-f', str(self.samples / 'blacklist'),
|
||||
'-n',
|
||||
'-s', '3600000']
|
||||
self.run_on(args, 'sample1', 'sample1-cleaned-stripped-shifted-1h')
|
||||
|
||||
def test_shifted_minus_1h(self):
|
||||
args = ['-s', '-3600000']
|
||||
self.run_on(args, 'sample1', 'sample1-shifted-minus-1h')
|
||||
|
||||
def test_shifted_minus_52s(self):
|
||||
args = ['-s', '-52000']
|
||||
self.run_on(args, 'sample1', 'sample1-shifted-minus-52s')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
1
tests/samples/blacklist
Normal file
1
tests/samples/blacklist
Normal file
@@ -0,0 +1 @@
|
||||
Even <a>
|
19
tests/samples/sample1-cleaned-stripped-shifted-1h.srt
Normal file
19
tests/samples/sample1-cleaned-stripped-shifted-1h.srt
Normal file
@@ -0,0 +1,19 @@
|
||||
1
|
||||
01:00:48,900 --> 01:00:49,800
|
||||
This one is full of HTML tags.
|
||||
Above, below, everywhere
|
||||
|
||||
2
|
||||
01:00:53,500 --> 01:00:55,200
|
||||
The script should not
|
||||
care whether the tag is
|
||||
valid or not
|
||||
|
||||
3
|
||||
01:00:56,000 --> 01:00:57,000
|
||||
It should just strip all of
|
||||
them mercilessly
|
||||
|
||||
4
|
||||
01:00:58,100 --> 01:00:59,600
|
||||
Including this one!
|
19
tests/samples/sample1-cleaned-stripped.srt
Normal file
19
tests/samples/sample1-cleaned-stripped.srt
Normal file
@@ -0,0 +1,19 @@
|
||||
1
|
||||
00:00:48,900 --> 00:00:49,800
|
||||
This one is full of HTML tags.
|
||||
Above, below, everywhere
|
||||
|
||||
2
|
||||
00:00:53,500 --> 00:00:55,200
|
||||
The script should not
|
||||
care whether the tag is
|
||||
valid or not
|
||||
|
||||
3
|
||||
00:00:56,000 --> 00:00:57,000
|
||||
It should just strip all of
|
||||
them mercilessly
|
||||
|
||||
4
|
||||
00:00:58,100 --> 00:00:59,600
|
||||
Including this one!
|
19
tests/samples/sample1-cleaned.srt
Normal file
19
tests/samples/sample1-cleaned.srt
Normal file
@@ -0,0 +1,19 @@
|
||||
1
|
||||
00:00:48,900 --> 00:00:49,800
|
||||
<b>This one is full of HTML tags.</b>
|
||||
<i>Above, below, everywhere</i>
|
||||
|
||||
2
|
||||
00:00:53,500 --> 00:00:55,200
|
||||
<html>The script should not
|
||||
care whether the tag is
|
||||
valid or not</html>
|
||||
|
||||
3
|
||||
00:00:56,000 --> 00:00:57,000
|
||||
<p>It should just strip all of
|
||||
them mercilessly</p>
|
||||
|
||||
4
|
||||
00:00:58,100 --> 00:00:59,600
|
||||
<ul>Including this one!</ul>
|
0
tests/samples/sample1-shifted-minus-1h.srt
Normal file
0
tests/samples/sample1-shifted-minus-1h.srt
Normal file
14
tests/samples/sample1-shifted-minus-52s.srt
Normal file
14
tests/samples/sample1-shifted-minus-52s.srt
Normal file
@@ -0,0 +1,14 @@
|
||||
1
|
||||
00:00:01,500 --> 00:00:03,200
|
||||
<html>The script should not
|
||||
care whether the tag is
|
||||
valid or not</html>
|
||||
|
||||
2
|
||||
00:00:04,000 --> 00:00:05,000
|
||||
<p>It should just strip all of
|
||||
them mercilessly</p>
|
||||
|
||||
3
|
||||
00:00:06,100 --> 00:00:07,600
|
||||
<ul>Including this one!</ul>
|
23
tests/samples/sample1-stripped.srt
Normal file
23
tests/samples/sample1-stripped.srt
Normal file
@@ -0,0 +1,23 @@
|
||||
1
|
||||
00:00:48,900 --> 00:00:49,800
|
||||
This one is full of HTML tags.
|
||||
Above, below, everywhere
|
||||
|
||||
2
|
||||
00:00:51,800 --> 00:00:52,700
|
||||
Even 's!
|
||||
|
||||
3
|
||||
00:00:53,500 --> 00:00:55,200
|
||||
The script should not
|
||||
care whether the tag is
|
||||
valid or not
|
||||
|
||||
4
|
||||
00:00:56,000 --> 00:00:57,000
|
||||
It should just strip all of
|
||||
them mercilessly
|
||||
|
||||
5
|
||||
00:00:58,100 --> 00:00:59,600
|
||||
Including this one!
|
23
tests/samples/sample1.srt
Normal file
23
tests/samples/sample1.srt
Normal file
@@ -0,0 +1,23 @@
|
||||
1
|
||||
00:00:48,900 --> 00:00:49,800
|
||||
<b>This one is full of HTML tags.</b>
|
||||
<i>Above, below, everywhere</i>
|
||||
|
||||
2
|
||||
00:00:51,800 --> 00:00:52,700
|
||||
<a href='dummy'>Even <a>'s!</a>
|
||||
|
||||
3
|
||||
00:00:53,500 --> 00:00:55,200
|
||||
<html>The script should not
|
||||
care whether the tag is
|
||||
valid or not</html>
|
||||
|
||||
4
|
||||
00:00:56,000 --> 00:00:57,000
|
||||
<p>It should just strip all of
|
||||
them mercilessly</p>
|
||||
|
||||
5
|
||||
00:00:58,100 --> 00:00:59,600
|
||||
<ul>Including this one!</ul>
|
6
tests/samples/sample2.srt
Normal file
6
tests/samples/sample2.srt
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
1
|
||||
00:01:00,000 --> 00:01:01,000
|
||||
Just a dummy line, I'm sorry
|
||||
But there's whitespace!
|
||||
|
198
tests/unit.py
Executable file
198
tests/unit.py
Executable file
@@ -0,0 +1,198 @@
|
||||
import unittest
|
||||
import re
|
||||
import io
|
||||
import sys
|
||||
import src.fsub.fsub as fsub
|
||||
|
||||
|
||||
class TestTimeStamp(unittest.TestCase):
|
||||
def test_parse(self):
|
||||
# 3 h = 10800000 ms
|
||||
# 46 min = 2760000 ms
|
||||
# 13 s = 13000 ms
|
||||
# 93 ms
|
||||
# summed up: 13573093 ms
|
||||
t = fsub.TimeStamp('03:46:13,093')
|
||||
self.assertEqual(t.time, 13573093)
|
||||
self.assertEqual(t.hours, 3)
|
||||
self.assertEqual(t.minutes, 46)
|
||||
self.assertEqual(t.seconds, 13)
|
||||
self.assertEqual(t.millisecods, 93)
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_missing_comma(self):
|
||||
fsub.TimeStamp('00:00:00000')
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_missing_zeros(self):
|
||||
fsub.TimeStamp('0:0:00,00')
|
||||
|
||||
def test_repr(self):
|
||||
time = '03:46:13,093'
|
||||
t = fsub.TimeStamp(time)
|
||||
self.assertEqual(repr(t), time)
|
||||
|
||||
def test_operations(self):
|
||||
t1_str = '03:46:13,093'
|
||||
t1 = fsub.TimeStamp(t1_str)
|
||||
t2 = fsub.TimeStamp('07:39:50,920')
|
||||
res = fsub.TimeStamp('11:26:04,013')
|
||||
zero = fsub.TimeStamp('00:00:00,000')
|
||||
|
||||
self.assertNotEqual(t1, t2)
|
||||
self.assertLess(t1, t2)
|
||||
self.assertGreater(t2, t1)
|
||||
|
||||
t1 += t2
|
||||
|
||||
self.assertEqual(t1, res)
|
||||
self.assertGreater(t1, t2)
|
||||
self.assertLess(t2, t1)
|
||||
|
||||
t1 += -t2
|
||||
|
||||
self.assertEqual(t1, fsub.TimeStamp(t1_str))
|
||||
self.assertLess(t1, t2)
|
||||
self.assertGreater(t2, t1)
|
||||
|
||||
t1 -= t1
|
||||
t = t2.time
|
||||
t2 += t
|
||||
t2 -= t
|
||||
t2 -= t
|
||||
|
||||
self.assertEqual(t1, zero)
|
||||
self.assertEqual(t2, zero)
|
||||
|
||||
|
||||
class TestSubtitle(unittest.TestCase):
|
||||
sample_n = 10
|
||||
sample_start = '02:01:02,000'
|
||||
sample_end = '02:02:00,000'
|
||||
sample_content = \
|
||||
'This is a test subtitle, which\n' + \
|
||||
'may contain line breaks'
|
||||
sample_sub = '{}\n{} --> {}\n{}' \
|
||||
.format(sample_n, sample_start, sample_end, sample_content)
|
||||
sample_fname = 'some_file.srt'
|
||||
sample_line = 30
|
||||
|
||||
def test_parse(self):
|
||||
sub = fsub.Subtitle(self.sample_sub,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
|
||||
self.assertEqual(sub.number, self.sample_n)
|
||||
self.assertEqual(repr(sub.time_start), self.sample_start)
|
||||
self.assertEqual(repr(sub.time_end), self.sample_end)
|
||||
self.assertEqual(len(sub), 4)
|
||||
|
||||
for line in zip(self.sample_content.splitlines(), sub.content):
|
||||
self.assertEqual(line[0], line[1])
|
||||
|
||||
def test_repr(self):
|
||||
sub = fsub.Subtitle(self.sample_sub,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
self.assertEqual(repr(sub), self.sample_sub)
|
||||
|
||||
def test_shift(self):
|
||||
sub = fsub.Subtitle(self.sample_sub,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
start = fsub.TimeStamp(self.sample_start)
|
||||
end = fsub.TimeStamp(self.sample_end)
|
||||
# Some random amount
|
||||
shift_by = 2327291392
|
||||
|
||||
sub.shift(shift_by)
|
||||
start += shift_by
|
||||
end += shift_by
|
||||
self.assertEqual(sub.time_start, start)
|
||||
self.assertEqual(sub.time_end, end)
|
||||
|
||||
def test_replace(self):
|
||||
sub = fsub.Subtitle(self.sample_sub,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
|
||||
sub.replace(re.compile('dummy str not in sub'), '')
|
||||
|
||||
self.assertEqual(repr(sub), self.sample_sub)
|
||||
|
||||
sub.replace(re.compile('is a test'), 'is not a test')
|
||||
|
||||
self.assertNotEqual(repr(sub), self.sample_sub)
|
||||
|
||||
def test_matches(self):
|
||||
sub = fsub.Subtitle(self.sample_sub,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
|
||||
m1 = sub.matches(re.compile('dummy str not in sub'))
|
||||
|
||||
self.assertFalse(m1)
|
||||
|
||||
m2 = sub.matches(re.compile('is a test'))
|
||||
|
||||
self.assertTrue(m2)
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_bad_number(self):
|
||||
sub_str = """badnumber
|
||||
02:01:02,000 --> 02:02:00,000
|
||||
This is a test subtitle, which
|
||||
may contain line breaks"""
|
||||
sys.stderr = io.StringIO()
|
||||
|
||||
fsub.Subtitle(sub_str,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
|
||||
sys.stderr = sys.__stderr__
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_neg_number(self):
|
||||
sub_str = """-1
|
||||
02:01:02,000 --> 02:02:00,000
|
||||
This is a test subtitle, which
|
||||
may contain line breaks"""
|
||||
sys.stderr = io.StringIO()
|
||||
|
||||
fsub.Subtitle(sub_str,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
|
||||
sys.stderr = sys.__stderr__
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_bad_time_span(self):
|
||||
sub_str = """1
|
||||
02:01:02,000 <-- 02:02:00,000
|
||||
This is a test subtitle, which
|
||||
may contain line breaks"""
|
||||
sys.stderr = io.StringIO()
|
||||
|
||||
fsub.Subtitle(sub_str,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
|
||||
sys.stderr = sys.__stderr__
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_inverted_time(self):
|
||||
sub_str = """1
|
||||
12:01:02,000 --> 02:02:00,000
|
||||
This is a test subtitle, which
|
||||
may contain line breaks"""
|
||||
sys.stderr = io.StringIO()
|
||||
|
||||
fsub.Subtitle(sub_str,
|
||||
self.sample_fname,
|
||||
self.sample_line)
|
||||
|
||||
sys.stderr = sys.__stderr__
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user