Skip to content

Commit

Permalink
srt_tools: unit test srt-remove
Browse files Browse the repository at this point in the history
Fixes the srt-remove unit tests.
  • Loading branch information
switchupcb committed May 20, 2021
1 parent d7347ca commit a800a1d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.pyc
4 changes: 2 additions & 2 deletions srt_tools/srt-remove
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def parse_args():
"Remove captions non-sequentially": "srt remove -i example.srt --t1 24 --t2 6",
"Remove every caption": "srt remove -i example.srt"
}
parser = srt_tools.utils.basic_parser(description=__doc__, examples=examples, multi_input=True)
parser = srt_tools.utils.basic_parser(description=__doc__, examples=examples)
parser.add_argument(
"--start",
"--t1",
Expand Down Expand Up @@ -191,7 +191,7 @@ def main():
logging.basicConfig(level=args.log_level)
srt_tools.utils.set_basic_args(args)

subs = list(args.input[0]) # TODO: require single input
subs = list(args.input)
removed_subs = remove_caption_timestamp(subs, get_timestamp(subs, args.start), get_timestamp(subs, args.end))

output = srt_tools.utils.compose_suggest_on_fail(removed_subs, strict=args.strict)
Expand Down
65 changes: 20 additions & 45 deletions srt_tools/tests/test_srt_remove.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import unittest

import srt
from srt import srt_timestamp_to_timedelta as t
### TODO: Need to import srt_remove

# import srt-remove as a source file.
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
import os
folder_path = os.path.normpath(os.path.join(
os.path.normpath(os.path.join(__file__, os.pardir)), os.pardir))
file_path = os.path.normpath(os.path.join(folder_path, 'srt-remove'))
spec = spec_from_loader("srt-remove", SourceFileLoader("srt-remove", file_path))
module = module_from_spec(spec)
spec.loader.exec_module(module)

# emulate `from module import *`
if "__all__" in module.__dict__:
names = module.__dict__["__all__"]
else:
names = [x for x in module.__dict__ if not x.startswith("_")]

globals().update({k: getattr(module, k) for k in names})


# helper methods
def create_blocks(setting=0):
"""Creates a generator of subtitles for testing purposes"""
subs = []
Expand Down Expand Up @@ -76,49 +94,6 @@ def setUp(self):
def tearDown(self):
pass

def test_remove_caption_index(self):
a = sort([self.x[0], self.x[2], self.x[3], self.x[4]])

result = remove_caption_index(self.subs(), 1, 2)
self.assertEqual(list(result), a)

result = remove_caption_index(self.subs(), 1, -3)
self.assertEqual(list(result), a)

result = remove_caption_index(self.subs(), 1, 3)
self.assertEqual(list(result), sort([self.x[0], self.x[3], self.x[4]]))

result = remove_caption_index(self.subs(), 2, 5)
self.assertEqual(list(result), sort([self.x[0], self.x[1]]))

result = remove_caption_index(self.subs(), -2, 3)
self.assertEqual(list(result), [])
with self.assertRaises(IndexError):
result = list(remove_caption_index((y for y in []), 0, 1))
result = list(remove_caption_index(self.subs(), 1, 8))
result = list(remove_caption_index(self.subs(), -7, 4))
result = list(remove_caption_index(self.subs(), 5, 4))

result = remove_caption_index(self.subs(), 3, 1) # reverse
self.assertEqual(list(result), sort([self.x[1], self.x[2]]))

result = remove_caption_index(self.subs(), 2, 0) # reverse
self.assertEqual(list(result), sort([self.x[0], self.x[1]]))

a = sort([self.x[2], self.x[3]])
result = remove_caption_index(self.subs(), -1, -3) # reverse
self.assertEqual(list(result), a)

result = remove_caption_index(self.subs(), 4, 2) # reverse
self.assertEqual(list(result), a)

# single parameter
result = remove_caption_index(self.subs(), 0)
self.assertEqual(list(result), [])

result = remove_caption_index(self.subs(), 2)
self.assertEqual(list(result), sort([self.x[0], self.x[1]]))

def test_remove_caption_timestamp(self):
result = remove_caption_timestamp(
[], t('00:00:00,000'), t('00:00:30,000'))
Expand Down

0 comments on commit a800a1d

Please sign in to comment.