Add utils.raises. Closes #274.

This commit is contained in:
Florian Bruhin 2014-11-27 20:44:48 +01:00
parent c87fa34544
commit 68b9aaace4
3 changed files with 63 additions and 14 deletions

View File

@ -340,5 +340,46 @@ class IsEnumTests(unittest.TestCase):
self.assertFalse(utils.is_enum(23))
class RaisesTests(unittest.TestCase):
"""Test raises."""
def do_raise(self):
raise Exception
def do_nothing(self):
pass
def test_raises_single_exc_true(self):
"""Test raises with a single exception which gets raised."""
self.assertTrue(utils.raises(ValueError, int, 'a'))
def test_raises_single_exc_false(self):
"""Test raises with a single exception which does not get raised."""
self.assertFalse(utils.raises(ValueError, int, '1'))
def test_raises_multiple_exc_true(self):
"""Test raises with multiple exceptions which get raised."""
self.assertTrue(utils.raises((ValueError, TypeError), int, 'a'))
self.assertTrue(utils.raises((ValueError, TypeError), int, None))
def test_raises_multiple_exc_false(self):
"""Test raises with multiple exceptions which do not get raised."""
self.assertFalse(utils.raises((ValueError, TypeError), int, '1'))
def test_no_args(self):
"""Test with no args and an exception which gets raised."""
self.assertTrue(utils.raises(Exception, self.do_raise))
def test_no_args(self):
"""Test with no args and an exception which does not get raised."""
self.assertFalse(utils.raises(Exception, self.do_nothing))
def test_unrelated_exception(self):
"""Test with an unrelated exception."""
with self.assertRaises(Exception):
utils.raises(ValueError, self.do_raise)
if __name__ == '__main__':
unittest.main()

View File

@ -28,7 +28,7 @@ from PyQt5.QtCore import QUrl
from PyQt5.QtNetwork import QHostInfo
from qutebrowser.config import config
from qutebrowser.utils import log, qtutils, message
from qutebrowser.utils import log, qtutils, message, utils
from qutebrowser.commands import cmdexc
@ -68,18 +68,6 @@ def _get_search_url(txt):
return url
def _is_numeric(s):
"""Check if the given string is some valid number."""
try:
int(s, 0)
except ValueError:
try:
float(s)
except ValueError:
return False
return True
def _is_url_naive(urlstr):
"""Naive check if given URL is really a URL.
@ -101,7 +89,8 @@ def _is_url_naive(urlstr):
# Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs
# which we don't want to. Note we already filtered *real* valid IPs
# above.
if _is_numeric(urlstr):
if ((not utils.raises(ValueError, int, urlstr, 0)) or
(not utils.raises(ValueError, float, urlstr))):
return False
if not url.isValid():

View File

@ -544,3 +544,22 @@ def qualname(obj):
return "{}.{}".format(obj.__module__, name)
else:
return name
def raises(exc, func, *args):
"""Check if a function raises a given exception.
Args:
exc: A single exception or an iterable of exceptions.
func: A function to call.
*args: The arguments to pass to the function.
Returns:
True if the exception was raised, False otherwise.
"""
try:
func(*args)
except exc:
return True
else:
return False