Commit 6d9fe76d by Victor Shnayder

wip. adding tests, basic refactor

parent dd6d7e99
......@@ -195,48 +195,37 @@ class OptionInput(InputTypeBase):
template = "optioninput.html"
tags = ['optioninput']
def _get_render_context(self):
return _optioninput(self.xml, self.value, self.status, self.system.render_template, self.msg)
def __init__(self, system, xml, state):
super(OptionInput, self).__init__(system, xml, state)
def optioninput(element, value, status, render_template, msg=''):
context = _optioninput(element, value, status, render_template, msg)
html = render_template("optioninput.html", context)
return etree.XML(html)
# Extract the options...
options = self.xml.get('options')
if not options:
raise Exception(
"[courseware.capa.inputtypes.optioninput] Missing options specification in "
+ etree.tostring(self.xml))
def _optioninput(element, value, status, render_template, msg=''):
"""
Select option input type.
# parse the set of possible options
oset = shlex.shlex(options[1:-1])
oset.quotes = "'"
oset.whitespace = ","
oset = [x[1:-1] for x in list(oset)]
Example:
# make ordered list with (key, value) same
self.osetdict = [(oset[x], oset[x]) for x in range(len(oset))]
# TODO: allow ordering to be randomized
<optioninput options="('Up','Down')" correct="Up"/><text>The location of the sky</text>
"""
eid = element.get('id')
options = element.get('options')
if not options:
raise Exception(
"[courseware.capa.inputtypes.optioninput] Missing options specification in "
+ etree.tostring(element))
# parse the set of possible options
oset = shlex.shlex(options[1:-1])
oset.quotes = "'"
oset.whitespace = ","
oset = [x[1:-1] for x in list(oset)]
# make ordered list with (key, value) same
osetdict = [(oset[x], oset[x]) for x in range(len(oset))]
# TODO: allow ordering to be randomized
def _get_render_context(self):
context = {'id': eid,
'value': value,
'state': status,
'msg': msg,
'options': osetdict,
'inline': element.get('inline',''),
}
return context
context = {
'id': self.id,
'value': self.value,
'state': self.status,
'msg': self.msg,
'options': self.osetdict,
'inline': self.xml.get('inline',''),
}
return context
register_input_class(OptionInput)
......@@ -245,7 +234,6 @@ register_input_class(OptionInput)
# TODO: consolidate choicegroup, radiogroup, checkboxgroup after discussion of
# desired semantics.
# @register_render_function
def choicegroup(element, value, status, render_template, msg=''):
'''
Radio button inputs: multiple choice or true/false
......
......@@ -27,14 +27,17 @@ class OptionInputTest(unittest.TestCase):
'''
Make sure option inputs work
'''
def test_rendering_new(self):
xml = """<optioninput options="('Up','Down')" id="sky_input" correct="Up"/>"""
element = etree.fromstring(xml)
value = 'Down'
status = 'answered'
context = inputtypes._optioninput(element, value, status, test_system.render_template)
print 'context: ', context
def test_rendering(self):
xml_str = """<optioninput options="('Up','Down')" id="sky_input" correct="Up"/>"""
element = etree.fromstring(xml_str)
state = {'value': 'Down',
'id': 'sky_input',
'status': 'answered'}
option_input = inputtypes.OptionInput(system, element, state)
context = option_input._get_render_context()
expected = {'value': 'Down',
'options': [('Up', 'Up'), ('Down', 'Down')],
......@@ -45,9 +48,30 @@ class OptionInputTest(unittest.TestCase):
self.assertEqual(context, expected)
def test_rendering(self):
xml_str = """<optioninput options="('Up','Down')" id="sky_input" correct="Up"/>"""
class ChoiceGroupTest(unittest.TestCase):
'''
Test choice groups.
'''
def test_mult_choice(self):
xml_str = """
<choicegroup>
<choice correct="false" name="foil1">
<startouttext />This is foil One.<endouttext />
</choice>
<choice correct="false" name="foil2">
<startouttext />This is foil Two.<endouttext />
</choice>
<choice correct="true" name="foil3">
<startouttext />This is foil Three.<endouttext />
</choice>
<choice correct="false" name="foil4">
<startouttext />This is foil Four.<endouttext />
</choice>
<choice correct="false" name="foil5">
<startouttext />This is foil Five.<endouttext />
</choice>
</choicegroup>
"""
element = etree.fromstring(xml_str)
state = {'value': 'Down',
......
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