Commit d0a9b231 by Victor Shnayder

Turn javascriptinput into a class, add tests

parent 009d6c2e
...@@ -318,7 +318,7 @@ def extract_choices(element): ...@@ -318,7 +318,7 @@ def extract_choices(element):
choices.append((choice.get("name"), choice_text)) choices.append((choice.get("name"), choice_text))
return choices return choices
register_input_class(ChoiceGroup) register_input_class(ChoiceGroup)
...@@ -326,37 +326,44 @@ register_input_class(ChoiceGroup) ...@@ -326,37 +326,44 @@ register_input_class(ChoiceGroup)
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
def javascriptinput(element, value, status, render_template, msg='null'): class JavascriptInput(InputTypeBase):
''' """
Hidden field for javascript to communicate via; also loads the required Hidden field for javascript to communicate via; also loads the required
scripts for rendering the problem and passes data to the problem. scripts for rendering the problem and passes data to the problem.
''' """
eid = element.get('id')
params = element.get('params')
problem_state = element.get('problem_state')
display_class = element.get('display_class')
display_file = element.get('display_file')
# Need to provide a value that JSON can parse if there is no template = "javascriptinput.html"
# student-supplied value yet. tags = ['javascriptinput']
if value == "":
value = 'null'
escapedict = {'"': '"'} def __init__(self, system, xml, state):
value = saxutils.escape(value, escapedict) super(JavascriptInput, self).__init__(system, xml, state)
msg = saxutils.escape(msg, escapedict) # Need to provide a value that JSON can parse if there is no
context = {'id': eid, # student-supplied value yet.
'params': params, if self.value == "":
'display_file': display_file, self.value = 'null'
'display_class': display_class,
'problem_state': problem_state, self.params = self.xml.get('params')
self.problem_state = self.xml.get('problem_state')
self.display_class = self.xml.get('display_class')
self.display_file = self.xml.get('display_file')
def _get_render_context(self):
escapedict = {'"': '"'}
value = saxutils.escape(self.value, escapedict)
msg = saxutils.escape(self.msg, escapedict)
context = {'id': self.id,
'params': self.params,
'display_file': self.display_file,
'display_class': self.display_class,
'problem_state': self.problem_state,
'value': value, 'value': value,
'evaluation': msg, 'evaluation': msg,
} }
html = render_template("javascriptinput.html", context) return context
return etree.XML(html)
_reg(javascriptinput) register_input_class(JavascriptInput)
def textline(element, value, status, render_template, msg=""): def textline(element, value, status, render_template, msg=""):
......
...@@ -8,6 +8,7 @@ from mock import Mock ...@@ -8,6 +8,7 @@ from mock import Mock
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import os import os
import unittest import unittest
import xml.sax.saxutils as saxutils
from . import test_system from . import test_system
from capa import inputtypes from capa import inputtypes
...@@ -128,6 +129,43 @@ class ChoiceGroupTest(unittest.TestCase): ...@@ -128,6 +129,43 @@ class ChoiceGroupTest(unittest.TestCase):
def test_radiogroup(self): def test_radiogroup(self):
self.check_group('radiogroup', 'radio', '[]') self.check_group('radiogroup', 'radio', '[]')
def test_checkboxgroup(self): def test_checkboxgroup(self):
self.check_group('checkboxgroup', 'checkbox', '[]') self.check_group('checkboxgroup', 'checkbox', '[]')
class JavascriptInputTest(unittest.TestCase):
'''
The javascript input is a pretty straightforward pass-thru, but test it anyway
'''
def test_rendering(self):
params = "(1,2,3)"
problem_state = "abc12',12&hi<there>"
display_class = "a_class"
display_file = "my_files/hi.js"
xml_str = """<javascriptinput id="prob_1_2" params="{params}" problem_state="{ps}"
display_class="{dc}" display_file="{df}"/>""".format(
params=params,
ps=saxutils.quoteattr(problem_state)[1:-1], # don't want the outer quotes
dc=display_class, df=display_file)
element = etree.fromstring(xml_str)
state = {'value': '3',}
the_input = inputtypes.get_class_for_tag('javascriptinput')(system, element, state)
context = the_input._get_render_context()
expected = {'id': 'prob_1_2',
'params': params,
'display_file': display_file,
'display_class': display_class,
'problem_state': problem_state,
'value': '3',
'evaluation': '',}
self.assertEqual(context, expected)
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