diff --git a/jambo/parser/string_type_parser.py b/jambo/parser/string_type_parser.py index 409aea2..dc44ca4 100644 --- a/jambo/parser/string_type_parser.py +++ b/jambo/parser/string_type_parser.py @@ -24,7 +24,7 @@ class StringTypeParser(GenericTypeParser): if not isinstance(default_value, str): raise ValueError( f"Default value for {name} must be a string, " - f"but got {type(properties['default'])}." + f"but got <{type(properties['default']).__name__}>." ) if len(default_value) > properties.get("maxLength", float("inf")): diff --git a/tests/parser/test_array_type_parser.py b/tests/parser/test_array_type_parser.py index f8d69ca..9c06a46 100644 --- a/tests/parser/test_array_type_parser.py +++ b/tests/parser/test_array_type_parser.py @@ -69,10 +69,10 @@ class TestArrayTypeParser(TestCase): with self.assertRaises(ValueError) as context: parser.from_properties("placeholder", properties) - self.assertEqual( - str(context.exception), - "All items in the default list must be of type str", - ) + self.assertEqual( + str(context.exception), + "All items in the default list must be of type str", + ) def test_array_parser_with_invalid_default_type(self): parser = ArrayTypeParser() @@ -82,9 +82,9 @@ class TestArrayTypeParser(TestCase): with self.assertRaises(ValueError) as context: parser.from_properties("placeholder", properties) - self.assertEqual( - str(context.exception), "Default value must be a list, got str" - ) + self.assertEqual( + str(context.exception), "Default value must be a list, got str" + ) def test_array_parser_with_invalid_default_min(self): parser = ArrayTypeParser() @@ -94,9 +94,9 @@ class TestArrayTypeParser(TestCase): with self.assertRaises(ValueError) as context: parser.from_properties("placeholder", properties) - self.assertEqual( - str(context.exception), "Default list is below minItems limit of 2" - ) + self.assertEqual( + str(context.exception), "Default list is below minItems limit of 2" + ) def test_array_parser_with_invalid_default_max(self): parser = ArrayTypeParser() @@ -110,6 +110,6 @@ class TestArrayTypeParser(TestCase): with self.assertRaises(ValueError) as context: parser.from_properties("placeholder", properties) - self.assertEqual( - str(context.exception), "Default list exceeds maxItems limit of 3" - ) + self.assertEqual( + str(context.exception), "Default list exceeds maxItems limit of 3" + ) diff --git a/tests/parser/test_string_type_parser.py b/tests/parser/test_string_type_parser.py new file mode 100644 index 0000000..f5d19fe --- /dev/null +++ b/tests/parser/test_string_type_parser.py @@ -0,0 +1,102 @@ +from unittest import TestCase + +from jambo.parser import StringTypeParser + + +class TestStringTypeParser(TestCase): + def test_string_parser_no_options(self): + parser = StringTypeParser() + + properties = {"type": "string"} + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, str) + + def test_string_parser_with_options(self): + parser = StringTypeParser() + + properties = { + "type": "string", + "maxLength": 10, + "minLength": 1, + "pattern": "^[a-zA-Z]+$", + } + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, str) + self.assertEqual(type_validator["max_length"], 10) + self.assertEqual(type_validator["min_length"], 1) + self.assertEqual(type_validator["pattern"], "^[a-zA-Z]+$") + + def test_string_parser_with_default_value(self): + parser = StringTypeParser() + + properties = { + "type": "string", + "default": "default_value", + "maxLength": 20, + "minLength": 5, + } + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, str) + self.assertEqual(type_validator["default"], "default_value") + self.assertEqual(type_validator["max_length"], 20) + self.assertEqual(type_validator["min_length"], 5) + + def test_string_parser_with_invalid_default_value_type(self): + parser = StringTypeParser() + + properties = { + "type": "string", + "default": 12345, # Invalid default value + "maxLength": 20, + "minLength": 5, + } + + with self.assertRaises(ValueError) as context: + parser.from_properties("placeholder", properties) + + self.assertEqual( + str(context.exception), + "Default value for placeholder must be a string, but got .", + ) + + def test_string_parser_with_default_invalid_maxlength(self): + parser = StringTypeParser() + + properties = { + "type": "string", + "default": "default_value", + "maxLength": 2, + "minLength": 1, + } + + with self.assertRaises(ValueError) as context: + parser.from_properties("placeholder", properties) + + self.assertEqual( + str(context.exception), + "Default value for placeholder exceeds maxLength limit of 2", + ) + + def test_string_parser_with_default_invalid_minlength(self): + parser = StringTypeParser() + + properties = { + "type": "string", + "default": "a", + "maxLength": 20, + "minLength": 2, + } + + with self.assertRaises(ValueError) as context: + parser.from_properties("placeholder", properties) + + self.assertEqual( + str(context.exception), + "Default value for placeholder is below minLength limit of 2", + )