MCPcopy Index your code
hub / github.com/python/cpython / ParserTesterMetaclass

Class ParserTesterMetaclass

Lib/test/test_argparse.py:222–335  ·  view source on GitHub ↗

Adds parser tests using the class attributes. Classes of this type should specify the following attributes: argument_signatures -- a list of Sig objects which specify the signatures of Argument objects to be created failures -- a list of args lists that should cause the parser

Source from the content-addressed store, hash-verified

220
221
222class ParserTesterMetaclass(type):
223 """Adds parser tests using the class attributes.
224
225 Classes of this type should specify the following attributes:
226
227 argument_signatures -- a list of Sig objects which specify
228 the signatures of Argument objects to be created
229 failures -- a list of args lists that should cause the parser
230 to fail
231 successes -- a list of (initial_args, options, remaining_args) tuples
232 where initial_args specifies the string args to be parsed,
233 options is a dict that should match the vars() of the options
234 parsed out of initial_args, and remaining_args should be any
235 remaining unparsed arguments
236 """
237
238 def __init__(cls, name, bases, bodydict):
239 if name == 'ParserTestCase':
240 return
241
242 # default parser signature is empty
243 if not hasattr(cls, 'parser_signature'):
244 cls.parser_signature = Sig()
245 if not hasattr(cls, 'parser_class'):
246 cls.parser_class = ErrorRaisingArgumentParser
247
248 # ---------------------------------------
249 # functions for adding optional arguments
250 # ---------------------------------------
251 def no_groups(parser, argument_signatures):
252 """Add all arguments directly to the parser"""
253 for sig in argument_signatures:
254 parser.add_argument(*sig.args, **sig.kwargs)
255
256 def one_group(parser, argument_signatures):
257 """Add all arguments under a single group in the parser"""
258 group = parser.add_argument_group('foo')
259 for sig in argument_signatures:
260 group.add_argument(*sig.args, **sig.kwargs)
261
262 def many_groups(parser, argument_signatures):
263 """Add each argument in its own group to the parser"""
264 for i, sig in enumerate(argument_signatures):
265 group = parser.add_argument_group('foo:%i' % i)
266 group.add_argument(*sig.args, **sig.kwargs)
267
268 # --------------------------
269 # functions for parsing args
270 # --------------------------
271 def listargs(parser, args):
272 """Parse the args by passing in a list"""
273 return parser.parse_args(args)
274
275 def sysargs(parser, args):
276 """Parse the args by defaulting to sys.argv"""
277 old_sys_argv = sys.argv
278 sys.argv = [old_sys_argv[0]] + args
279 try:

Callers 1

test_argparse.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…