Skip to content

Commit 41c304a

Browse files
authored
Merge pull request #220 from datacamp/feature-expr-kwargs
Let old scts pass kwargs for has_equal_value
2 parents 5ef9fbf + 5365969 commit 41c304a

6 files changed

Lines changed: 46 additions & 18 deletions

File tree

pythonwhat/test_funcs/test_expression_output.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def test_expression_output(extra_env=None,
77
expr_code=None,
88
pre_code=None,
99
keep_objs_in_env=None,
10-
state=None):
10+
state=None,
11+
**kwargs):
1112
"""Test output of expression.
1213
1314
The code of the student is ran in the active state and the output it generates is
@@ -37,6 +38,7 @@ def test_expression_output(extra_env=None,
3738
keep_obj_in_env (list()): a list of variable names that should be hold in the copied environment where
3839
the expression is evaluated. All primitive types are copied automatically, other objects have to
3940
be passed explicitely.
41+
kwargs: named arguments which are the same as those used by ``has_equal_value``.
4042
4143
:Example:
4244
Student code::
@@ -83,4 +85,5 @@ def test_expression_output(extra_env=None,
8385
expr_code=expr_code,
8486
pre_code=pre_code,
8587
keep_objs_in_env=keep_objs_in_env,
86-
state = state)
88+
state = state,
89+
**kwargs)

pythonwhat/test_funcs/test_expression_result.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def test_expression_result(extra_env=None,
1515
pre_code=None,
1616
keep_objs_in_env=None,
1717
error_msg=None,
18-
state=None):
18+
state=None,
19+
**kwargs):
1920
"""Test result of expression.
2021
2122
The code of the student is ran in the active state and the result of the evaluation is
@@ -46,6 +47,7 @@ def test_expression_result(extra_env=None,
4647
the expression is evaluated. All primitive types are copied automatically, other objects have to
4748
be passed explicitely.
4849
error_msg (str): Message to override the default error message that is thrown if the expression resulted in an error.
50+
kwargs: named arguments which are the same as those used by ``has_equal_value``.
4951
5052
:Example:
5153
Student code::
@@ -90,4 +92,4 @@ def test_expression_result(extra_env=None,
9092
expr_code=expr_code,
9193
pre_code=pre_code,
9294
keep_objs_in_env=keep_objs_in_env,
93-
state = state)
95+
state = state, **kwargs)

