diff --git a/tests/unit/config/test_configutils.py b/tests/unit/config/test_configutils.py index e961948d5..29305c6ed 100644 --- a/tests/unit/config/test_configutils.py +++ b/tests/unit/config/test_configutils.py @@ -19,6 +19,8 @@ import pytest +from PyQt5.QtCore import QUrl + from qutebrowser.config import configutils, configdata, configtypes from qutebrowser.utils import urlmatch @@ -39,13 +41,27 @@ def opt(): raw_backends=None, description=None) @pytest.fixture -def values(opt): - pattern = urlmatch.UrlPattern('*://www.example.com/') +def pattern(): + return urlmatch.UrlPattern('*://www.example.com/') + + +@pytest.fixture +def other_pattern(): + return urlmatch.UrlPattern('https://www.example.org/') + + +@pytest.fixture +def values(opt, pattern): scoped_values = [configutils.ScopedValue('global value', None), configutils.ScopedValue('example value', pattern)] return configutils.Values(opt, scoped_values) +@pytest.fixture +def empty_values(opt): + return configutils.Values(opt) + + def test_repr(opt, values): expected = ("qutebrowser.config.configutils.Values(opt={!r}, " "values=[ScopedValue(value='global value', pattern=None), " @@ -63,6 +79,126 @@ def test_str(values): assert str(values) == '\n'.join(expected) -def test_str_empty(opt): - values = configutils.Values(opt) - assert str(values) == 'example.option: ' +def test_str_empty(empty_values): + assert str(empty_values) == 'example.option: ' + + +def test_bool(values, empty_values): + assert values + assert not empty_values + + +def test_iter(values): + assert list(iter(values)) == list(iter(values._values)) + + +def test_add_existing(values): + values.add('new global value') + assert values.get_for_url() == 'new global value' + + +def test_add_new(values, other_pattern): + values.add('example.org value', other_pattern) + assert values.get_for_url() == 'global value' + example_com = QUrl('https://www.example.com/') + example_org = QUrl('https://www.example.org/') + assert values.get_for_url(example_com) == 'example value' + assert values.get_for_url(example_org) == 'example.org value' + + +def test_remove_existing(values, pattern): + removed = values.remove(pattern) + assert removed + + url = QUrl('https://www.example.com/') + assert values.get_for_url(url) == 'global value' + + +def test_remove_non_existing(values, other_pattern): + removed = values.remove(other_pattern) + assert not removed + + url = QUrl('https://www.example.com/') + assert values.get_for_url(url) == 'example value' + + +def test_clear(values): + assert values + values.clear() + assert not values + assert values.get_for_url(fallback=False) is configutils.UNSET + + +def test_get_matching(values): + url = QUrl('https://www.example.com/') + assert values.get_for_url(url, fallback=False) == 'example value' + + +def test_get_unset(empty_values): + assert empty_values.get_for_url(fallback=False) is configutils.UNSET + + +def test_get_no_global(empty_values, other_pattern): + empty_values.add('example.org value', pattern) + assert empty_values.get_for_url(fallback=False) is configutils.UNSET + + +def test_get_unset_fallback(empty_values): + assert empty_values.get_for_url() == 'default value' + + +def test_get_non_matching(values): + url = QUrl('https://www.example.ch/') + assert values.get_for_url(url, fallback=False) is configutils.UNSET + + +def test_get_non_matching_fallback(values): + url = QUrl('https://www.example.ch/') + assert values.get_for_url(url) == 'global value' + + +def test_get_multiple_matches(values): + """With multiple matching pattern, the last added should win.""" + all_pattern = urlmatch.UrlPattern('*://*/') + values.add('new value', all_pattern) + url = QUrl('https://www.example.com/') + assert values.get_for_url(url) == 'new value' + + +def test_get_matching_pattern(values, pattern): + assert values.get_for_pattern(pattern, fallback=False) == 'example value' + + +def test_get_unset_pattern(empty_values, pattern): + value = empty_values.get_for_pattern(pattern, fallback=False) + assert value is configutils.UNSET + + +def test_get_no_global_pattern(empty_values, pattern, other_pattern): + empty_values.add('example.org value', other_pattern) + value = empty_values.get_for_pattern(pattern, fallback=False) + assert value is configutils.UNSET + + +def test_get_unset_fallback_pattern(empty_values, pattern): + assert empty_values.get_for_pattern(pattern) == 'default value' + + +def test_get_non_matching_pattern(values, other_pattern): + value = values.get_for_pattern(other_pattern, fallback=False) + assert value is configutils.UNSET + + +def test_get_non_matching_fallback_pattern(values, other_pattern): + assert values.get_for_pattern(other_pattern) == 'global value' + + +def test_get_equivalent_patterns(empty_values): + """With multiple matching pattern, the last added should win.""" + pat1 = urlmatch.UrlPattern('https://www.example.com/') + pat2 = urlmatch.UrlPattern('https://www.example.com') + empty_values.add('pat1 value', pat1) + empty_values.add('pat2 value', pat2) + + assert empty_values.get_for_pattern(pat1) == 'pat1 value' + assert empty_values.get_for_pattern(pat2) == 'pat2 value'