Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
a15388b2
Commit
a15388b2
authored
Dec 13, 2015
by
Martin Segado
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow python variable interpolation in CustomResponse 'expect' and 'answer' attributes
parent
e65e8df2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
1 deletions
+35
-1
common/lib/capa/capa/responsetypes.py
+1
-1
common/lib/capa/capa/tests/response_xml_factory.py
+7
-0
common/lib/capa/capa/tests/test_responsetypes.py
+27
-0
No files found.
common/lib/capa/capa/responsetypes.py
View file @
a15388b2
...
...
@@ -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
)
...
...
common/lib/capa/capa/tests/response_xml_factory.py
View file @
a15388b2
...
...
@@ -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
)
...
...
common/lib/capa/capa/tests/test_responsetypes.py
View file @
a15388b2
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment