configtypes: Allow completions for String.

This commit is contained in:
Florian Bruhin 2015-10-25 21:18:06 +01:00
parent 2d4adf4476
commit bb5e5137cd
2 changed files with 14 additions and 1 deletions

View File

@ -236,10 +236,11 @@ class String(BaseType):
minlen: Minimum length (inclusive).
maxlen: Maximum length (inclusive).
forbidden: Forbidden chars in the string.
_completions: completions to be used, or None
"""
def __init__(self, minlen=None, maxlen=None, forbidden=None,
none_ok=False):
none_ok=False, completions=None):
super().__init__(none_ok)
if minlen is not None and minlen < 1:
raise ValueError("minlen ({}) needs to be >= 1!".format(minlen))
@ -251,6 +252,7 @@ class String(BaseType):
self.minlen = minlen
self.maxlen = maxlen
self.forbidden = forbidden
self._completions = completions
def validate(self, value):
self._basic_validation(value)
@ -267,6 +269,9 @@ class String(BaseType):
raise configexc.ValidationError(value, "must be at most {} chars "
"long!".format(self.maxlen))
def complete(self):
return self._completions
class List(BaseType):

View File

@ -334,6 +334,14 @@ class TestString:
def test_transform(self, klass):
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: