mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add the tag mapper tests to the main test runner
This commit is contained in:
parent
7ff81c84b3
commit
08d56e2eee
@ -8,7 +8,7 @@ import unittest
|
|||||||
|
|
||||||
from setup import Command
|
from setup import Command
|
||||||
|
|
||||||
TEST_MODULES = frozenset('srv db polish opf css docx cfi matcher icu smartypants build'.split())
|
TEST_MODULES = frozenset('srv db polish opf css docx cfi matcher icu smartypants build misc'.split())
|
||||||
|
|
||||||
def find_tests(which_tests=None):
|
def find_tests(which_tests=None):
|
||||||
ans = []
|
ans = []
|
||||||
@ -59,6 +59,9 @@ def find_tests(which_tests=None):
|
|||||||
if ok('smartypants'):
|
if ok('smartypants'):
|
||||||
from calibre.utils.smartypants import run_tests
|
from calibre.utils.smartypants import run_tests
|
||||||
a(run_tests(return_tests=True))
|
a(run_tests(return_tests=True))
|
||||||
|
if ok('misc'):
|
||||||
|
from calibre.ebooks.metadata.tag_mapper import find_tests
|
||||||
|
a(find_tests())
|
||||||
|
|
||||||
tests = unittest.TestSuite(ans)
|
tests = unittest.TestSuite(ans)
|
||||||
return tests
|
return tests
|
||||||
@ -68,7 +71,7 @@ class Test(Command):
|
|||||||
description = 'Run the calibre test suite'
|
description = 'Run the calibre test suite'
|
||||||
|
|
||||||
def add_options(self, parser):
|
def add_options(self, parser):
|
||||||
parser.add_option('--test-module', default=[], action='append', type='choice', choices=sorted(map(str, TEST_MODULES)),
|
parser.add_option('--test-module', '--test-group', default=[], action='append', type='choice', choices=sorted(map(str, TEST_MODULES)),
|
||||||
help='The test module to run (can be specified more than once for multiple modules). Choices: %s' % ', '.join(sorted(TEST_MODULES)))
|
help='The test module to run (can be specified more than once for multiple modules). Choices: %s' % ', '.join(sorted(TEST_MODULES)))
|
||||||
parser.add_option('--test-verbosity', type=int, default=4, help='Test verbosity (0-4)')
|
parser.add_option('--test-verbosity', type=int, default=4, help='Test verbosity (0-4)')
|
||||||
parser.add_option('--test-name', default=[], action='append',
|
parser.add_option('--test-name', default=[], action='append',
|
||||||
|
@ -122,42 +122,47 @@ def map_tags(tags, rules=()):
|
|||||||
ans.extend(apply_rules(t, rules))
|
ans.extend(apply_rules(t, rules))
|
||||||
return uniq(filter(None, ans))
|
return uniq(filter(None, ans))
|
||||||
|
|
||||||
def test():
|
def find_tests():
|
||||||
|
import unittest
|
||||||
|
class TestTagMapper(unittest.TestCase):
|
||||||
|
|
||||||
def rule(action, query, replace=None, match_type='one_of'):
|
def test_tag_mapper(self):
|
||||||
ans = {'action':action, 'query': query, 'match_type':match_type}
|
|
||||||
if replace is not None:
|
|
||||||
ans['replace'] = replace
|
|
||||||
return ans
|
|
||||||
|
|
||||||
def run(rules, tags, expected):
|
def rule(action, query, replace=None, match_type='one_of'):
|
||||||
if isinstance(rules, dict):
|
ans = {'action':action, 'query': query, 'match_type':match_type}
|
||||||
rules = [rules]
|
if replace is not None:
|
||||||
if isinstance(tags, type('')):
|
ans['replace'] = replace
|
||||||
tags = [x.strip() for x in tags.split(',')]
|
return ans
|
||||||
if isinstance(expected, type('')):
|
|
||||||
expected = [x.strip() for x in expected.split(',')]
|
|
||||||
ans = map_tags(tags, rules)
|
|
||||||
if ans != expected:
|
|
||||||
raise AssertionError('Expected: %r != %r' % (expected, ans))
|
|
||||||
|
|
||||||
run(rule('capitalize', 't1,t2'), 't1,x1', 'T1,x1')
|
def run(rules, tags, expected):
|
||||||
run(rule('upper', 'ta,t2'), 'ta,x1', 'TA,x1')
|
if isinstance(rules, dict):
|
||||||
run(rule('lower', 'ta,x1'), 'TA,X1', 'ta,x1')
|
rules = [rules]
|
||||||
run(rule('replace', 't1', 't2'), 't1,x1', 't2,x1')
|
if isinstance(tags, type('')):
|
||||||
run(rule('replace', '(.)1', r'\g<1>2', 'matches'), 't1,x1', 't2,x2')
|
tags = [x.strip() for x in tags.split(',')]
|
||||||
run(rule('replace', '(.)1', r'\g<1>2,3', 'matches'), 't1,x1', 't2,3,x2')
|
if isinstance(expected, type('')):
|
||||||
run(rule('replace', 't1', 't2, t3'), 't1,x1', 't2,t3,x1')
|
expected = [x.strip() for x in expected.split(',')]
|
||||||
run([rule('replace', 't1', 't2,t3'), rule('remove', 't2')], 't1,x1', 't3,x1')
|
ans = map_tags(tags, rules)
|
||||||
run(rule('replace', 't1', 't1'), 't1,x1', 't1,x1')
|
self.assertEqual(ans, expected)
|
||||||
run([rule('replace', 't1', 't2'), rule('replace', 't2', 't1')], 't1,t2', 't1,t2')
|
|
||||||
run(rule('replace', 'a', 'A'), 'a,b', 'A,b')
|
run(rule('capitalize', 't1,t2'), 't1,x1', 'T1,x1')
|
||||||
run(rule('replace', 'a,b', 'A,B'), 'a,b', 'A,B')
|
run(rule('upper', 'ta,t2'), 'ta,x1', 'TA,x1')
|
||||||
run(rule('replace', 'L', 'T', 'has'), 'L', 'T')
|
run(rule('lower', 'ta,x1'), 'TA,X1', 'ta,x1')
|
||||||
run(rule('split', '/', '/', 'has'), 'a/b/c,d', 'a,b,c,d')
|
run(rule('replace', 't1', 't2'), 't1,x1', 't2,x1')
|
||||||
run(rule('split', '/', '/', 'has'), '/,d', 'd')
|
run(rule('replace', '(.)1', r'\g<1>2', 'matches'), 't1,x1', 't2,x2')
|
||||||
run(rule('split', '/', '/', 'has'), '/a/', 'a')
|
run(rule('replace', '(.)1', r'\g<1>2,3', 'matches'), 't1,x1', 't2,3,x2')
|
||||||
run(rule('split', 'a,b', '/'), 'a,b', 'a,b')
|
run(rule('replace', 't1', 't2, t3'), 't1,x1', 't2,t3,x1')
|
||||||
|
run([rule('replace', 't1', 't2,t3'), rule('remove', 't2')], 't1,x1', 't3,x1')
|
||||||
|
run(rule('replace', 't1', 't1'), 't1,x1', 't1,x1')
|
||||||
|
run([rule('replace', 't1', 't2'), rule('replace', 't2', 't1')], 't1,t2', 't1,t2')
|
||||||
|
run(rule('replace', 'a', 'A'), 'a,b', 'A,b')
|
||||||
|
run(rule('replace', 'a,b', 'A,B'), 'a,b', 'A,B')
|
||||||
|
run(rule('replace', 'L', 'T', 'has'), 'L', 'T')
|
||||||
|
run(rule('split', '/', '/', 'has'), 'a/b/c,d', 'a,b,c,d')
|
||||||
|
run(rule('split', '/', '/', 'has'), '/,d', 'd')
|
||||||
|
run(rule('split', '/', '/', 'has'), '/a/', 'a')
|
||||||
|
run(rule('split', 'a,b', '/'), 'a,b', 'a,b')
|
||||||
|
return unittest.defaultTestLoader.loadTestsFromTestCase(TestTagMapper)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test()
|
from calibre.utils.run_tests import run_cli
|
||||||
|
run_cli(find_tests())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user