Skip to content

Commit

Permalink
Formalise constraint on _NormalizeString's inputs
Browse files Browse the repository at this point in the history
Now that all strings given as inputs to _NormalizeString have been
verified (or corrected) to be correctly delimited with double quotes,
there's no reason to continue doing an internal strip anymore. Moreover,
we can express this internal constraint with an assertion to avoid
issues in the future.

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
  • Loading branch information
rtobar committed Dec 17, 2024
1 parent 00062fb commit ff5213b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
3 changes: 2 additions & 1 deletion babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ def __init__(self, *args: str) -> None:
self.append(arg)

def append(self, s: str) -> None:
self._strs.append(s.strip())
assert s[0] == '"' and s[-1] == '"'
self._strs.append(s)

def denormalize(self) -> str:
return ''.join(map(unescape, self._strs))
Expand Down
21 changes: 8 additions & 13 deletions tests/messages/test_normalized_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@


def test_normalized_string():
ab1 = _NormalizedString('"a"', '"b" ')
ab2 = _NormalizedString('"a"', ' "b"')
ac1 = _NormalizedString('"a"', '"c"')
ac2 = _NormalizedString(' "a"', '"c" ')
ab = _NormalizedString('"a"', '"b"')
ac = _NormalizedString('"a"', '"c"')
z = _NormalizedString()
assert ab1 == ab2 and ac1 == ac2 # __eq__
assert ab1 < ac1 # __lt__
assert ac1 > ab2 # __gt__
assert ac1 >= ac2 # __ge__
assert ab1 <= ab2 # __le__
assert ab1 != ac1 # __ne__
assert ab < ac # __lt__
assert ac > ab # __gt__
assert ab != ac # __ne__
assert not z # __nonzero__ / __bool__
assert sorted([ab1, ab2, ac1]) == [ab1, ab2, ac1] # sorted() is stable
assert sorted([ac, ab, z]) == [z, ab, ac] # sorted() is stable


@pytest.mark.parametrize(
Expand All @@ -38,10 +33,10 @@ def test_denormalized_simple_normalized_string(original, denormalized):
"originals, denormalized",
(
(('"a"', '"b"'), "ab"),
(('"a" ', '"b"'), "ab"),
(('"a"', '"b"'), "ab"),
(('"ab"', '""'), "ab"),
(('"ab"', '"c"'), "abc"),
(('"a"', ' "bc"'), "abc"),
(('"a"', '"bc"'), "abc"),
),
)
def test_denormalized_multi_normalized_string(originals, denormalized):
Expand Down

0 comments on commit ff5213b

Please sign in to comment.