forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_for_loop.py
More file actions
72 lines (55 loc) · 3.17 KB
/
test_for_loop.py
File metadata and controls
72 lines (55 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pythonwhat.Reporter import Reporter
from pythonwhat.check_funcs import check_part, check_node, multi
from functools import partial
MSG_MISSING = "FMT:Define more {typestr}."
MSG_PREPEND = "FMT:Check your code in the {child[part]} of the {ordinal} for loop. "
def test_for_loop(index=1,
for_iter=None,
body=None,
orelse=None,
expand_message=True,
state=None):
"""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 can have a else part as well, `orelse`, but
this is almost never used.::
for i in range(10):
print(i)
Has :code:`range(10)` as the sequence and :code:`print(i)` as the body.
Args:
index (int): index of the function call to be checked. Defaults to 1.
for_iter: this argument holds the part of code that will be ran to check the sequence of the for loop.
It should be passed as a lambda expression or a function. The functions that are ran should
be other pythonwhat test functions, and they will be tested specifically on only the sequence part of
the for loop.
body: this argument holds the part of code that will be ran to check the body of the for loop.
It should be passed as a lambda expression or a function. The functions that are ran should
be other pythonwhat test functions, and they will be tested specifically on only the body of
the for loop.
orelse: this argument holds the part of code that will be ran to check the else part of the for loop.
It should be passed as a lambda expression or a function. The functions that are ran should
be other pythonwhat test functions, and they will be tested specifically on only the else part of
the for loop.
expand_message (bool): if true, feedback messages will be expanded with :code:`in the ___ of the for loop on
line ___`. Defaults to True. If False, :code:`test_for_loop()` will generate no extra feedback.
:Example:
Student code::
for i in range(10):
print(i)
Solution code::
for n in range(10):
print(n)
SCT::
test_for_loop(1,
for_iter = test_function("range"),
body = test_expression_output(context_val = [5])
This SCT will evaluate to True as the function :code:`range` is used in the sequence and the function
:code:`test_exression_output()` will pass on the body code.
"""
rep = Reporter.active_reporter
state = check_node('for_loops', index-1, "`for` loops", MSG_MISSING, MSG_PREPEND, state=state)
# TODO for_iter is a level up, so shouldn't have targets set, but this is done is check_node
multi(for_iter, state = check_part('iter', 'sequence part', state))
multi(body, state = check_part('body', 'body', state))
multi(orelse, state = check_part('orelse', 'else part', state))