Commit 83feff56 authored by Ezio Melotti's avatar Ezio Melotti

#6484: refactor a bit the tests.

parent 91f948a7
...@@ -9,35 +9,47 @@ MAILCAPFILE = test.support.findfile("mailcap.txt") ...@@ -9,35 +9,47 @@ MAILCAPFILE = test.support.findfile("mailcap.txt")
# Dict to act as mock mailcap entry for this test # Dict to act as mock mailcap entry for this test
# The keys and values should match the contents of MAILCAPFILE # The keys and values should match the contents of MAILCAPFILE
MAILCAPDICT = {'application/x-movie': [{'compose': 'moviemaker %s', MAILCAPDICT = {
'x11-bitmap': '"/usr/lib/Zmail/bitmaps/movie.xbm"', 'application/x-movie':
'description': '"Movie"', [{'compose': 'moviemaker %s',
'view': 'movieplayer %s'}], 'x11-bitmap': '"/usr/lib/Zmail/bitmaps/movie.xbm"',
'application/*': [{'copiousoutput': '', 'description': '"Movie"',
'view': 'echo "This is \\"%t\\" but is 50 \\% Greek to me" \\; cat %s'}], 'view': 'movieplayer %s'}],
'audio/basic': [{'edit': 'audiocompose %s', 'application/*':
'compose': 'audiocompose %s', [{'copiousoutput': '',
'description': '"An audio fragment"', 'view': 'echo "This is \\"%t\\" but is 50 \\% Greek to me" \\; cat %s'}],
'view': 'showaudio %s'}], 'audio/basic':
'video/mpeg': [{'view': 'mpeg_play %s'}], [{'edit': 'audiocompose %s',
'application/postscript': [{'needsterminal': '', 'compose': 'audiocompose %s',
'view': 'ps-to-terminal %s'}, 'description': '"An audio fragment"',
{'compose': 'idraw %s', 'view': 'showaudio %s'}],
'view': 'ps-to-terminal %s'}], 'video/mpeg':
'application/x-dvi': [{'view': 'xdvi %s'}], [{'view': 'mpeg_play %s'}],
'message/external-body': [{'composetyped': 'extcompose %s', 'application/postscript':
'description': '"A reference to data stored in an external location"', [{'needsterminal': '', 'view': 'ps-to-terminal %s'},
'needsterminal': '', {'compose': 'idraw %s', 'view': 'ps-to-terminal %s'}],
'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'}], 'application/x-dvi':
'text/richtext': [{'test': 'test "`echo %{charset} | tr \'[A-Z]\' \'[a-z]\'`" = iso-8859-8', [{'view': 'xdvi %s'}],
'copiousoutput': '', 'message/external-body':
'view': 'shownonascii iso-8859-8 -e richtext -p %s'}], [{'composetyped': 'extcompose %s',
'image/x-xwindowdump': [{'view': 'display %s'}], 'description': '"A reference to data stored in an external location"',
'audio/*': [{'view': '/usr/local/bin/showaudio %t'}], 'needsterminal': '',
'video/*': [{'view': 'animate %s'}], 'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'}],
'application/frame': [{'print': '"cat %s | lp"', 'text/richtext':
'view': 'showframe %s'}], [{'test': 'test "`echo %{charset} | tr \'[A-Z]\' \'[a-z]\'`" = iso-8859-8',
'image/rgb': [{'view': 'display %s'}]} 'copiousoutput': '',
'view': 'shownonascii iso-8859-8 -e richtext -p %s'}],
'image/x-xwindowdump':
[{'view': 'display %s'}],
'audio/*':
[{'view': '/usr/local/bin/showaudio %t'}],
'video/*':
[{'view': 'animate %s'}],
'application/frame':
[{'print': '"cat %s | lp"', 'view': 'showframe %s'}],
'image/rgb':
[{'view': 'display %s'}]
}
class HelperFunctionTest(unittest.TestCase): class HelperFunctionTest(unittest.TestCase):
...@@ -47,8 +59,9 @@ class HelperFunctionTest(unittest.TestCase): ...@@ -47,8 +59,9 @@ class HelperFunctionTest(unittest.TestCase):
# So verify that listmailcapfiles() returns a list of strings that is of # So verify that listmailcapfiles() returns a list of strings that is of
# non-zero length. # non-zero length.
mcfiles = mailcap.listmailcapfiles() mcfiles = mailcap.listmailcapfiles()
self.assertTrue(isinstance(mcfiles, list)) self.assertIsInstance(mcfiles, list)
self.assertTrue(all([isinstance(m, str) for m in mcfiles])) for m in mcfiles:
self.assertIsInstance(m, str)
with test.support.EnvironmentVarGuard() as env: with test.support.EnvironmentVarGuard() as env:
# According to RFC 1524, if MAILCAPS env variable exists, use that # According to RFC 1524, if MAILCAPS env variable exists, use that
# and only that. # and only that.
...@@ -85,14 +98,14 @@ class HelperFunctionTest(unittest.TestCase): ...@@ -85,14 +98,14 @@ class HelperFunctionTest(unittest.TestCase):
plist = ['id=1', 'number=2', 'total=3'] plist = ['id=1', 'number=2', 'total=3']
# test case: ([field, MIMEtype, filename, plist=[]], <expected string>) # test case: ([field, MIMEtype, filename, plist=[]], <expected string>)
test_cases = [ test_cases = [
(["", "audio/*", "foo.txt"], ""), (["", "audio/*", "foo.txt"], ""),
(["echo foo", "audio/*", "foo.txt"], "echo foo"), (["echo foo", "audio/*", "foo.txt"], "echo foo"),
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"), (["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"), (["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
(["echo \%t", "audio/*", "foo.txt"], "echo %t"), (["echo \%t", "audio/*", "foo.txt"], "echo %t"),
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"), (["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3") (["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
] ]
for tc in test_cases: for tc in test_cases:
self.assertEqual(mailcap.subst(*tc[0]), tc[1]) self.assertEqual(mailcap.subst(*tc[0]), tc[1])
...@@ -111,15 +124,16 @@ class GetcapsTest(unittest.TestCase): ...@@ -111,15 +124,16 @@ class GetcapsTest(unittest.TestCase):
def test_system_mailcap(self): def test_system_mailcap(self):
# Test mailcap.getcaps() with mailcap file(s) on system, if any. # Test mailcap.getcaps() with mailcap file(s) on system, if any.
caps = mailcap.getcaps() caps = mailcap.getcaps()
self.assertTrue(isinstance(caps, dict)) self.assertIsInstance(caps, dict)
mailcapfiles = mailcap.listmailcapfiles() mailcapfiles = mailcap.listmailcapfiles()
existingmcfiles = [mcf for mcf in mailcapfiles if os.path.exists(mcf)] existingmcfiles = [mcf for mcf in mailcapfiles if os.path.exists(mcf)]
if existingmcfiles: if existingmcfiles:
# At least 1 mailcap file exists, so test that. # At least 1 mailcap file exists, so test that.
for (k, v) in caps.items(): for (k, v) in caps.items():
self.assertTrue(isinstance(k, str)) self.assertIsInstance(k, str)
self.assertTrue(isinstance(v, list)) self.assertIsInstance(v, list)
self.assertTrue(all([isinstance(e, dict) for e in v])) for e in v:
self.assertIsInstance(e, dict)
else: else:
# No mailcap files on system. getcaps() should return empty dict. # No mailcap files on system. getcaps() should return empty dict.
self.assertEqual({}, caps) self.assertEqual({}, caps)
...@@ -134,15 +148,19 @@ class FindmatchTest(unittest.TestCase): ...@@ -134,15 +148,19 @@ class FindmatchTest(unittest.TestCase):
fname = "foo.txt" fname = "foo.txt"
plist = ["access-type=default", "name=john", "site=python.org", plist = ["access-type=default", "name=john", "site=python.org",
"directory=/tmp", "mode=foo", "server=bar"] "directory=/tmp", "mode=foo", "server=bar"]
audio_basic_entry = {'edit': 'audiocompose %s', audio_basic_entry = {
'compose': 'audiocompose %s', 'edit': 'audiocompose %s',
'description': '"An audio fragment"', 'compose': 'audiocompose %s',
'view': 'showaudio %s'} 'description': '"An audio fragment"',
'view': 'showaudio %s'
}
audio_entry = {"view": "/usr/local/bin/showaudio %t"} audio_entry = {"view": "/usr/local/bin/showaudio %t"}
video_entry = {'view': 'animate %s'} video_entry = {'view': 'animate %s'}
message_entry = {'composetyped': 'extcompose %s', message_entry = {
'description': '"A reference to data stored in an external location"', 'needsterminal': '', 'composetyped': 'extcompose %s',
'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'} 'description': '"A reference to data stored in an external location"', 'needsterminal': '',
'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'
}
# test case: (findmatch args, findmatch keyword args, expected output) # test case: (findmatch args, findmatch keyword args, expected output)
# positional args: caps, MIMEtype # positional args: caps, MIMEtype
...@@ -169,7 +187,7 @@ class FindmatchTest(unittest.TestCase): ...@@ -169,7 +187,7 @@ class FindmatchTest(unittest.TestCase):
([c, "message/external-body"], ([c, "message/external-body"],
{"plist": plist}, {"plist": plist},
("showexternal /dev/null default john python.org /tmp foo bar", message_entry)) ("showexternal /dev/null default john python.org /tmp foo bar", message_entry))
] ]
self._run_cases(cases) self._run_cases(cases)
@unittest.skipUnless(os.name == "posix", "Requires 'test' command on system") @unittest.skipUnless(os.name == "posix", "Requires 'test' command on system")
...@@ -187,7 +205,7 @@ class FindmatchTest(unittest.TestCase): ...@@ -187,7 +205,7 @@ class FindmatchTest(unittest.TestCase):
([caps, "test/pass", "test"], {}, ("test 1 -eq 1", {"test": "test 1 -eq 1"})), ([caps, "test/pass", "test"], {}, ("test 1 -eq 1", {"test": "test 1 -eq 1"})),
# findmatch will return None because test/fail evaluates to false # findmatch will return None because test/fail evaluates to false
([caps, "test/fail", "test"], {}, (None, None)) ([caps, "test/fail", "test"], {}, (None, None))
] ]
self._run_cases(cases) self._run_cases(cases)
def _run_cases(self, cases): def _run_cases(self, cases):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment