diff --git a/common/lib/capa/capa/tests/response_xml_factory.py b/common/lib/capa/capa/tests/response_xml_factory.py index 5920840ed6..cdf5a5e47f 100644 --- a/common/lib/capa/capa/tests/response_xml_factory.py +++ b/common/lib/capa/capa/tests/response_xml_factory.py @@ -382,12 +382,19 @@ class StringResponseXMLFactory(ResponseXMLFactory): Uses **kwargs: *answer*: The correct answer (a string) [REQUIRED] + *case_sensitive*: Whether the response is case-sensitive (True/False) [DEFAULT: True] + + *hints*: List of (hint_prompt, hint_name, hint_text) tuples + Where *hint_prompt* is the string for which we show the hint, + *hint_name* is an internal identifier for the hint, + and *hint_text* is the text we show for the hint. """ # Retrieve the **kwargs answer = kwargs.get("answer", None) case_sensitive = kwargs.get("case_sensitive", True) + hint_list = kwargs.get('hints', None) assert(answer) # Create the element @@ -399,6 +406,20 @@ class StringResponseXMLFactory(ResponseXMLFactory): # Set the case sensitivity response_element.set("type", "cs" if case_sensitive else "ci") + # Add the hints if specified + if hint_list: + hintgroup_element = etree.SubElement(response_element, "hintgroup") + for (hint_prompt, hint_name, hint_text) in hint_list: + stringhint_element = etree.SubElement(hintgroup_element, "stringhint") + stringhint_element.set("answer", str(hint_prompt)) + stringhint_element.set("name", str(hint_name)) + + hintpart_element = etree.SubElement(hintgroup_element, "hintpart") + hintpart_element.set("on", str(hint_name)) + + hint_text_element = etree.SubElement(hintpart_element, "text") + hint_text_element.text = str(hint_text) + return response_element def create_input_element(self, **kwargs): diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index 0c1fe24180..9f6bf7fe4b 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -309,6 +309,35 @@ class StringResponseWithHintTest(ResponseTest): # Other strings are not allowed self.assert_grade(problem, "Other String", "incorrect") + def test_hints(self): + hints = [("wisconsin", "wisc", "The state capital of Wisconsin is Madison"), + ("minnesota", "minn", "The state capital of Minnesota is St. Paul")] + + problem = self.build_problem(answer="Michigan", + case_sensitive=False, + hints=hints) + + # We should get a hint for Wisconsin + input_dict = {'1_2_1': 'Wisconsin'} + correct_map = problem.grade_answers(input_dict) + self.assertEquals(correct_map.get_hint('1_2_1'), + "The state capital of Wisconsin is Madison") + + # We should get a hint for Minnesota + input_dict = {'1_2_1': 'Minnesota'} + correct_map = problem.grade_answers(input_dict) + self.assertEquals(correct_map.get_hint('1_2_1'), + "The state capital of Minnesota is St. Paul") + + # We should NOT get a hint for Michigan (the correct answer) + input_dict = {'1_2_1': 'Michigan'} + correct_map = problem.grade_answers(input_dict) + self.assertEquals(correct_map.get_hint('1_2_1'), "") + + # We should NOT get a hint for any other string + input_dict = {'1_2_1': 'California'} + correct_map = problem.grade_answers(input_dict) + self.assertEquals(correct_map.get_hint('1_2_1'), "") class CodeResponseTest(unittest.TestCase): '''