Commit 2b6e9359 by Victor Shnayder

Initial refactor of inputtypes into classes.

- for now, wraps existing render functions as separate classes
- a bit of cleanup in how it's called from capa_problem
- initial tests to make sure things are testable.
parent cb291871
...@@ -481,8 +481,8 @@ class LoncapaProblem(object): ...@@ -481,8 +481,8 @@ class LoncapaProblem(object):
problemid = problemtree.get('id') # my ID problemid = problemtree.get('id') # my ID
if problemtree.tag in inputtypes.get_input_xml_tags(): if problemtree.tag in inputtypes.registered_input_tags():
# If this is an inputtype subtree, let it render itself.
status = "unsubmitted" status = "unsubmitted"
msg = '' msg = ''
hint = '' hint = ''
...@@ -499,20 +499,17 @@ class LoncapaProblem(object): ...@@ -499,20 +499,17 @@ class LoncapaProblem(object):
value = self.student_answers[problemid] value = self.student_answers[problemid]
# do the rendering # do the rendering
render_object = inputtypes.SimpleInput(system=self.system,
xml=problemtree, state = {'value': value,
state={'value': value, 'status': status,
'status': status, 'id': problemtree.get('id'),
'id': problemtree.get('id'), 'feedback': {'message': msg,
'feedback': {'message': msg, 'hint': hint,
'hint': hint, 'hintmode': hintmode,}}
'hintmode': hintmode,
} input_type_cls = inputtypes.get_class_for_tag(problemtree.tag)
}, the_input = input_type_cls(self.system, problemtree, state)
use='capa_input') return the_input.get_html()
# function(problemtree, value, status, msg)
# render the special response (textline, schematic,...)
return render_object.get_html()
# let each Response render itself # let each Response render itself
if problemtree in self.responders: if problemtree in self.responders:
......
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
Tests of input types (and actually responsetypes too) Tests of input types (and actually responsetypes too)
""" """
from datetime import datetime from datetime import datetime
import json import json
from mock import Mock
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import os import os
import unittest import unittest
...@@ -14,24 +14,55 @@ from capa import inputtypes ...@@ -14,24 +14,55 @@ from capa import inputtypes
from lxml import etree from lxml import etree
def tst_render_template(template, context):
"""
A test version of render to template. Renders to the repr of the context, completely ignoring the template name.
"""
return repr(context)
system = Mock(render_template=tst_render_template)
class OptionInputTest(unittest.TestCase): class OptionInputTest(unittest.TestCase):
''' '''
Make sure option inputs work Make sure option inputs work
''' '''
def test_rendering(self): def test_rendering_new(self):
xml = """<optioninput options="('Up','Down')" id="sky_input" correct="Up"/>""" xml = """<optioninput options="('Up','Down')" id="sky_input" correct="Up"/>"""
element = etree.fromstring(xml) element = etree.fromstring(xml)
value = 'Down' value = 'Down'
status = 'incorrect' status = 'answered'
rendered_element = inputtypes.optioninput(element, value, status, test_system.render_template) context = inputtypes._optioninput(element, value, status, test_system.render_template)
rendered_str = etree.tostring(rendered_element) print 'context: ', context
print rendered_str
self.assertTrue(False) 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 = """<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')],
'state': 'answered',
'msg': '',
'inline': '',
'id': 'sky_input'}
self.assertEqual(context, expected)
# TODO: split each inputtype into a get_render_context function and a
# template property, and have the rendering done in one place. (and be
# able to test the logic without dealing with xml at least on the output
# end)
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