MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / test_args

Function test_args

pythonwhat/test_funcs/test_compound_statement.py:339–390  ·  view source on GitHub ↗
(
    state, arg_names, arg_defaults, nb_args_msg, arg_names_msg, arg_defaults_msg
)

Source from the content-addressed store, hash-verified

337
338
339def test_args(
340 state, arg_names, arg_defaults, nb_args_msg, arg_names_msg, arg_defaults_msg
341):
342
343 MSG_NUM_ARGS = "You should define {{parent[typestr]}} with {{sol_len}} arguments, instead got {{stu_len}}."
344 MSG_BAD_ARG_NAME = "The {{parent[ordinal]}} {{parent[part]}} should be called `{{sol_part[name]}}`, instead got `{{stu_part[name]}}`."
345 MSG_BAD_DEFAULT = (
346 "The {{parent[part]}} `{{stu_part[name]}}` should have no default."
347 )
348 MSG_INC_DEFAULT = (
349 "The {{parent[part]}} `{{stu_part[name]}}` does not have the correct default."
350 )
351
352 MSG_NO_VARARG = "Have you specified an argument to take a `*` argument and named it `{{sol_part['*args'][name]}}`?"
353 MSG_NO_KWARGS = "Have you specified an argument to take a `**` argument and named it `{{sol_part['**kwargs'][name]}}`?"
354 MSG_VARARG_NAME = "Have you specified an argument to take a `*` argument and named it `{{sol_part[name]}}`?"
355 MSG_KWARG_NAME = "Have you specified an argument to take a `**` argument and named it `{{sol_part[name]}}`?"
356
357 if arg_names or arg_defaults:
358 # test number of args
359 has_equal_part_len(state, "_spec1_args", nb_args_msg or MSG_NUM_ARGS)
360
361 # iterate over each arg, testing name and default
362 for ii in range(len(state.solution_parts["_spec1_args"])):
363 # get argument state
364 arg_state = check_part_index(
365 state, "_spec1_args", ii, "argument", "NO MISSING MSG"
366 )
367 # test exact name
368 has_equal_part(arg_state, "name", arg_names_msg or MSG_BAD_ARG_NAME)
369
370 if arg_defaults:
371 # test whether is default
372 has_equal_part(
373 arg_state, "is_default", arg_defaults_msg or MSG_BAD_DEFAULT
374 )
375 # test default value, use if to prevent running a process no default
376 if arg_state.solution_parts["is_default"]:
377 has_equal_value(
378 arg_state,
379 incorrect_msg=arg_defaults_msg or MSG_INC_DEFAULT,
380 append=True,
381 )
382
383 # test *args and **kwargs
384 if state.solution_parts["*args"]:
385 vararg = check_part(state, "*args", "", missing_msg=MSG_NO_VARARG)
386 has_equal_part(vararg, "name", MSG_VARARG_NAME)
387
388 if state.solution_parts["**kwargs"]:
389 kwarg = check_part(state, "**kwargs", "", missing_msg=MSG_NO_KWARGS)
390 has_equal_part(kwarg, "name", MSG_KWARG_NAME)
391
392
393def test_expression_result(

Callers 1

test_function_definitionFunction · 0.85

Calls 4

has_equal_part_lenFunction · 0.90
check_part_indexFunction · 0.90
has_equal_partFunction · 0.90
check_partFunction · 0.90

Tested by

no test coverage detected