From f4696a6245bf66c3e2c9723671f6871aa0b27c09 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 7 Jan 2019 18:46:16 +0100 Subject: [PATCH] Add \r as a valid linebreak for splitlines --- parso/utils.py | 2 +- test/test_utils.py | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/parso/utils.py b/parso/utils.py index f7a38cc..dfc98c8 100644 --- a/parso/utils.py +++ b/parso/utils.py @@ -41,7 +41,7 @@ def split_lines(string, keepends=False): # The stdlib's implementation of the end is inconsistent when calling # it with/without keepends. One time there's an empty string in the # end, one time there's none. - if string.endswith('\n') or string == '': + if string.endswith('\n') or string.endswith('\r') or string == '': lst.append('') return lst else: diff --git a/test/test_utils.py b/test/test_utils.py index 4425bd2..ea32589 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -3,21 +3,36 @@ from codecs import BOM_UTF8 from parso.utils import split_lines, python_bytes_to_unicode import parso - -def test_split_lines_no_keepends(): - assert split_lines('asd\r\n') == ['asd', ''] - assert split_lines('asd\r\n\f') == ['asd', '\f'] - assert split_lines('\fasd\r\n') == ['\fasd', ''] - assert split_lines('') == [''] - assert split_lines('\n') == ['', ''] +import pytest -def test_split_lines_keepends(): - assert split_lines('asd\r\n', keepends=True) == ['asd\r\n', ''] - assert split_lines('asd\r\n\f', keepends=True) == ['asd\r\n', '\f'] - assert split_lines('\fasd\r\n', keepends=True) == ['\fasd\r\n', ''] - assert split_lines('', keepends=True) == [''] - assert split_lines('\n', keepends=True) == ['\n', ''] +@pytest.mark.parametrize( + ('string', 'expected_result', 'keepends'), [ + ('asd\r\n', ['asd', ''], False), + ('asd\r\n', ['asd\r\n', ''], True), + ('asd\r', ['asd', ''], False), + ('asd\r', ['asd\r', ''], True), + ('asd\n', ['asd', ''], False), + ('asd\n', ['asd\n', ''], True), + + ('asd\r\n\f', ['asd', '\f'], False), + ('asd\r\n\f', ['asd\r\n', '\f'], True), + + ('\fasd\r\n', ['\fasd', ''], False), + ('\fasd\r\n', ['\fasd\r\n', ''], True), + + ('', [''], False), + ('', [''], True), + + ('\n', ['', ''], False), + ('\n', ['\n', ''], True), + + ('\r', ['', ''], False), + ('\r', ['\r', ''], True), + ] +) +def test_split_lines(string, expected_result, keepends): + assert split_lines(string, keepends=keepends) == expected_result def test_python_bytes_to_unicode_unicode_text():