|
13 | 13 | def commands(): |
14 | 14 | return RootCommand(), SecondCommand(), ThirdCommand() |
15 | 15 |
|
| 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 | + |
16 | 28 |
|
17 | 29 | def get_sub_commands(cmd_set): |
18 | 30 | if len(cmd_set) == 0: |
@@ -338,3 +350,34 @@ def get_options(self): |
338 | 350 | cmd_parser = cmd.create_default_parser() |
339 | 351 | args = cmd_parser.parse_args(argv) |
340 | 352 | 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() == [] |
0 commit comments