Skip to content

Commit

Permalink
fix has_expr string formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
James O'Reilly authored and JamesDanielOReilly committed Jul 29, 2020
1 parent 20adb3f commit cac7226
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
19 changes: 15 additions & 4 deletions pythonwhat/checks/has_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,24 @@ def has_expr(
"expr_code": expr_code,
}

fmt_kwargs["stu_eval"] = utils.shorten_str(str(eval_stu))
fmt_kwargs["sol_eval"] = utils.shorten_str(str(eval_sol))
fmt_kwargs["stu_eval"] = str(eval_stu)
fmt_kwargs["sol_eval"] = str(eval_sol)

# wrap in quotes if eval_sol or eval_stu are strings
if test == "value":
if isinstance(eval_stu, str):
fmt_kwargs["stu_eval"] = '\'{}\''.format(fmt_kwargs["stu_eval"])
if isinstance(eval_sol, str):
fmt_kwargs["sol_eval"] = '\'{}\''.format(fmt_kwargs["sol_eval"])

# check if student or solution evaluations are too long or contain newlines
if incorrect_msg == DEFAULT_INCORRECT_MSG and (
fmt_kwargs["stu_eval"] is None
or fmt_kwargs["sol_eval"] is None
utils.unshowable_string(fmt_kwargs["stu_eval"])
or utils.unshowable_string(fmt_kwargs["sol_eval"])
or fmt_kwargs["stu_eval"] == fmt_kwargs["sol_eval"]
):
fmt_kwargs["stu_eval"] = None
fmt_kwargs["sol_eval"] = None
incorrect_msg = "Expected something different."

# tests ---
Expand Down
6 changes: 2 additions & 4 deletions pythonwhat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ def v2_only():
return not include_v1()


def shorten_str(text, to_chars=100):
if "\n" in text or len(text) > 50:
return None
return text
def unshowable_string(text):
return "\n" in text or len(text) > 50


def copy_env(env):
Expand Down
49 changes: 48 additions & 1 deletion tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def test_check_call(stu, patt):
[
(
"echo_word = (lambda word1, echo: word1 * echo * 2)",
"Check the first lambda function. To verify it, we reran it with the arguments `('test', 2)`. Expected `testtest`, but got `testtesttesttest`.",
"Check the first lambda function. To verify it, we reran it with the arguments `('test', 2)`. Expected `'testtest'`, but got `'testtesttesttest'`.",
)
],
)
Expand Down Expand Up @@ -695,6 +695,53 @@ def test_has_equal_x_2(stu, patt, cols, cole):
assert lines(output, cols, cole)


def test_has_equal_value_wrap_string():
sol = """print(' , ')"""
stu = """print(', ')"""
sct = """Ex().check_function('print', index=0, signature=False).check_args(0).has_equal_value(copy = False)"""
output = helper.run(
{
"DC_CODE": stu,
"DC_SOLUTION": sol,
"DC_SCT": sct,
}
)
assert not output["correct"]
assert output["message"] == "Check your call of <code>print()</code>. Did you correctly specify the first argument? Expected <code>' , '</code>, but got <code>', '</code>." # nopep8


## Testing output edge cases -------------------------------------------------


def test_has_equal_value_dont_wrap_newline():
sol = """print('\\n')"""
stu = """print('text')"""
sct = """Ex().check_function('print', index=0, signature=False).check_args(0).has_equal_value()"""
output = helper.run(
{
"DC_CODE": stu,
"DC_SOLUTION": sol,
"DC_SCT": sct,
}
)
assert not output["correct"]
assert output["message"] == "Check your call of <code>print()</code>. Did you correctly specify the first argument? Expected something different." # nopep8


def test_has_equal_value_dont_wrap_too_long():
sol = """print('short text')"""
stu = """print('This text is longer than 50 characters if I copy it 3 times. This text is longer than 50 characters if I copy it 3 times. This text is longer than 50 characters if I copy it 3 times.')""" # nopep8
sct = """Ex().check_function('print', index=0, signature=False).check_args(0).has_equal_value()"""
output = helper.run(
{
"DC_CODE": stu,
"DC_SOLUTION": sol,
"DC_SCT": sct,
}
)
assert not output["correct"]
assert output["message"] == "Check your call of <code>print()</code>. Did you correctly specify the first argument? Expected something different." # nopep8

## Check has no error ---------------------------------------------------------


Expand Down

0 comments on commit cac7226

Please sign in to comment.