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

Function test_comp

pythonwhat/test_funcs/test_compound_statement.py:552–603  ·  view source on GitHub ↗
(
    state,
    typestr,
    comptype,
    index,
    iter_vars_names,
    not_called_msg,
    insufficient_ifs_msg,
    incorrect_iter_vars_msg,
    comp_iter,
    ifs,
    key=None,
    body=None,
    value=None,
    rep=None,
)

Source from the content-addressed store, hash-verified

550
551
552def test_comp(
553 state,
554 typestr,
555 comptype,
556 index,
557 iter_vars_names,
558 not_called_msg,
559 insufficient_ifs_msg,
560 incorrect_iter_vars_msg,
561 comp_iter,
562 ifs,
563 key=None,
564 body=None,
565 value=None,
566 rep=None,
567):
568
569 MSG_INCORRECT_ITER_VARS = "Have you used the correct iterator variables?"
570 MSG_INCORRECT_NUM_ITER_VARS = "Have you used {{num_vars}} iterator variables?"
571 MSG_INSUFFICIENT_IFS = "Have you used {{sol_len}} ifs?"
572
573 # make sure other messages are set to default if None
574 if insufficient_ifs_msg is None:
575 insufficient_ifs_msg = MSG_INSUFFICIENT_IFS
576
577 # get comprehension
578 child = check_node(state, comptype, index - 1, typestr, missing_msg=not_called_msg)
579
580 # test comprehension iter and its variable names (or number of variables)
581 if comp_iter:
582 multi(check_part(child, "iter", "iterable part"), comp_iter)
583
584 # test iterator variables
585 default_msg = (
586 MSG_INCORRECT_ITER_VARS if iter_vars_names else MSG_INCORRECT_NUM_ITER_VARS
587 )
588 has_context(child, incorrect_iter_vars_msg or default_msg, iter_vars_names)
589
590 # test the main expressions.
591 if body:
592 multi(check_part(child, "body", "body"), body) # list and gen comp
593 if key:
594 multi(check_part(child, "key", "key part"), key) # dict comp
595 if value:
596 multi(check_part(child, "value", "value part"), value) # ""
597
598 # test a list of ifs. each entry corresponds to a filter in the comprehension.
599 for i, if_test in enumerate(ifs or []):
600 # test that ifs are same length
601 has_equal_part_len(child, "ifs", insufficient_ifs_msg)
602 # test individual ifs
603 multi(check_part_index(child, "ifs", i, get_ord(i + 1) + " if"), if_test)

Callers 1

test_list_compFunction · 0.85

Calls 5

check_nodeFunction · 0.90
check_partFunction · 0.90
has_contextFunction · 0.90
has_equal_part_lenFunction · 0.90
check_part_indexFunction · 0.90

Tested by

no test coverage detected