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

Function test_with

pythonwhat/test_funcs/test_compound_statement.py:466–531  ·  view source on GitHub ↗

Test a with statement. with open_file('...') as bla: [ open_file('...').__enter__() ] with open_file('...') as file: [ ]

(
    state,
    index,
    context_vals=False,  # whether to check number of context vals
    context_tests=None,  # check on context expressions
    body=None,
    undefined_msg=None,
    context_vals_len_msg=None,
    context_vals_msg=None,
)

Source from the content-addressed store, hash-verified

464
465
466def test_with(
467 state,
468 index,
469 context_vals=False, # whether to check number of context vals
470 context_tests=None, # check on context expressions
471 body=None,
472 undefined_msg=None,
473 context_vals_len_msg=None,
474 context_vals_msg=None,
475):
476 """Test a with statement.
477with open_file('...') as bla:
478
479 [ open_file('...').__enter__() ]
480
481
482with open_file('...') as file:
483 [ ]
484
485 """
486
487 MSG_NUM_CTXT = "Make sure to use the correct number of context variables. It seems you defined too many."
488 MSG_NUM_CTXT2 = "Make sure to use the correct number of context variables. It seems you defined too little."
489 MSG_CTXT_NAMES = "Make sure to use the correct context variable names. Was expecting `{{sol_vars}}` but got `{{stu_vars}}`."
490
491 check_with = partial(
492 check_node, state, "withs", index - 1, "{{ordinal}} `with` statement"
493 )
494
495 child = check_with()
496 child2 = check_with()
497
498 if context_vals:
499 # test context var names ----
500 has_context(
501 child, incorrect_msg=context_vals_msg or MSG_CTXT_NAMES, exact_names=True
502 )
503
504 # test num context vars ----
505 has_equal_part_len(child, "context", MSG_NUM_CTXT)
506
507 # Context sub tests ----
508 if context_tests and not isinstance(context_tests, list):
509 context_tests = [context_tests]
510
511 for i, context_test in enumerate(context_tests or []):
512 # partial the substate check, because the function uses two prepended messages
513 def check_context(state):
514 return check_part_index(
515 state,
516 "context",
517 i,
518 "%s context" % get_ord(i + 1),
519 missing_msg=MSG_NUM_CTXT2,
520 )
521
522 check_context(child) # test exist
523

Callers

nothing calls this directly

Calls 5

has_contextFunction · 0.90
has_equal_part_lenFunction · 0.90
check_partFunction · 0.90
with_contextFunction · 0.90
check_contextFunction · 0.85

Tested by

no test coverage detected