diff --git a/rangy/parse.py b/rangy/parse.py index 627c0ad..25da5fd 100644 --- a/rangy/parse.py +++ b/rangy/parse.py @@ -14,6 +14,27 @@ def _split(as_squence): else: raise ParseRangeError("Invalid range tuple/list length") +def _nomalize_str(range_str): + """ + Normalizes a range string by removing brackets and splitting it into parts. + + "1-5" -> ["1", "5"] + "(1-5)" -> ["1", "5"] + "1-5" -> ["1", "5"] + "(1,3)" + + Args: + range_str: The range string to normalize. + Returns: + A tuples of parts of the range string. + """ + range_str = range_str.strip() + if range_str.startswith("(") and range_str.endswith(")"): + range_str = range_str[1:-1] + elif range_str.startswith("[") and range_str.endswith("]"): + range_str = range_str[1:-1] + + return tuple(re.split(r'[\s,;|-]+', range_str)) def _normalize_to_sequence(range_input): """Normalizes various range inputs into a consistent tuple representation. @@ -36,15 +57,7 @@ def _normalize_to_sequence(range_input): return range_input, range_input # elif isinstance(range_input, str): - # treat the string. - range_str = range_input.strip() - if range_str.startswith("(") and range_str.endswith(")"): - range_str = range_str[1:-1] - elif range_str.startswith("[") and range_str.endswith("]"): - range_str = range_str[1:-1] - - parts = re.split(r'[\s,;|-]+', range_str) # Use regex to split by any whitespace, comma, semicolon, or hyphen. - return _split(parts) + return _split(_nomalize_str(range_input)) else: raise ParseRangeError(f"Unsupported range input type: {type(range_input)}") @@ -90,4 +103,4 @@ def parse_range(range_input): return parsed_start, parsed_end except (KeyError, ValueError, TypeError) as e: # Handle converter and TypeRegistry errors. - raise ParseRangeError(f"Error parsing range: {e}") from e \ No newline at end of file + raise ParseRangeError(f"Error parsing range: {e}") from e diff --git a/tests/parser/test_from_rangy.py b/tests/parser/test_from_rangy.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/parser/test_normalize_str.py b/tests/parser/test_normalize_str.py new file mode 100644 index 0000000..6c0511c --- /dev/null +++ b/tests/parser/test_normalize_str.py @@ -0,0 +1,35 @@ +import pytest + +from rangy.exceptions import ParseRangeError +from rangy import parse_range +from rangy.parse import _nomalize_str + + +def test_str_single_integer(): + assert parse_range("5") == (5, 5) + +def test_normalize_str_basic(): + assert _nomalize_str("1-5") == ("1", "5") + assert _nomalize_str("(1-5)") == ("1", "5") + assert _nomalize_str("[1-5]") == ("1", "5") + +def test_normalize_str_with_spaces(): + assert _nomalize_str(" 1 - 5 ") == ("1", "5") + assert _nomalize_str("[ 1 - 5 ]") == ("1", "5") + assert _nomalize_str("( 1 - 5 )") == ("1", "5") + +def test_normalize_str_with_commas(): + assert _nomalize_str("1,5") == ("1", "5") + assert _nomalize_str("(1,5)") == ("1", "5") + assert _nomalize_str("[1,5]") == ("1", "5") + +def test_normalize_str_with_semicolons(): + assert _nomalize_str("1;5") == ("1", "5") + assert _nomalize_str("(1;5)") == ("1", "5") + assert _nomalize_str("[1;5]") == ("1", "5") + +def test_normalize_str_single_value(): + assert _nomalize_str("5") == ("5",) + assert _nomalize_str("(5)") == ("5",) + assert _nomalize_str("[5]") == ("5",) + diff --git a/tests/parser/test_parser_by_strings.py b/tests/parser/test_parser_by_strings.py deleted file mode 100644 index 2e1e1cc..0000000 --- a/tests/parser/test_parser_by_strings.py +++ /dev/null @@ -1,9 +0,0 @@ -import pytest - -from rangy.exceptions import ParseRangeError -from rangy import parse_range - - -def test_str_single_integer(): - assert parse_range("5") == (5, 5) -