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

Function test_if_else

pythonwhat/test_funcs/test_compound_statement.py:27–86  ·  view source on GitHub ↗

Test parts of the if statement. This test function will allow you to extract parts of a specific if statement and perform a set of tests specifically on these parts. A for loop consists of three potential parts: the condition test, :code:`test`, which specifies the condition of the if s

(state, index=1, test=None, body=None, orelse=None)

Source from the content-addressed store, hash-verified

25
26
27def test_if_else(state, index=1, test=None, body=None, orelse=None):
28 """Test parts of the if statement.
29
30 This test function will allow you to extract parts of a specific if statement and perform a set of tests
31 specifically on these parts. A for loop consists of three potential parts: the condition test, :code:`test`,
32 which specifies the condition of the if statement, the :code:`body`, which is what's executed if the condition is
33 True and a else part, :code:`orelse`, which will be executed if the condition is not True.::
34
35 if 5 == 3:
36 print("success")
37 else:
38 print("fail")
39
40 Has :code:`5 == 3` as the condition test, :code:`print("success")` as the body and :code:`print("fail")` as the else part.
41
42 Args:
43 index (int): index of the function call to be checked. Defaults to 1.
44 test: this argument holds the part of code that will be ran to check the condition test of the if statement.
45 It should be passed as a lambda expression or a function definition. The functions that are ran should
46 be other pythonwhat test functions, and they will be tested specifically on only the condition test of
47 the if statement.
48 body: this argument holds the part of code that will be ran to check the body of the if statement.
49 It should be passed as a lambda expression or a function definition. The functions that are ran should
50 be other pythonwhat test functions, and they will be tested specifically on only the body of
51 the if statement.
52 orelse: this argument holds the part of code that will be ran to check the else part of the if statement.
53 It should be passed as a lambda expression or a function definition. The functions that are ran should
54 be other pythonwhat test functions, and they will be tested specifically on only the else part of
55 the if statement.
56
57 :Example:
58
59 Student code::
60
61 a = 12
62 if a > 3:
63 print('test %d' % a)
64
65 Solution code::
66
67 a = 4
68 if a > 3:
69 print('test %d' % a)
70
71 SCT::
72
73 test_if_else(1,
74 body = test_expression_output(
75 extra_env = { 'a': 5 }
76 incorrect_msg = "Print out the correct things"))
77
78 This SCT will pass as :code:`test_expression_output()` is ran on the body of the if statement and it will output
79 the same thing in the solution as in the student code.
80 """
81 state = check_node(
82 state, "if_elses", index - 1, typestr="{{ordinal}} if expression"
83 )
84 multi(check_part(state, "test", "condition"), test)

Callers

nothing calls this directly

Calls 2

check_nodeFunction · 0.90
check_partFunction · 0.90

Tested by

no test coverage detected