configtypes: Allow completions for String.
This commit is contained in:
parent
2d4adf4476
commit
bb5e5137cd
@ -236,10 +236,11 @@ class String(BaseType):
|
|||||||
minlen: Minimum length (inclusive).
|
minlen: Minimum length (inclusive).
|
||||||
maxlen: Maximum length (inclusive).
|
maxlen: Maximum length (inclusive).
|
||||||
forbidden: Forbidden chars in the string.
|
forbidden: Forbidden chars in the string.
|
||||||
|
_completions: completions to be used, or None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, minlen=None, maxlen=None, forbidden=None,
|
def __init__(self, minlen=None, maxlen=None, forbidden=None,
|
||||||
none_ok=False):
|
none_ok=False, completions=None):
|
||||||
super().__init__(none_ok)
|
super().__init__(none_ok)
|
||||||
if minlen is not None and minlen < 1:
|
if minlen is not None and minlen < 1:
|
||||||
raise ValueError("minlen ({}) needs to be >= 1!".format(minlen))
|
raise ValueError("minlen ({}) needs to be >= 1!".format(minlen))
|
||||||
@ -251,6 +252,7 @@ class String(BaseType):
|
|||||||
self.minlen = minlen
|
self.minlen = minlen
|
||||||
self.maxlen = maxlen
|
self.maxlen = maxlen
|
||||||
self.forbidden = forbidden
|
self.forbidden = forbidden
|
||||||
|
self._completions = completions
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
self._basic_validation(value)
|
self._basic_validation(value)
|
||||||
@ -267,6 +269,9 @@ class String(BaseType):
|
|||||||
raise configexc.ValidationError(value, "must be at most {} chars "
|
raise configexc.ValidationError(value, "must be at most {} chars "
|
||||||
"long!".format(self.maxlen))
|
"long!".format(self.maxlen))
|
||||||
|
|
||||||
|
def complete(self):
|
||||||
|
return self._completions
|
||||||
|
|
||||||
|
|
||||||
class List(BaseType):
|
class List(BaseType):
|
||||||
|
|
||||||
|
@ -334,6 +334,14 @@ class TestString:
|
|||||||
def test_transform(self, klass):
|
def test_transform(self, klass):
|
||||||
assert klass().transform('foobar') == 'foobar'
|
assert klass().transform('foobar') == 'foobar'
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('value', [
|
||||||
|
None,
|
||||||
|
['one', 'two'],
|
||||||
|
[('1', 'one'), ('2', 'two')],
|
||||||
|
])
|
||||||
|
def test_complete(self, klass, value):
|
||||||
|
assert klass(completions=value).complete() == value
|
||||||
|
|
||||||
|
|
||||||
class TestList:
|
class TestList:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user