Skip to content

Commit

Permalink
Extracted normalize str and created tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur-debert committed Dec 4, 2024
1 parent cf2808f commit d6ed0bb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
33 changes: 23 additions & 10 deletions rangy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)}")

Expand Down Expand Up @@ -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
raise ParseRangeError(f"Error parsing range: {e}") from e
Empty file added tests/parser/test_from_rangy.py
Empty file.
35 changes: 35 additions & 0 deletions tests/parser/test_normalize_str.py
Original file line number Diff line number Diff line change
@@ -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",)

9 changes: 0 additions & 9 deletions tests/parser/test_parser_by_strings.py

This file was deleted.

0 comments on commit d6ed0bb

Please sign in to comment.