Skip to content

Commit

Permalink
Add some name parser tests & extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Mar 16, 2024
1 parent 3ee6f8c commit 57e8737
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ def parse_name(name: str) -> str:
else:
parsed_name += char
elif char.isdigit():
num_digits = 0
for c in name: # greedily retrieve as many digits as possible
if c.isdigit():
num_digits += 1
else:
break
num_digits = _greedily_count_digits_at_start_of_str(name)
word = Helpers.number_to_word(int(name[:num_digits]))
if parsed_name != "":
word = Helpers.capitalize(word)
Expand All @@ -75,6 +70,17 @@ def parse_name(name: str) -> str:
return parsed_name


def _greedily_count_digits_at_start_of_str(word: str) -> int:
"""Counts the number of digits at the start of the string."""
num_digits = 0
for c in word:
if c.isdigit():
num_digits += 1
else:
break
return num_digits


def parse_unit(unit: str) -> str:
"""Parses the unit."""
if not isinstance(unit, str):
Expand Down
39 changes: 39 additions & 0 deletions tests/parsers_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from api import parsers


class TestNameParser:

@pytest.mark.parametrize(
"name, expected",
[
("a12", "aTwelve"),
("a01", "aOne"),
("042", "fortyTwo"),
("a911bc13", "aNineHundredElevenBcThirteen"),
("13_600", "thirteenSixHundred"),
("a_5_6b7_$5", "aFiveSixBSevenFive"),
],
)
def test_number_substitution(self, name: str, expected: str):
assert parsers.parse_name(name) == expected

@pytest.mark.parametrize(
"name, expected",
[
("ä", "ae"),
("Ä", "Ae"),
("ü", "ue"),
("Ü", "Ue"),
("ö", "oe"),
("Ö", "Oe"),
("ß", "ss"),
("ẞ", "Ss"),
("äh", "aeh"),
("Füße", "Fuesse"),
("GIEẞEN", "GIESsEN"),
],
)
def test_umlaut_replacement(self, name: str, expected: str):
assert parsers.parse_name(name) == expected

0 comments on commit 57e8737

Please sign in to comment.