Commit c305fc38 by Adam Palay

fix regression for stringresponse problems with unspecified case sensitivity (BLD-710)

write failing tests
parent ac3cd92b
...@@ -1054,8 +1054,11 @@ class StringResponse(LoncapaResponse): ...@@ -1054,8 +1054,11 @@ class StringResponse(LoncapaResponse):
def setup_response(self): def setup_response(self):
self.backward = '_or_' in self.xml.get('answer').lower() self.backward = '_or_' in self.xml.get('answer').lower()
self.regexp = 'regexp' in self.xml.get('type').lower().split(' ') self.regexp = False
self.case_insensitive = 'ci' in self.xml.get('type').lower().split(' ') self.case_insensitive = False
if self.xml.get('type') is not None:
self.regexp = 'regexp' in self.xml.get('type').lower().split(' ')
self.case_insensitive = 'ci' in self.xml.get('type').lower().split(' ')
# backward compatibility, can be removed in future, it is up to @Lyla Fisher. # backward compatibility, can be removed in future, it is up to @Lyla Fisher.
if self.backward: if self.backward:
......
...@@ -700,7 +700,7 @@ class StringResponseXMLFactory(ResponseXMLFactory): ...@@ -700,7 +700,7 @@ class StringResponseXMLFactory(ResponseXMLFactory):
""" """
# Retrieve the **kwargs # Retrieve the **kwargs
answer = kwargs.get("answer", None) answer = kwargs.get("answer", None)
case_sensitive = kwargs.get("case_sensitive", True) case_sensitive = kwargs.get("case_sensitive", None)
hint_list = kwargs.get('hints', None) hint_list = kwargs.get('hints', None)
hint_fn = kwargs.get('hintfn', None) hint_fn = kwargs.get('hintfn', None)
regexp = kwargs.get('regexp', None) regexp = kwargs.get('regexp', None)
...@@ -714,9 +714,12 @@ class StringResponseXMLFactory(ResponseXMLFactory): ...@@ -714,9 +714,12 @@ class StringResponseXMLFactory(ResponseXMLFactory):
response_element.set("answer", unicode(answer)) response_element.set("answer", unicode(answer))
# Set the case sensitivity and regexp: # Set the case sensitivity and regexp:
type_value = "cs" if case_sensitive else "ci" type_value = ''
if case_sensitive is not None:
type_value += "cs" if case_sensitive else "ci"
type_value += ' regexp' if regexp else '' type_value += ' regexp' if regexp else ''
response_element.set("type", type_value) if type_value:
response_element.set("type", type_value.strip())
# Add the hints if specified # Add the hints if specified
if hint_list or hint_fn: if hint_list or hint_fn:
......
...@@ -564,6 +564,10 @@ class StringResponseTest(ResponseTest): ...@@ -564,6 +564,10 @@ class StringResponseTest(ResponseTest):
problem = self.build_problem(answer=".*tre+", case_sensitive=False, regexp=True) problem = self.build_problem(answer=".*tre+", case_sensitive=False, regexp=True)
self.assert_grade(problem, "There is a tree", "correct") self.assert_grade(problem, "There is a tree", "correct")
# test with case_sensitive not specified
problem = self.build_problem(answer=".*tre+", regexp=True)
self.assert_grade(problem, "There is a tree", "correct")
answers = [ answers = [
"Martin Luther King Junior", "Martin Luther King Junior",
"Doctor Martin Luther King Junior", "Doctor Martin Luther King Junior",
...@@ -611,6 +615,7 @@ class StringResponseTest(ResponseTest): ...@@ -611,6 +615,7 @@ class StringResponseTest(ResponseTest):
self.assert_grade(problem, u"î", "incorrect") self.assert_grade(problem, u"î", "incorrect")
self.assert_grade(problem, u"o", "incorrect") self.assert_grade(problem, u"o", "incorrect")
def test_backslash_and_unicode_regexps(self): def test_backslash_and_unicode_regexps(self):
""" """
Test some special cases of [unicode] regexps. Test some special cases of [unicode] regexps.
...@@ -643,26 +648,39 @@ class StringResponseTest(ResponseTest): ...@@ -643,26 +648,39 @@ class StringResponseTest(ResponseTest):
def test_case_sensitive(self): def test_case_sensitive(self):
# Test single answer # Test single answer
problem = self.build_problem(answer="Second", case_sensitive=True) problem_specified = self.build_problem(answer="Second", case_sensitive=True)
# should also be case_sensitive if case sensitivity is not specified
problem_not_specified = self.build_problem(answer="Second")
problems = [problem_specified, problem_not_specified]
# Exact string should be correct for problem in problems:
self.assert_grade(problem, "Second", "correct") # Exact string should be correct
self.assert_grade(problem, "Second", "correct")
# Other strings and the lowercase version of the string are incorrect # Other strings and the lowercase version of the string are incorrect
self.assert_grade(problem, "Other String", "incorrect") self.assert_grade(problem, "Other String", "incorrect")
self.assert_grade(problem, "second", "incorrect") self.assert_grade(problem, "second", "incorrect")
# Test multiple answers # Test multiple answers
answers = ["Second", "Third", "Fourth"] answers = ["Second", "Third", "Fourth"]
problem = self.build_problem(answer="sample_answer", case_sensitive=True, additional_answers=answers)
for answer in answers: # set up problems
# Exact string should be correct problem_specified = self.build_problem(
self.assert_grade(problem, answer, "correct") answer="sample_answer", case_sensitive=True, additional_answers=answers
)
# Other strings and the lowercase version of the string are incorrect problem_not_specified = self.build_problem(
self.assert_grade(problem, "Other String", "incorrect") answer="sample_answer", additional_answers=answers
self.assert_grade(problem, "second", "incorrect") )
problems = [problem_specified, problem_not_specified]
for problem in problems:
for answer in answers:
# Exact string should be correct
self.assert_grade(problem, answer, "correct")
# Other strings and the lowercase version of the string are incorrect
self.assert_grade(problem, "Other String", "incorrect")
self.assert_grade(problem, "second", "incorrect")
def test_bogus_escape_not_raised(self): def test_bogus_escape_not_raised(self):
""" """
......
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