def test_parse_matcher_instance_is_struct_no_type_error(self): s = dict_to_struct( {"value": "20.1"}) with self.assertRaises(ValidationError) as cm: parse_matcher(None, "some where", s) self.assertIn("some where: matcher must supply 'type'", str(cm.exception))
def __init__(self, vctx, location, name, answers_desc): super(ShortAnswer, self).__init__(vctx, location, name, answers_desc) validate_struct( vctx, location, answers_desc, required_attrs=(("type", str), ("correct_answer", list)), allowed_attrs=( ("weight", (int, float)), ("prepended_text", str), ("appended_text", str), ("hint", str), ("hint_title", str), ("width", (str, int, float)), ("required", bool), ), ) self.weight = getattr(answers_desc, "weight", 0) if len(answers_desc.correct_answer) == 0: raise ValidationError( string_concat("%s: ", _("at least one answer must be provided")) % location) self.hint = getattr(self.answers_desc, "hint", "") self.width = getattr(self.answers_desc, "width", None) parsed_length = self.get_length_attr_em(location, self.width) self.width = 0 if parsed_length is not None: self.width = max(MINIMUN_WIDTH, parsed_length) else: self.width = DEFAULT_WIDTH self.width_str = "width: " + str(self.width) + "em" self.matchers = [ parse_matcher( vctx, string_concat( "%s, ", # Translators: refers to optional # correct answer for checking # correctness sumbitted by students. _("answer"), " %d") % (location, i + 1), answer) for i, answer in enumerate(answers_desc.correct_answer) ] if not any(matcher.correct_answer_text() is not None for matcher in self.matchers): raise ValidationError( string_concat( "%s: ", _("no matcher is able to provide a plain-text " "correct answer")) % location)
def test_parse_matcher_instance_is_struct(self): s = dict_to_struct({ "type": "float", "value": "20.1", }) result = parse_matcher(None, "", s) self.assertTrue(isinstance(result, FloatMatcher)) self.assertEqual(result.correct_answer_text(), "20.1")
def test_parse_matcher_instance_is_struct(self): s = dict_to_struct( {"type": "float", "value": "20.1", }) result = parse_matcher(None, "", s) self.assertTrue(isinstance(result, FloatMatcher)) self.assertEqual(result.correct_answer_text(), "20.1")
def test_parse_matcher_instance_not_supported(self): s = {"type": "float", "value": "20.1"} with self.assertRaises(ValidationError) as cm: parse_matcher(None, "some where", s) self.assertIn("some where: must be struct or string", str(cm.exception))
def test_parse_matcher_instance_is_string(self): s = "<plain>half" result = parse_matcher(None, "", s) self.assertTrue(isinstance(result, PlainMatcher)) self.assertEqual(result.correct_answer_text(), "half")
def __init__(self, vctx, location, name, answers_desc): super(ShortAnswer, self).__init__( vctx, location, name, answers_desc) validate_struct( vctx, location, answers_desc, required_attrs=( ("type", str), ("correct_answer", list) ), allowed_attrs=( ("weight", (int, float)), ("prepended_text", str), ("appended_text", str), ("hint", str), ("hint_title", str), ("width", (str, int, float)), ("required", bool), ), ) weight = getattr(answers_desc, "weight", 0) if weight < 0: raise ValidationError( string_concat( "%s: %s: ", _("'weight' must be a non-negative value, " "got '%s' instead") % str(weight)) % (location, self.name)) self.weight = weight if len(answers_desc.correct_answer) == 0: raise ValidationError( string_concat( "%s: %s: ", _("at least one answer must be provided")) % (location, self.name)) self.hint = getattr(self.answers_desc, "hint", "") width = getattr(self.answers_desc, "width", None) parsed_length = self.get_length_attr_em( "%s: %s: 'width'" % (location, self.name), width) self.width = 0 if parsed_length is not None: self.width = max(MINIMUN_WIDTH, parsed_length) else: self.width = DEFAULT_WIDTH self.width_str = "width: " + str(self.width) + "em" self.matchers = [ parse_matcher( vctx, string_concat("%s, ", # Translators: refers to optional # correct answer for checking # correctness sumbitted by students. _("answer"), " %d") % (location, i+1), answer) for i, answer in enumerate(answers_desc.correct_answer)] if not any(matcher.correct_answer_text() is not None for matcher in self.matchers): raise ValidationError( string_concat( "%s: %s: ", _("no matcher is able to provide a plain-text " "correct answer")) % (location, self.name))