| 284 | # class that holds the combination of one optional argument |
| 285 | # addition method and one arg parsing method |
| 286 | class AddTests(object): |
| 287 | |
| 288 | def __init__(self, tester_cls, add_arguments, parse_args): |
| 289 | self._add_arguments = add_arguments |
| 290 | self._parse_args = parse_args |
| 291 | |
| 292 | add_arguments_name = self._add_arguments.__name__ |
| 293 | parse_args_name = self._parse_args.__name__ |
| 294 | for test_func in [self.test_failures, self.test_successes]: |
| 295 | func_name = test_func.__name__ |
| 296 | names = func_name, add_arguments_name, parse_args_name |
| 297 | test_name = '_'.join(names) |
| 298 | |
| 299 | def wrapper(self, test_func=test_func): |
| 300 | test_func(self) |
| 301 | try: |
| 302 | wrapper.__name__ = test_name |
| 303 | except TypeError: |
| 304 | pass |
| 305 | setattr(tester_cls, test_name, wrapper) |
| 306 | |
| 307 | def _get_parser(self, tester): |
| 308 | args = tester.parser_signature.args |
| 309 | kwargs = tester.parser_signature.kwargs |
| 310 | parser = tester.parser_class(*args, **kwargs) |
| 311 | self._add_arguments(parser, tester.argument_signatures) |
| 312 | return parser |
| 313 | |
| 314 | def test_failures(self, tester): |
| 315 | parser = self._get_parser(tester) |
| 316 | for args_str in tester.failures: |
| 317 | args = args_str.split() |
| 318 | with tester.subTest(args=args): |
| 319 | with tester.assertRaises(ArgumentParserError, msg=args): |
| 320 | parser.parse_args(args) |
| 321 | |
| 322 | def test_successes(self, tester): |
| 323 | parser = self._get_parser(tester) |
| 324 | for args, expected_ns in tester.successes: |
| 325 | if isinstance(args, str): |
| 326 | args = args.split() |
| 327 | with tester.subTest(args=args): |
| 328 | result_ns = self._parse_args(parser, args) |
| 329 | tester.assertEqual(expected_ns, result_ns) |
| 330 | |
| 331 | # add tests for each combination of an optionals adding method |
| 332 | # and an arg parsing method |