Commit a15388b2 by Martin Segado

Allow python variable interpolation in CustomResponse 'expect' and 'answer' attributes

parent e65e8df2
......@@ -2208,7 +2208,7 @@ class CustomResponse(LoncapaResponse):
# if <customresponse> has an "expect" (or "answer") attribute then save
# that
self.expect = xml.get('expect') or xml.get('answer')
self.expect = contextualize_text(xml.get('expect') or xml.get('answer'), self.context)
log.debug('answer_ids=%s', self.answer_ids)
......
......@@ -264,11 +264,15 @@ class CustomResponseXMLFactory(ResponseXMLFactory):
*expect*: The value passed to the function cfn
*answer*: Inline script that calculates the answer
*answer_attr*: The "answer" attribute on the tag itself (treated as an
alias to "expect", though "expect" takes priority if both are given)
"""
# Retrieve **kwargs
cfn = kwargs.get('cfn', None)
expect = kwargs.get('expect', None)
answer_attr = kwargs.get('answer_attr', None)
answer = kwargs.get('answer', None)
options = kwargs.get('options', None)
cfn_extra_args = kwargs.get('cfn_extra_args', None)
......@@ -282,6 +286,9 @@ class CustomResponseXMLFactory(ResponseXMLFactory):
if expect:
response_element.set('expect', str(expect))
if answer_attr:
response_element.set('answer', str(answer_attr))
if answer:
answer_element = etree.SubElement(response_element, "answer")
answer_element.text = str(answer)
......
......@@ -1799,6 +1799,33 @@ class CustomResponseTest(ResponseTest): # pylint: disable=missing-docstring
self.assertEqual(correct_map.get_npoints('1_2_1'), 0.5)
self.assertEqual(correct_map.get_correctness('1_2_1'), 'partially-correct')
def test_script_context(self):
# Ensure that python script variables can be used in the "expect" and "answer" fields,
script = script = textwrap.dedent("""
expected_ans = 42
def check_func(expect, answer_given):
return answer_given == expect
""")
problems = (
self.build_problem(script=script, cfn="check_func", expect="$expected_ans"),
self.build_problem(script=script, cfn="check_func", answer_attr="$expected_ans")
)
input_dict = {'1_2_1': '42'}
for problem in problems:
correctmap = problem.grade_answers(input_dict)
# CustomResponse also adds 'expect' to the problem context; check that directly first:
self.assertEqual(problem.context['expect'], '42')
# Also make sure the problem was graded correctly:
correctness = correctmap.get_correctness('1_2_1')
self.assertEqual(correctness, 'correct')
def test_function_code_multiple_input_no_msg(self):
# Check functions also have the option of returning
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment