test_if_exp

test_if_exp(index=1, test=None, body=None, orelse=None, expand_message=True, state=None)

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, test, which specifies the condition of the if statement, the body, which is what’s executed if the condition is True and a else part, orelse, which will be executed if the condition is not True.:

if 5 == 3:
    print("success")
else:
    print("fail")

Has 5 == 3 as the condition test, print("success") as the body and print("fail") as the else part.

Parameters:
  • index (int) – index of the function call to be checked. Defaults to 1.
  • test – this argument holds the part of code that will be ran to check the condition test of the if statement. It should be passed as a lambda expression or a function definition. The functions that are ran should be other pythonwhat test functions, and they will be tested specifically on only the condition test of the if statement.
  • body – this argument holds the part of code that will be ran to check the body of the if statement. It should be passed as a lambda expression or a function definition. The functions that are ran should be other pythonwhat test functions, and they will be tested specifically on only the body of the if statement.
  • orelse – this argument holds the part of code that will be ran to check the else part of the if statement. It should be passed as a lambda expression or a function definition. The functions that are ran should be other pythonwhat test functions, and they will be tested specifically on only the else part of the if statement.
  • expand_message (bool) – if true, feedback messages will be expanded with in the ___ of the if statement on line ___. Defaults to True. If False, test_if_else() will generate no extra feedback.
Example:

Student code:

a = 12
if a > 3:
    print('test %d' % a)

Solution code:

a = 4
if a > 3:
    print('test %d' % a)

SCT:

test_if_else(1,
    body = test_expression_output(
            extra_env = { 'a': 5 }
            incorrect_msg = "Print out the correct things"))

This SCT will pass as test_expression_output() is ran on the body of the if statement and it will output the same thing in the solution as in the student code.

test_if_exp is a wrapper around test_if_else, which tells it to look for inline if expressions. As such, it uses the same arguments. See.

What is an inline if expression?

An inline if expression looks like..

x = 'a' if True else 'b'

This is in contrast to an if block, which looks like..

if True:
    x = 'a'
else:
    x = 'b'

Parts

This test tries to break code into 3 parts, BODY, TEST, and ORELSE. The table below shows an example inline if expression on the left, and the parts that would be extracted on the right.

| code | parts breakdown | | ————————- | ——————— | | x = 'a' if True else 'b' | x = BODY if TEST else ORELSE |

Nested if expressions

Just like test_if_else, test_if_exp will not find a nested if expression. Instead, the nested portion will be inside one of the parts. For example, below is an exercise with an if expression in the ORELSE part of another if expression.

*** =solution

x = 'a' if True else ('b' if False else 'c')

*** =sct

test_if_exp(orelse=lambda: test_if_exp(orelse=lambda: test_student_typed('c')))

The SCT above tests that the student typed ‘c’ in the ORELSE part of the inner if expression. In parts, this looks like..

BODY1 if TEST1 else (ORELSE1 = BODY2 if TEST2 else ORELSE2)