Skip to content

Commit cac7226

Browse files
James O'ReillyJamesDanielOReilly
authored andcommitted
fix has_expr string formatting
1 parent 20adb3f commit cac7226

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

pythonwhat/checks/has_funcs.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,24 @@ def has_expr(
339339
"expr_code": expr_code,
340340
}
341341

342-
fmt_kwargs["stu_eval"] = utils.shorten_str(str(eval_stu))
343-
fmt_kwargs["sol_eval"] = utils.shorten_str(str(eval_sol))
342+
fmt_kwargs["stu_eval"] = str(eval_stu)
343+
fmt_kwargs["sol_eval"] = str(eval_sol)
344+
345+
# wrap in quotes if eval_sol or eval_stu are strings
346+
if test == "value":
347+
if isinstance(eval_stu, str):
348+
fmt_kwargs["stu_eval"] = '\'{}\''.format(fmt_kwargs["stu_eval"])
349+
if isinstance(eval_sol, str):
350+
fmt_kwargs["sol_eval"] = '\'{}\''.format(fmt_kwargs["sol_eval"])
351+
352+
# check if student or solution evaluations are too long or contain newlines
344353
if incorrect_msg == DEFAULT_INCORRECT_MSG and (
345-
fmt_kwargs["stu_eval"] is None
346-
or fmt_kwargs["sol_eval"] is None
354+
utils.unshowable_string(fmt_kwargs["stu_eval"])
355+
or utils.unshowable_string(fmt_kwargs["sol_eval"])
347356
or fmt_kwargs["stu_eval"] == fmt_kwargs["sol_eval"]
348357
):
358+
fmt_kwargs["stu_eval"] = None
359+
fmt_kwargs["sol_eval"] = None
349360
incorrect_msg = "Expected something different."
350361

351362
# tests ---

pythonwhat/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ def v2_only():
1111
return not include_v1()
1212

1313

14-
def shorten_str(text, to_chars=100):
15-
if "\n" in text or len(text) > 50:
16-
return None
17-
return text
14+
def unshowable_string(text):
15+
return "\n" in text or len(text) > 50
1816

1917

2018
def copy_env(env):

tests/test_messaging.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def test_check_call(stu, patt):
532532
[
533533
(
534534
"echo_word = (lambda word1, echo: word1 * echo * 2)",
535-
"Check the first lambda function. To verify it, we reran it with the arguments `('test', 2)`. Expected `testtest`, but got `testtesttesttest`.",
535+
"Check the first lambda function. To verify it, we reran it with the arguments `('test', 2)`. Expected `'testtest'`, but got `'testtesttesttest'`.",
536536
)
537537
],
538538
)
@@ -695,6 +695,53 @@ def test_has_equal_x_2(stu, patt, cols, cole):
695695
assert lines(output, cols, cole)
696696

697697

698+
def test_has_equal_value_wrap_string():
699+
sol = """print(' , ')"""
700+
stu = """print(', ')"""
701+
sct = """Ex().check_function('print', index=0, signature=False).check_args(0).has_equal_value(copy = False)"""
702+
output = helper.run(
703+
{
704+
"DC_CODE": stu,
705+
"DC_SOLUTION": sol,
706+
"DC_SCT": sct,
707+
}
708+
)
709+
assert not output["correct"]
710+
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
711+
712+
713+
## Testing output edge cases -------------------------------------------------
714+
715+
716+
def test_has_equal_value_dont_wrap_newline():
717+
sol = """print('\\n')"""
718+
stu = """print('text')"""
719+
sct = """Ex().check_function('print', index=0, signature=False).check_args(0).has_equal_value()"""
720+
output = helper.run(
721+
{
722+
"DC_CODE": stu,
723+
"DC_SOLUTION": sol,
724+
"DC_SCT": sct,
725+
}
726+
)
727+
assert not output["correct"]
728+
assert output["message"] == "Check your call of <code>print()</code>. Did you correctly specify the first argument? Expected something different." # nopep8
729+
730+
731+
def test_has_equal_value_dont_wrap_too_long():
732+
sol = """print('short text')"""
733+
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
734+
sct = """Ex().check_function('print', index=0, signature=False).check_args(0).has_equal_value()"""
735+
output = helper.run(
736+
{
737+
"DC_CODE": stu,
738+
"DC_SOLUTION": sol,
739+
"DC_SCT": sct,
740+
}
741+
)
742+
assert not output["correct"]
743+
assert output["message"] == "Check your call of <code>print()</code>. Did you correctly specify the first argument? Expected something different." # nopep8
744+
698745
## Check has no error ---------------------------------------------------------
699746

700747

0 commit comments

Comments
 (0)