[lang-ref] ( string_split_lines ) ( python )
@pytest.mark.parametrize(
'text, count',
[
('aa\nbb\ncc', 3),
('aa\nbb\ncc\n', 3), # the last newline is ignored
('aa\nbb\ncc\n\n', 4), # only a single trailing newline is ignored
('\naa\nbb\ncc', 4), # a leading newline is not ignored
('aa\n\nbb\ncc', 4), # a middle newline is not ignored
],
)
def test_string_split_lines(text, count):
# split lines
lines = text.splitlines()
assert len(lines) == count
def test_string_split_lines():
lines = '''\
aaa
bbb
ccc
'''
r = ''
for line in lines.splitlines():
r += line.strip()
assert r == 'aaabbbccc'
@pytest.mark.parametrize(
'text, count',
[
('aa\nbb\ncc', 3),
('aa\nbb\ncc\n', 4),
('aa\nbb\ncc\n\n', 5),
('\naa\nbb\ncc', 4),
('aa\n\nbb\ncc', 4),
],
)
def test_string_split_lines_alternative1(text, count):
import re
lines = re.split(r'\r?\n', text)
assert len(lines) == count
@pytest.mark.parametrize(
'text, count',
[
('aa\nbb\ncc', 3),
('aa\nbb\ncc\n', 3),
('aa\nbb\ncc\n\n', 3),
('\naa\nbb\ncc', 4),
('aa\n\nbb\ncc', 4),
],
)
def test_string_split_lines_alternative2(text, count):
lines = text.rstrip().splitlines()
assert len(lines) == count