@@ -167,7 +167,11 @@ def has_equal_ast(state, incorrect_msg=None, code=None, exact=True, append=None)
167167 ): # if not specified, set to False if incorrect_msg was manually specified
168168 append = incorrect_msg is None
169169 if incorrect_msg is None :
170- incorrect_msg = "Expected `{{sol_str}}`, but got `{{stu_str}}`."
170+ incorrect_msg = (
171+ "Expected \n ```\n {{sol_str}}\n ```\n , but got \n ```\n {{stu_str}}\n ```\n "
172+ if utils .is_multiline_code (state .student_code , state .solution_code )
173+ else "Expected `{{sol_str}}`, but got `{{stu_str}}`."
174+ )
171175
172176 def parse_tree (tree ):
173177 # get contents of module.body if only 1 element
@@ -183,10 +187,18 @@ def parse_tree(tree):
183187 stu_rep = parse_tree (state .student_ast )
184188 sol_rep = parse_tree (state .solution_ast if not code else ast .parse (code ))
185189
186- fmt_kwargs = {
187- "sol_str" : state .solution_code if not code else code ,
188- "stu_str" : state .student_code ,
189- }
190+ if utils .is_multiline_code (state .student_code , state .solution_code ):
191+ fmt_kwargs = {
192+ "sol_str" : utils .format_code (state .solution_code )
193+ if not code
194+ else utils .format_code (code ),
195+ "stu_str" : utils .format_code (state .student_code ),
196+ }
197+ else :
198+ fmt_kwargs = {
199+ "sol_str" : state .solution_code if not code else code ,
200+ "stu_str" : state .student_code ,
201+ }
190202
191203 if exact and not code :
192204 state .do_test (
@@ -345,19 +357,20 @@ def has_expr(
345357 # wrap in quotes if eval_sol or eval_stu are strings
346358 if test == "value" :
347359 if isinstance (eval_stu , str ):
348- fmt_kwargs ["stu_eval" ] = ' \' {} \' ' .format (fmt_kwargs ["stu_eval" ])
360+ fmt_kwargs ["stu_eval" ] = "'{}'" .format (fmt_kwargs ["stu_eval" ])
349361 if isinstance (eval_sol , str ):
350- fmt_kwargs ["sol_eval" ] = ' \' {} \' ' .format (fmt_kwargs ["sol_eval" ])
362+ fmt_kwargs ["sol_eval" ] = "'{}'" .format (fmt_kwargs ["sol_eval" ])
351363
352364 # reformat student evaluation string if it is too long
353365 fmt_kwargs ["stu_eval" ] = utils .shorten_string (fmt_kwargs ["stu_eval" ])
354366
355367 # check if student or solution evaluations are too long or contain newlines
356368 if incorrect_msg == DEFAULT_INCORRECT_MSG and (
357- len (fmt_kwargs ["sol_eval" ]) > 50 or
358- utils .has_newline (fmt_kwargs ["stu_eval" ]) or
359- utils .has_newline (fmt_kwargs ["sol_eval" ]) or
360- fmt_kwargs ["stu_eval" ] == fmt_kwargs ["sol_eval" ]):
369+ len (fmt_kwargs ["sol_eval" ]) > 50
370+ or utils .has_newline (fmt_kwargs ["stu_eval" ])
371+ or utils .has_newline (fmt_kwargs ["sol_eval" ])
372+ or fmt_kwargs ["stu_eval" ] == fmt_kwargs ["sol_eval" ]
373+ ):
361374 fmt_kwargs ["stu_eval" ] = None
362375 fmt_kwargs ["sol_eval" ] = None
363376 incorrect_msg = "Expected something different."
0 commit comments