Add a FuzzyUrl config type.

This commit is contained in:
Florian Bruhin 2015-02-27 08:08:46 +01:00
parent 6d51fcfb2e
commit 5f46870594
2 changed files with 69 additions and 0 deletions

View File

@ -1101,6 +1101,30 @@ class SearchEngineUrl(BaseType):
url.errorString()))
class FuzzyUrl(BaseType):
"""A single URL."""
def validate(self, value):
from qutebrowser.utils import urlutils
if not value:
if self._none_ok:
return
else:
raise configexc.ValidationError(value, "may not be empty!")
try:
self.transform(value)
except urlutils.FuzzyUrlError as e:
raise configexc.ValidationError(value, str(e))
def transform(self, value):
from qutebrowser.utils import urlutils
if not value:
return None
else:
return urlutils.fuzzy_url(value, do_search=False)
class Encoding(BaseType):
"""Setting for a python encoding."""

View File

@ -1780,6 +1780,51 @@ class SearchEngineUrlTests(unittest.TestCase):
self.assertEqual(self.t.transform("foobar"), "foobar")
class FuzzyUrlTests(unittest.TestCase):
"""Test FuzzyUrl."""
def setUp(self):
self.t = configtypes.FuzzyUrl()
def test_validate_empty(self):
"""Test validate with empty string and none_ok = False."""
with self.assertRaises(configexc.ValidationError):
self.t.validate('')
def test_validate_empty_none_ok(self):
"""Test validate with empty string and none_ok = True."""
t = configtypes.FuzzyUrl(none_ok=True)
t.validate('')
def test_validate(self):
"""Test validate with a good value."""
self.t.validate('http://example.com/?q={}')
def test_validate_good_fuzzy(self):
"""Test validate with a good fuzzy value."""
self.t.validate('example.com')
def test_validate_invalid_url(self):
"""Test validate with an invalid URL."""
with self.assertRaises(configexc.ValidationError):
self.t.validate('::foo')
def test_validate_invalid_search(self):
"""Test validate with an invalid search term."""
with self.assertRaises(configexc.ValidationError):
self.t.validate('foo bar')
def test_transform_empty(self):
"""Test transform with an empty value."""
self.assertIsNone(self.t.transform(''))
def test_transform(self):
"""Test transform with a value."""
self.assertEqual(self.t.transform("example.com"),
QUrl('http://example.com'))
class UserStyleSheetTests(unittest.TestCase):
"""Test UserStyleSheet."""