From 6d9fe76dbb637cfca407bde24abe78329459b720 Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Tue, 23 Oct 2012 17:35:19 -0400 Subject: [PATCH] wip. adding tests, basic refactor --- common/lib/capa/capa/inputtypes.py | 70 ++++++++----------- common/lib/capa/capa/tests/test_inputtypes.py | 60 +++++++++++----- 2 files changed, 71 insertions(+), 59 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 40fe24434e..55115e66d8 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -195,48 +195,37 @@ class OptionInput(InputTypeBase): template = "optioninput.html" tags = ['optioninput'] + def __init__(self, system, xml, state): + super(OptionInput, self).__init__(system, xml, state) + + # 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)) + + # 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 + self.osetdict = [(oset[x], oset[x]) for x in range(len(oset))] + # TODO: allow ordering to be randomized + def _get_render_context(self): - return _optioninput(self.xml, self.value, self.status, self.system.render_template, self.msg) - -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) - -def _optioninput(element, value, status, render_template, msg=''): - """ - Select option input type. - - Example: - - The location of the sky - """ - 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 - - 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 diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 9ef642d468..79cd9b6c98 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -27,24 +27,6 @@ class OptionInputTest(unittest.TestCase): ''' Make sure option inputs work ''' - def test_rendering_new(self): - xml = """""" - element = etree.fromstring(xml) - - value = 'Down' - status = 'answered' - context = inputtypes._optioninput(element, value, status, test_system.render_template) - print 'context: ', context - - expected = {'value': 'Down', - 'options': [('Up', 'Up'), ('Down', 'Down')], - 'state': 'answered', - 'msg': '', - 'inline': '', - 'id': 'sky_input'} - - self.assertEqual(context, expected) - def test_rendering(self): xml_str = """""" @@ -66,3 +48,45 @@ class OptionInputTest(unittest.TestCase): self.assertEqual(context, expected) +class ChoiceGroupTest(unittest.TestCase): + ''' + Test choice groups. + ''' + def test_mult_choice(self): + xml_str = """ + + + This is foil One. + + + This is foil Two. + + + This is foil Three. + + + This is foil Four. + + + This is foil Five. + + + """ + 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')], + 'state': 'answered', + 'msg': '', + 'inline': '', + 'id': 'sky_input'} + + self.assertEqual(context, expected) +