Skip to content

Commit

Permalink
test(test_miscellaneous): added a missing test for class FirstOf
Browse files Browse the repository at this point in the history
There was a missing test to check the name of the custom type given at instantiation level
  • Loading branch information
lewoudar committed Aug 13, 2022
1 parent 3f2d7e6 commit a0032cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
12 changes: 6 additions & 6 deletions click_params/miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ class FirstOf(CustomParamType):
def __init__(self, *param_types: click.ParamType, name: Optional[str] = None, return_param: bool = False):
self.param_types = param_types
self.return_param = return_param
if not getattr(self, "name", None):
if not getattr(self, 'name', None):
if name:
self.name = name
else:
# Set name to union representation of individual params.
# Using pipe | as thats used by python sets for union.
self.name = "(" + " | ".join(p.name for p in self.param_types) + ")"
# Using pipe | as that is used by python sets for union.
self.name = '(' + ' | '.join(p.name for p in self.param_types) + ')'

def convert(
self, value: str, param: Optional[click.Parameter], ctx: Optional[click.Context]
Expand All @@ -104,13 +104,13 @@ def convert(
fails.append((param_type, str(e)))

self.fail(
"All possible options exhausted without any successful conversion:\n - "
'All possible options exhausted without any successful conversion:\n - '
+ "\n - ".join(
[
indent(
f"{getattr(f[0], 'name', f[0].__class__.__name__).upper()}:"
f" {f[1]}",
" ",
f' {f[1]}',
' ',
)
for f in fails
]
Expand Down
7 changes: 4 additions & 3 deletions tests/test_miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class CoreNumber(FirstOf):
name = 'core number'

assert 'CORE NUMBER' == repr(CoreNumber(click.INT, click.Choice(['all', 'half'])))
assert "(INTEGER | CHOICE)" == repr(FirstOf(click.INT, click.Choice(['all', 'half'])))
assert 'CORE NUMBER' == repr(FirstOf(click.INT, click.Choice(['all', 'half']), name='core number'))
assert '(INTEGER | CHOICE)' == repr(FirstOf(click.INT, click.Choice(['all', 'half'])))

@pytest.mark.parametrize(('expression', 'param_types', 'value'), [
('12', (click.INT,), 12),
Expand All @@ -165,7 +166,7 @@ def test_should_parse_expression_successfully(self, expression, param_types, val
('12', (click.Choice(['auto', 'full']), click.INT), click.INT),
('12.3', (click.Choice(['auto', 'full']), click.INT, click.FLOAT), click.FLOAT)
])
def test_should_return_correct_paramtype(self, expression, param_types, expected_param_type):
def test_should_return_correct_param_type(self, expression, param_types, expected_param_type):
union_type = FirstOf(*param_types, return_param=True)
(param_type, _) = union_type.convert(expression, None, None)
assert repr(expected_param_type) == repr(param_type)
Expand All @@ -177,5 +178,5 @@ def test_should_return_correct_paramtype(self, expression, param_types, expected
])
def test_should_parse_expression_unsuccessfully(self, expression, param_types):
union_type = FirstOf(*param_types)
with pytest.raises(click.BadParameter, match=r'.*\n - '.join(p.name.upper() for p in param_types)) as e:
with pytest.raises(click.BadParameter, match=r'.*\n - '.join(p.name.upper() for p in param_types)):
union_type.convert(expression, None, None)

0 comments on commit a0032cb

Please sign in to comment.