diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 72dad99..fc7af22 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -111,6 +111,7 @@ concatenate, dedent, generate_slug, + join_lines, pluralize, random_string, trim_empty_lines, @@ -130,6 +131,21 @@ class HumanFriendlyTestCase(TestCase): """Container for the `humanfriendly` test suite.""" + def test_join_lines_consecutive_short_lines(self): + """Join every hard wrap even when an intervening line has one character.""" + assert join_lines('a\nb\nc\nd') == 'a b c d' + assert join_lines('one\na\nword') == 'one a word' + + def test_join_lines_windows_line_endings(self): + """Join CRLF hard wraps without leaving carriage returns behind.""" + assert join_lines('first\r\nsecond\r\nthird') == 'first second third' + assert join_lines('a\r\nb\r\nc') == 'a b c' + + def test_join_lines_preserves_paragraphs_and_indentation(self): + """Leave paragraph boundaries and indented lines intact.""" + for text in ('a\n\nb', 'a\r\n\r\nb', 'a\n b', 'a \nb', '\na\n', ''): + assert join_lines(text) == text + def test_case_insensitive_dict(self): """Test the CaseInsensitiveDict class.""" # Test support for the dict(iterable) signature. diff --git a/humanfriendly/text.py b/humanfriendly/text.py index a257a6a..7b30e08 100644 --- a/humanfriendly/text.py +++ b/humanfriendly/text.py @@ -289,7 +289,7 @@ def join_lines(text): break :func:`join_lines()` (in that case you can use :func:`dedent()` before calling :func:`join_lines()`). """ - return re.sub(r'(\S)\n(\S)', r'\1 \2', text) + return re.sub(r'(?<=\S)\r?\n(?=\S)', ' ', text) def pluralize(count, singular, plural=None):