pythonwhat/test_funcs/test_function.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def test_function(name,
2020
incorrect_msg=None,
2121
add_more=False,
2222
highlight=True,
23-
state=None):
23+
state=None,
24+
**kwargs):
2425
"""Test if function calls match.
2526
2627
This function compares a function call in the student's code with the corresponding one in the solution
@@ -42,6 +43,7 @@ def test_function(name,
4243
args_not_specified_msg (str): feedback message if the function is called but not all required arguments are specified
4344
incorrect_msg (str): feedback message if the arguments of the function in the solution doesn't match
4445
the one of the student.
46+
kwargs: named arguments which are the same as those used by ``has_equal_value``.
4547
4648
:Example:
4749
@@ -149,7 +151,8 @@ def test_function(name,
149151
test = build_test(arg_student, arg_solution,
150152
state,
151153
do_eval, eq_fun, msg, add_more=add_more,
152-
highlight=arg_student if highlight else None)
154+
highlight=arg_student if highlight else None,
155+
**kwargs)
153156
test.test()
154157

155158
if not test.result:
@@ -172,7 +175,7 @@ def test_function(name,
172175
test = build_test(key_student, key_solution,
173176
state,
174177
do_eval, eq_fun, msg, add_more=add_more,
175-
highlight=key_student if highlight else None)
178+
highlight=key_student if highlight else None, **kwargs)
176179
test.test()
177180

178181
if not test.result:
@@ -229,7 +232,8 @@ def test_function_v2(name,
229232
incorrect_msg=None,
230233
add_more=False,
231234
highlight=True,
232-
state=None):
235+
state=None,
236+
**kwargs):
233237
"""Test if function calls match (v2).
234238
235239
This function compares a function call in the student's code with the corresponding one in the solution
@@ -255,6 +259,7 @@ def test_function_v2(name,
255259
parameters listed in params are specified by the student.
256260
incorrect_msg (list(str)): string or list of strings (parameter-specific). Custom feedback messages if the arguments
257261
don't correspond between student and solution code.
262+
kwargs: named arguments which are the same as those used by ``has_equal_value``.
258263
"""
259264

260265
rep = Reporter.active_reporter
@@ -346,7 +351,7 @@ def test_function_v2(name,
346351
sub_tests = [partial(test_call, name, call_ind, signature, params, do_eval, solution_args,
347352
eq_fun, add_more, index,
348353
params_not_specified_msg, params_not_matched_msg, incorrect_msg,
349-
keywords, state=state, highlight = highlight)
354+
keywords, state=state, highlight = highlight, **kwargs)
350355
for call_ind in call_indices]
351356
test_or(*sub_tests, state=state)
352357

@@ -360,7 +365,7 @@ def test_call(name, call_ind, signature, params, do_eval, solution_args,
360365
eq_fun, add_more, index,
361366
params_not_specified_msg, params_not_matched_msg, incorrect_msg,
362367
keywords, # pulled from solution process
363-
state, highlight):
368+
state, highlight, **kwargs):
364369
#stud_name = get_mapped_name(name, state.student_mappings)
365370

366371
rep = Reporter.active_reporter
@@ -409,15 +414,16 @@ def test_call(name, call_ind, signature, params, do_eval, solution_args,
409414
test_arg(param, do_eval[ind],
410415
arg_student, arg_solution, param_kind, stud_name,
411416
eq_fun, add_more,
412-
incorrect_msg[ind], state=state, highlight = arg_student if highlight else None)
417+
incorrect_msg[ind], state=state, highlight = arg_student if highlight else None,
418+
**kwargs)
413419

414420
# If all is still good, we have a winner!
415421
state.set_used(name, call_ind, index)
416422

417423
def test_arg(param, do_eval,
418424
arg_student, arg_solution, param_kind, stud_name,
419425
eq_fun, add_more,
420-
incorrect_msg, state=None, highlight = None):
426+
incorrect_msg, state=None, highlight = None, **kwargs):
421427
rep = Reporter.active_reporter
422428

423429
if incorrect_msg is None:
@@ -430,7 +436,8 @@ def test_arg(param, do_eval,
430436

431437
test = build_test(arg_student, arg_solution,
432438
state,
433-
do_eval, eq_fun, msg, add_more = add_more, highlight=highlight)
439+
do_eval, eq_fun, msg, add_more = add_more, highlight=highlight,
440+
**kwargs)
434441
# TODO
435442
rep.do_test(test)
436443

@@ -447,17 +454,17 @@ def bind_args(signature, arguments, keyws):
447454
bound_args = signature.bind(*arguments, **keyws)
448455
return(bound_args.arguments, signature.parameters)
449456

450-
def build_test(stud, sol, state, do_eval, eq_fun, feedback_msg, add_more, highlight = False):
457+
def build_test(stud, sol, state, do_eval, eq_fun, feedback_msg, add_more, highlight = False, **kwargs):
451458
got_error = False
452459
if do_eval:
453460

454-
eval_solution, str_solution = getResultInProcess(tree = sol, process = state.solution_process)
461+
eval_solution, str_solution = getResultInProcess(tree = sol, process = state.solution_process, **kwargs)
455462
if isinstance(str_solution, Exception):
456463
raise ValueError("Running an argument in the solution environment raised an error")
457464
if isinstance(eval_solution, ReprFail):
458465
raise ValueError("Couldn't figure out the argument: " + eval_solution.info)
459466

460-
eval_student, str_student = getResultInProcess(tree = stud, process = state.student_process)
467+
eval_student, str_student = getResultInProcess(tree = stud, process = state.student_process, **kwargs)
461468
if isinstance(str_student, Exception):
462469
got_error = True
463470

pythonwhat/test_funcs/test_object_after_expression.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def test_object_after_expression(name,
99
expr_code=None,
1010
pre_code=None,
1111
keep_objs_in_env=None,
12-
state=None):
12+
state=None,
13+
**kwargs):
1314
"""Test object after expression.
1415
1516
The code of the student is ran in the active state and the the value of the given object is
@@ -41,6 +42,7 @@ def test_object_after_expression(name,
4142
keep_obj_in_env (list()): a list of variable names that should be hold in the copied environment where
4243
the expression is evaluated. All primitive types are copied automatically, other objects have to
4344
be passed explicitely.
45+
kwargs: named arguments which are the same as those used by ``has_equal_value``.
4446
4547
:Example:
4648
@@ -86,4 +88,5 @@ def test_object_after_expression(name,
8688
name = name,
8789
highlight = ass_node,
8890
expr_code = expr_code,
89-
state=state)
91+
state=state,
92+
**kwargs)

tests/test_test_expression_result.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,11 @@ def test_copy_sct_fails(self):
3535
sct_payload = helper.run(self.data)
3636
self.assertFalse(sct_payload['correct'])
3737

38+
def test_test_expression_result_copy_pass(self):
39+
self.data["DC_CODE"] = self.data["DC_SOLUTION"]
40+
self.data["DC_SCT"] = "test_expression_result(expr_code = 'a', error_msg = 'cough', copy = False)"
41+
sct_payload = helper.run(self.data)
42+
self.assertTrue(sct_payload['correct'])
43+
3844
if __name__ == "__main__":
3945
unittest.main()

tests/test_test_function.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ def test_fail_pos_0_inline_if_body(self):
742742
self.data["DC_SOLUTION"] = "np.array([1,2] if False else [1])"
743743
self.run_fail(".check_args(0).check_if_exp(0).check_body().has_equal_ast()")
744744

745+
def test_test_function_kwargs(self):
746+
self.data["DC_SCT"] = "test_function('np.array', copy = False)"
747+
748+
def test_test_function2_kwargs(self):
749+
self.data["DC_SCT"] = "test_function2('np.array', params = ['object'], copy = False)"
750+
751+
745752
class TestCheckFunctionCases(unittest.TestCase):
746753
def setup_color(self):
747754
self.data = {

0 commit comments

Comments
 (0)