diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 58bd0b807..5a6a524e7 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -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.""" diff --git a/qutebrowser/test/config/test_configtypes.py b/qutebrowser/test/config/test_configtypes.py index e50db31cd..3ff7de7bc 100644 --- a/qutebrowser/test/config/test_configtypes.py +++ b/qutebrowser/test/config/test_configtypes.py @@ -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."""