Skip to content

Commit 9245237

Browse files
committed
actually, don't mutate state.highlight, specify highlight in unittests
1 parent 53c81c7 commit 9245237

7 files changed

Lines changed: 65 additions & 69 deletions

File tree

pythonwhat/test_funcs/test_function.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ def test_function(name,
6767
rep = Reporter.active_reporter
6868
rep.set_tag("fun", "test_function")
6969

70-
if not highlight: state.highlight = None
71-
7270
index = index - 1
7371

7472
eq_map = {"equal": EqualTest}
@@ -202,7 +200,7 @@ def test_print(index = 1,
202200
params_not_matched_msg="Have you correctly called `print()`?",
203201
params_not_specified_msg="Have you correctly called `print()`?",
204202
incorrect_msg="Have you printed out the correct object?",
205-
highlight=True,
203+
highlight=False,
206204
state=None):
207205
test_function_v2("print",
208206
index=index,
@@ -263,11 +261,6 @@ def test_function_v2(name,
263261
rep = Reporter.active_reporter
264262
rep.set_tag("fun", "test_function")
265263

266-
# Note that this mutates state, which is bad. The easiest way to avoid this
267-
# would be to make a copy of state, but that would break blacklisting here,
268-
# which also requires state mutation.
269-
if not highlight: state.highlight = None
270-
271264
index = index - 1
272265
eq_map = {"equal": EqualTest}
273266

tests/test_test_comp.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def setUp(self):
1515
iter_vars_names=True,
1616
incorrect_iter_vars_msg=None,
1717
body=lambda: test_expression_result(context_vals = ['a', 2]),
18-
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False]),
19-
lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False])],
18+
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], highlight=True),
19+
lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], highlight=True)],
2020
insufficient_ifs_msg=None,
2121
expand_message=True)
2222
'''
@@ -96,8 +96,8 @@ def test_pass_mix_lam(self):
9696
iter_vars_names=True,
9797
incorrect_iter_vars_msg=None,
9898
body=test_expression_result(context_vals = ['a', 2]),
99-
ifs=[test_function_v2('isinstance', params = ['obj'], do_eval = [False]),
100-
test_function_v2('isinstance', params = ['obj'], do_eval = [False])],
99+
ifs=[test_function_v2('isinstance', params = ['obj'], do_eval = [False], highlight=True),
100+
test_function_v2('isinstance', params = ['obj'], do_eval = [False], highlight=True)],
101101
insufficient_ifs_msg=None,
102102
expand_message=True)
103103
'''
@@ -124,8 +124,8 @@ def setUp(self):
124124
iter_vars_names=True,
125125
incorrect_iter_vars_msg='incorrectitervars',
126126
body=lambda: test_expression_result(context_vals = ['a', 2], incorrect_msg = 'bodyincorrect'),
127-
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], not_called_msg = 'notcalled1', incorrect_msg = 'incorrect2'),
128-
lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], not_called_msg = 'notcalled2', incorrect_msg = 'incorrect2')],
127+
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], not_called_msg = 'notcalled1', incorrect_msg = 'incorrect2', highlight=True),
128+
lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], not_called_msg = 'notcalled2', incorrect_msg = 'incorrect2', highlight=True)],
129129
insufficient_ifs_msg='insufficientifs')
130130
'''
131131
}
@@ -311,7 +311,7 @@ def setUp(self):
311311
incorrect_iter_vars_msg=None,
312312
key=lambda: test_expression_result(context_vals = ['a']),
313313
value=lambda: test_expression_result(context_vals = ['a']),
314-
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False])],
314+
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], highlight=True)],
315315
insufficient_ifs_msg=None,
316316
expand_message=True)
317317
'''
@@ -395,8 +395,8 @@ def setUp(self):
395395
iter_vars_names=True,
396396
incorrect_iter_vars_msg=None,
397397
body=lambda: test_expression_result(context_vals = ['a', 2]),
398-
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False]),
399-
lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False])],
398+
ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], highlight=True),
399+
lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False], highlight=True)],
400400
insufficient_ifs_msg=None,
401401
expand_message=True)
402402
'''

tests/test_test_for_loop.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def setUp(self):
1515
''',
1616
"DC_SCT": '''
1717
test_for_loop(1,
18-
lambda: test_function("range"),
18+
lambda: test_function("range", highlight=True),
1919
lambda: test_object_after_expression("size", {"size": 1}, [1]))
2020
success_msg("Great!")
2121
'''
@@ -88,7 +88,7 @@ def setUp(self):
8888
''',
8989
"DC_SCT": '''
9090
msg = "loopinggonewrong"
91-
test_for_loop(1, for_iter=lambda msg=msg: test_function("enumerate", incorrect_msg = msg))
91+
test_for_loop(1, for_iter=lambda msg=msg: test_function("enumerate", incorrect_msg = msg, highlight=True))
9292
9393
msg = "blabla"
9494
test_for_loop(1, body=lambda msg=msg: test_expression_output(incorrect_msg = msg, context_vals = [2, "test"]))
@@ -137,7 +137,7 @@ def test_Pass_spec(self):
137137
forl = Ex().check_for_loop(0)
138138
139139
forl.check_iter()\
140-
.multi(test_function("enumerate", incorrect_msg=msg))
140+
.multi(test_function("enumerate", incorrect_msg=msg, highlight=True))
141141
142142
msg = "blabla"
143143
forl.check_body()\
@@ -165,7 +165,7 @@ def setUp(self):
165165
.check_for_loop(0)\
166166
.check_body()\
167167
.set_context(jj=2)\
168-
.multi(test_function('sum', incorrect_msg="wronginnerfor"))
168+
.multi(test_function('sum', incorrect_msg="wronginnerfor", highlight=True))
169169
'''
170170
}
171171

tests/test_test_function.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def test_line_numbers1(self):
458458
self.data = {"DC_PEC": '',
459459
"DC_SOLUTION": "round(1.23456, ndigits = 1)",
460460
"DC_CODE": "round(1.34567, ndigits = 1)",
461-
"DC_SCT": "test_function('round', index = 1)"}
461+
"DC_SCT": "test_function('round', index = 1, highlight=True)"}
462462
sct_payload = helper.run(self.data)
463463
self.assertFalse(sct_payload['correct'])
464464
self.assertIn("Did you call <code>round()</code> with the correct arguments? The first argument seems to be incorrect.", sct_payload['message'])
@@ -468,7 +468,7 @@ def test_line_numbers(self):
468468
self.data = {"DC_PEC": '',
469469
"DC_SOLUTION": "round(1.23456, ndigits = 1)",
470470
"DC_CODE": "round(1.23456, ndigits = 3)",
471-
"DC_SCT": "test_function('round', index = 1)"}
471+
"DC_SCT": "test_function('round', index = 1, highlight=True)"}
472472
sct_payload = helper.run(self.data)
473473
self.assertFalse(sct_payload['correct'])
474474
self.assertIn("Did you call <code>round()</code> with the correct arguments? Keyword <code>ndigits</code> seems to be incorrect.", sct_payload['message'])
@@ -488,7 +488,7 @@ def test_nested_arg2(self):
488488
self.data = {"DC_PEC": '',
489489
"DC_SOLUTION": "print(type([1, 2, 3]))",
490490
"DC_CODE": "print(type([1, 2, 4]))",
491-
"DC_SCT": "test_function('type')"}
491+
"DC_SCT": "test_function('type', highlight=True)"}
492492
sct_payload = helper.run(self.data)
493493
self.assertFalse(sct_payload['correct'])
494494
helper.test_lines(self, sct_payload, 1, 1, 12, 20)
@@ -497,15 +497,15 @@ def test_nested_keyw1(self):
497497
self.data = {"DC_PEC": '',
498498
"DC_SOLUTION": "round(1.1234, ndigits = max([1, 2, 3]))",
499499
"DC_CODE": "round(1.1234, ndigits = max([1, 2, 3]))",
500-
"DC_SCT": "test_function('max')"}
500+
"DC_SCT": "test_function('max', highlight=True)"}
501501
sct_payload = helper.run(self.data)
502502
self.assertTrue(sct_payload['correct'])
503503

504504
def test_nested_keyw2(self):
505505
self.data = {"DC_PEC": '',
506506
"DC_SOLUTION": "round(1.1234, ndigits = max([1, 2, 3]))",
507507
"DC_CODE": "round(1.1234, ndigits = max([1, 2]))",
508-
"DC_SCT": "test_function('max')"}
508+
"DC_SCT": "test_function('max', highlight=True)"}
509509
sct_payload = helper.run(self.data)
510510
self.assertFalse(sct_payload['correct'])
511511
helper.test_lines(self, sct_payload, 1, 1, 29, 34)
@@ -558,7 +558,7 @@ def test_do_eval_none_fail1(self):
558558
self.data = {"DC_PEC": '',
559559
"DC_SOLUTION": "round(123.123, ndigits = 2)",
560560
"DC_CODE": "round(123.123)",
561-
"DC_SCT": "test_function('round', do_eval = None)"}
561+
"DC_SCT": "test_function('round', do_eval = None, highlight=True)"}
562562
sct_payload = helper.run(self.data)
563563
self.assertFalse(sct_payload['correct'])
564564
self.assertEqual("Have you specified all required arguments inside <code>round()</code>?", sct_payload['message'])
@@ -568,7 +568,7 @@ def test_do_eval_none_fail2(self):
568568
self.data = {"DC_PEC": '',
569569
"DC_SOLUTION": "round(123.123)", # args = [0]
570570
"DC_CODE": "round(number = 123.123)", # student args is len 0
571-
"DC_SCT": "test_function('round', do_eval = None)"}
571+
"DC_SCT": "test_function('round', do_eval = None, highlight=True)"}
572572
sct_payload = helper.run(self.data)
573573
self.assertFalse(sct_payload['correct'])
574574
self.assertEqual("Have you specified all required arguments inside <code>round()</code>?", sct_payload['message'])
@@ -578,7 +578,7 @@ def test_do_eval_none_fail3(self):
578578
self.data = {"DC_PEC": '',
579579
"DC_SOLUTION": "round(123.123, 2)", # args = [0, 1]
580580
"DC_CODE": "round(123.123)", # student_args is len 1
581-
"DC_SCT": "test_function('round', do_eval = None)"}
581+
"DC_SCT": "test_function('round', do_eval = None, highlight=True)"}
582582
sct_payload = helper.run(self.data)
583583
self.assertFalse(sct_payload['correct'])
584584
self.assertEqual("Have you specified all required arguments inside <code>round()</code>?", sct_payload['message'])
@@ -594,9 +594,9 @@ def setUp(self):
594594
print([1, 2, 3])
595595
''',
596596
"DC_SCT": '''
597-
test_function("print", index = 1)
598-
test_function("print", index = 2)
599-
test_function("print", index = 3)
597+
test_function("print", index = 1, highlight=True)
598+
test_function("print", index = 2, highlight=True)
599+
test_function("print", index = 3, highlight=True)
600600
'''
601601
}
602602
self.DC_SCT_SPEC2 = '''

0 commit comments

Comments
 (0)