From 1a9a142e52d11106334c013197759166208b6254 Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Sun, 17 Nov 2024 23:05:01 +0800 Subject: [PATCH] Formalise constraint on _NormalizeString's inputs 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 --- babel/messages/pofile.py | 3 ++- tests/messages/test_normalized_string.py | 21 ++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 57b038883..5d7c80e8c 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -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)) diff --git a/tests/messages/test_normalized_string.py b/tests/messages/test_normalized_string.py index 735603770..f398a1680 100644 --- a/tests/messages/test_normalized_string.py +++ b/tests/messages/test_normalized_string.py @@ -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( @@ -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):