Changeset - 5231a1784f87
[Not reviewed]
0 2 0
Brett Smith - 3 years ago 2021-02-24 18:15:33
brettcsmith@brettcsmith.org
cliutil: EnumArgument matches user arguments against aliases.
2 files changed with 13 insertions and 7 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/cliutil.py
Show inline comments
...
 
@@ -91,13 +91,17 @@ class EnumArgument(Generic[ET]):
 
    def __init__(self, base: Type[ET]) -> None:
 
        self.base = base
 

	
 
    def enum_type(self, arg: str) -> ET:
 
        """Return a single enum whose name matches the user argument"""
 
        regexp = re.compile(re.escape(arg), re.IGNORECASE)
 
        matches = frozenset(choice for choice in self.base if regexp.match(choice.name))
 
        matches = frozenset(
 
            choice
 
            for name, choice in self.base.__members__.items()
 
            if regexp.match(name)
 
        )
 
        count = len(matches)
 
        if count == 1:
 
            return next(iter(matches))
 
        elif count:
 
            names = ', '.join(repr(choice.name) for choice in matches)
 
            raise ValueError(f"ambiguous argument {arg!r}: matches {names}")
tests/test_cliutil.py
Show inline comments
...
 
@@ -29,12 +29,14 @@ FILE_NAMES = ['-foobar', '-foo.bin']
 
STREAM_PATHS = [None, Path('-')]
 

	
 
class ArgChoices(enum.Enum):
 
    AA = 'aa'
 
    AB = 'ab'
 
    BB = 'bb'
 
    START = AA
 
    END = BB
 

	
 

	
 
class MockTraceback:
 
    def __init__(self, stack=None, index=0):
 
        if stack is None:
 
            stack = inspect.stack(context=False)
...
 
@@ -267,25 +269,25 @@ def test_diff_year(date, diff, expected):
 
    (['false', '--version'], False),
 
    ([str(testutil.TESTS_DIR)], False),
 
])
 
def test_can_run(cmd, expected):
 
    assert cliutil.can_run(cmd) == expected
 

	
 
@pytest.mark.parametrize('choice', ArgChoices)
 
def test_enum_arg_enum_type(arg_enum, choice):
 
    assert arg_enum.enum_type(choice.name) is choice
 
@pytest.mark.parametrize('name,choice', ArgChoices.__members__.items())
 
def test_enum_arg_enum_type(arg_enum, name, choice):
 
    assert arg_enum.enum_type(name.lower()) is choice
 
    assert arg_enum.enum_type(choice.value) is choice
 

	
 
@pytest.mark.parametrize('arg', 'az\0')
 
def test_enum_arg_no_enum_match(arg_enum, arg):
 
    with pytest.raises(ValueError):
 
        arg_enum.enum_type(arg)
 

	
 
@pytest.mark.parametrize('choice', ArgChoices)
 
def test_enum_arg_value_type(arg_enum, choice):
 
    assert arg_enum.value_type(choice.name) == choice.value
 
@pytest.mark.parametrize('name,choice', ArgChoices.__members__.items())
 
def test_enum_arg_value_type(arg_enum, name, choice):
 
    assert arg_enum.value_type(name.lower()) == choice.value
 
    assert arg_enum.value_type(choice.value) == choice.value
 

	
 
@pytest.mark.parametrize('arg', 'az\0')
 
def test_enum_arg_no_value_match(arg_enum, arg):
 
    with pytest.raises(ValueError):
 
        arg_enum.value_type(arg)
0 comments (0 inline, 0 general)