Add all possible combinations to FlagList.complete

This commit is contained in:
Daniel 2015-10-02 18:46:31 +02:00
parent f12fbe875e
commit 73ba3ddaee
2 changed files with 46 additions and 16 deletions

View File

@ -268,6 +268,8 @@ class FlagList(List):
self.valid_values (if not empty).
"""
combinable_values = None
def validate(self, value):
self._basic_validation(value)
if not value:
@ -290,6 +292,25 @@ class FlagList(List):
raise configexc.ValidationError(
value, "List contains invalid values!")
def complete(self):
if self.valid_values is None:
return None
out = []
# Single value completions
for value in self.valid_values:
desc = self.valid_values.descriptions.get(value, "")
out.append((value, desc))
combinables = self.combinable_values
if combinables is None:
combinables = list(self.valid_values)
# Generate combinations of each possible value combination
for size in range(2, len(combinables) + 1):
for combination in itertools.combinations(combinables, size):
out.append((','.join(combination), ''))
return out
class Bool(BaseType):
@ -1400,22 +1421,6 @@ class ConfirmQuit(FlagList):
raise configexc.ValidationError(
value, "List cannot contain always!")
def complete(self):
combinations = []
# Generate combinations of the options that can be combined
for size in range(2, len(self.combinable_values) + 1):
combinations += list(
itertools.combinations(self.combinable_values, size))
out = []
# Add valid single values
for val in self.valid_values:
out.append((val, self.valid_values.descriptions[val]))
# Add combinations to list of options
for val in combinations:
desc = ''
out.append((','.join(val), desc))
return out
class ForwardUnboundKeys(BaseType):

View File

@ -371,6 +371,7 @@ class FlagListSubclass(configtypes.FlagList):
"""
valid_values = configtypes.ValidValues('foo', 'bar', 'baz')
combinable_values = ['foo', 'bar']
class TestFlagList:
@ -412,6 +413,30 @@ class TestFlagList:
def test_validate_values_none(self, klass_valid_none, val):
klass_valid_none().validate(val)
def test_complete(self, klass):
"""Test completing by doing some samples."""
completions = [e[0] for e in klass().complete()]
assert 'foo' in completions
assert 'bar' in completions
assert 'baz' in completions
assert 'foo,bar' in completions
for val in completions:
assert 'baz,' not in val
assert ',baz' not in val
def test_complete_all_valid_values(self, klass):
inst = klass()
inst.combinable_values = None
completions = [e[0] for e in inst.complete()]
assert 'foo' in completions
assert 'bar' in completions
assert 'baz' in completions
assert 'foo,bar' in completions
assert 'foo,baz' in completions
def test_complete_no_valid_values(self, klass_valid_none):
assert klass_valid_none().complete() == None
class TestBool: