Test parts of the while loop. This test function will allow you to extract parts of a specific while loop and perform a set of tests specifically on these parts. A while loop generally consists of two parts: the condition test, :code:`test`, which is the condition that is tested each lo
(state, index=1, test=None, body=None, orelse=None)
| 142 | |
| 143 | |
| 144 | def test_while_loop(state, index=1, test=None, body=None, orelse=None): |
| 145 | """Test parts of the while loop. |
| 146 | |
| 147 | This test function will allow you to extract parts of a specific while loop and perform a set of tests |
| 148 | specifically on these parts. A while loop generally consists of two parts: the condition test, :code:`test`, |
| 149 | which is the condition that is tested each loop, and the :code:`body`. A for while can have a else part as well, |
| 150 | :code:`orelse`, but this is almost never used.:: |
| 151 | |
| 152 | a = 10 |
| 153 | while a < 5: |
| 154 | print(a) |
| 155 | a -= 1 |
| 156 | |
| 157 | Has :code:`a < 5` as the condition test and `print(i)` as the body. |
| 158 | |
| 159 | Args: |
| 160 | index (int): index of the function call to be checked. Defaults to 1. |
| 161 | test: this argument holds the part of code that will be ran to check the condition test of the while loop. |
| 162 | It should be passed as a lambda expression or a function definition. The functions that are ran should |
| 163 | be other pythonwhat test functions, and they will be tested specifically on only the condition test of |
| 164 | the while loop. |
| 165 | body: this argument holds the part of code that will be ran to check the body of the while loop. |
| 166 | It should be passed as a lambda expression or a function definition. The functions that are ran should |
| 167 | be other pythonwhat test functions, and they will be tested specifically on only the body of |
| 168 | the while loop. |
| 169 | orelse: this argument holds the part of code that will be ran to check the else part of the while loop. |
| 170 | It should be passed as a lambda expression or a function definition. The functions that are ran should |
| 171 | be other pythonwhat test functions, and they will be tested specifically on only the else part of |
| 172 | the while loop. |
| 173 | |
| 174 | :Example: |
| 175 | |
| 176 | Student code:: |
| 177 | |
| 178 | a = 10 |
| 179 | while a < 5: |
| 180 | print(a) |
| 181 | a -= 1 |
| 182 | |
| 183 | Solution code:: |
| 184 | |
| 185 | a = 20 |
| 186 | while a < 5: |
| 187 | print(a) |
| 188 | a -= 1 |
| 189 | |
| 190 | SCT:: |
| 191 | |
| 192 | test_while_loop(1, |
| 193 | test = test_expression_result({"a": 5}), |
| 194 | body = test_expression_output({"a": 5})) |
| 195 | |
| 196 | This SCT will evaluate to True as condition test will have thes same result in student |
| 197 | and solution code and `test_exression_output()` will pass on the body code. |
| 198 | """ |
| 199 | state = check_node(state, "whiles", index - 1, "{{ordinal}} while loop") |
| 200 | multi(check_part(state, "test", "condition"), test) |
| 201 | multi(check_part(state, "body", "body"), body) |
nothing calls this directly
no test coverage detected