Skip to content

Commit 1064680

Browse files
authored
Merge pull request #37 from pddg/fix/get_options
Fix get_options to return a new list instead of class attribute
2 parents 7532a7f + 72c18ed commit 1064680

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

tests/test_command.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
def commands():
1414
return RootCommand(), SecondCommand(), ThirdCommand()
1515

16+
class Opt1(Option):
17+
def build_option(self, parser):
18+
return parser
19+
20+
class Opt2(Option):
21+
def build_option(self, parser):
22+
return parser
23+
24+
class Opt3(Option):
25+
def build_option(self, parser):
26+
return parser
27+
1628

1729
def get_sub_commands(cmd_set):
1830
if len(cmd_set) == 0:
@@ -338,3 +350,34 @@ def get_options(self):
338350
cmd_parser = cmd.create_default_parser()
339351
args = cmd_parser.parse_args(argv)
340352
assert args.test == 'test'
353+
354+
@pytest.mark.parametrize(
355+
'option_objs', [
356+
[Opt1(), Opt2(), Opt3()],
357+
]
358+
)
359+
def test_get_options(self, option_objs):
360+
additional_opt = Opt1()
361+
class SourceCommand(RootCommand):
362+
options = option_objs
363+
class Cmd(SourceCommand):
364+
def get_options(self):
365+
opts =super(Cmd, self).get_options()
366+
opts.append(additional_opt)
367+
return opts
368+
root = SourceCommand()
369+
source_opts = root.get_options()
370+
cmd = Cmd()
371+
actual_options = cmd.get_options()
372+
expected_options = option_objs + [additional_opt]
373+
assert len(actual_options) == len(expected_options)
374+
# All options are instantiated
375+
types = map(type, actual_options)
376+
bools = map(lambda x: x != type, types)
377+
assert all(bools)
378+
# All class is correct
379+
actual_classes = map(lambda x: type(x), actual_options)
380+
expected_classes = map(lambda x: x if type(x) == type else type(x), expected_options)
381+
assert list(actual_classes) == list(expected_classes)
382+
# Inheritance source class is not modified
383+
assert RootCommand().get_options() == []

uroboros/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def get_options(self) -> 'List[Option]':
226226
Get all `Option` instance of this `Command`.
227227
:return: List of Option instance
228228
"""
229-
return self.options
229+
return [opt() if type(opt) == type else opt for opt in self.options]
230230

231231
def print_help(self):
232232
"""

0 commit comments

Comments
 (0)