Test parts of the for loop. This test function will allow you to extract parts of a specific for loop and perform a set of tests specifically on these parts. A for loop consists of two parts: the sequence, `for_iter`, which is the values over which are looped, and the `body`. A for loop
(state, index=1, for_iter=None, body=None, orelse=None)
| 87 | |
| 88 | |
| 89 | def test_for_loop(state, index=1, for_iter=None, body=None, orelse=None): |
| 90 | """Test parts of the for loop. |
| 91 | |
| 92 | This test function will allow you to extract parts of a specific for loop and perform a set of tests |
| 93 | specifically on these parts. A for loop consists of two parts: the sequence, `for_iter`, which is the |
| 94 | values over which are looped, and the `body`. A for loop can have a else part as well, `orelse`, but |
| 95 | this is almost never used.:: |
| 96 | |
| 97 | for i in range(10): |
| 98 | print(i) |
| 99 | |
| 100 | Has :code:`range(10)` as the sequence and :code:`print(i)` as the body. |
| 101 | |
| 102 | Args: |
| 103 | index (int): index of the function call to be checked. Defaults to 1. |
| 104 | for_iter: this argument holds the part of code that will be ran to check the sequence of the for loop. |
| 105 | It should be passed as a lambda expression or a function. The functions that are ran should |
| 106 | be other pythonwhat test functions, and they will be tested specifically on only the sequence part of |
| 107 | the for loop. |
| 108 | body: this argument holds the part of code that will be ran to check the body of the for loop. |
| 109 | It should be passed as a lambda expression or a function. The functions that are ran should |
| 110 | be other pythonwhat test functions, and they will be tested specifically on only the body of |
| 111 | the for loop. |
| 112 | orelse: this argument holds the part of code that will be ran to check the else part of the for loop. |
| 113 | It should be passed as a lambda expression or a function. The functions that are ran should |
| 114 | be other pythonwhat test functions, and they will be tested specifically on only the else part of |
| 115 | the for loop. |
| 116 | |
| 117 | :Example: |
| 118 | Student code:: |
| 119 | |
| 120 | for i in range(10): |
| 121 | print(i) |
| 122 | |
| 123 | Solution code:: |
| 124 | |
| 125 | for n in range(10): |
| 126 | print(n) |
| 127 | |
| 128 | SCT:: |
| 129 | |
| 130 | test_for_loop(1, |
| 131 | for_iter = test_function("range"), |
| 132 | body = test_expression_output(context_val = [5]) |
| 133 | |
| 134 | This SCT will evaluate to True as the function :code:`range` is used in the sequence and the function |
| 135 | :code:`test_exression_output()` will pass on the body code. |
| 136 | """ |
| 137 | state = check_node(state, "for_loops", index - 1, "{{ordinal}} for loop") |
| 138 | |
| 139 | multi(check_part(state, "iter", "sequence part"), for_iter) |
| 140 | multi(check_part(state, "body", "body"), body) |
| 141 | multi(check_part(state, "orelse", "else part"), orelse) |
| 142 | |
| 143 | |
| 144 | def test_while_loop(state, index=1, test=None, body=None, orelse=None): |
nothing calls this directly
no test coverage detected