Skip to content

Commit 430c051

Browse files
Add tests and simplify normalize
1 parent 66d8eac commit 430c051

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

Lib/test/test_tools/test_i18n.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import sys
66
import unittest
7+
from test.test_decimal import skip_expected
78
from textwrap import dedent
89
from pathlib import Path
910

@@ -18,7 +19,7 @@
1819

1920

2021
with imports_under_tool("i18n"):
21-
from pygettext import parse_spec
22+
from pygettext import parse_spec, normalize, make_escapes
2223

2324

2425
def normalize_POT_file(pot):
@@ -516,6 +517,42 @@ def test_parse_keyword_spec(self):
516517
parse_spec(spec)
517518
self.assertEqual(str(cm.exception), message)
518519

520+
def test_normalize_multiline(self):
521+
# required to set up normalize
522+
class NormOptions:
523+
width = 78
524+
make_escapes(True)
525+
526+
s = 'multi-line\n translation'
527+
s_expected = '""\n"multi-line\\n"\n" translation"'
528+
529+
data = normalize(s, 'UTF-8', NormOptions)
530+
self.assertEqual(s_expected, data)
531+
532+
def test_normalize_wrap(self):
533+
# required to set up normalize
534+
class NormOptions:
535+
width = 30
536+
make_escapes(True)
537+
538+
s = 'this string should be wrapped to 30 chars'
539+
s_expected = '""\n"this string should be wrapped "\n"to 30 chars"'
540+
541+
data = normalize(s, 'UTF-8', NormOptions)
542+
self.assertEqual(s_expected, data)
543+
544+
def test_normalize_nostr(self):
545+
# required to set up normalize
546+
class NormOptions:
547+
width = 78
548+
make_escapes(True)
549+
550+
s = ''
551+
s_expected = '""'
552+
553+
data = normalize(s, 'UTF-8', NormOptions)
554+
self.assertEqual(s_expected, data)
555+
519556

520557
def extract_from_snapshots():
521558
snapshots = {

Tools/i18n/pygettext.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def normalize(s, encoding, options):
225225
words = space_splitter(line)
226226
words.reverse()
227227
buf = []
228-
size = 2
228+
size = 0
229229
while words:
230230
word = words.pop()
231231
escaped_word_len = len(escape(word, encoding))
@@ -235,17 +235,13 @@ def normalize(s, encoding, options):
235235
else:
236236
lines.append(''.join(buf))
237237
buf = [word]
238-
size = 2 + escaped_word_len
238+
size = escaped_word_len
239239
lines.append(''.join(buf))
240240
else:
241241
lines.append(line)
242242
if len(lines) <= 1:
243243
return f'"{escape(s, encoding)}"'
244-
if lines and not lines[-1]:
245-
del lines[-1]
246-
lines[-1] += '\n'
247-
return '""\n' + '\n'.join(
248-
[f'"{escape(line, encoding)}"' for line in lines])
244+
return '""\n' + '\n'.join([f'"{escape(line, encoding)}"' for line in lines])
249245

250246

251247
def containsAny(str, set):

0 commit comments

Comments
 (0)