File: test_stringio.py

package info (click to toggle)
pypy3 7.3.20%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212,628 kB
  • sloc: python: 2,101,020; ansic: 540,684; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (54 lines) | stat: -rw-r--r-- 1,637 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from hypothesis import given, strategies as st

from io import StringIO
from os import linesep

LINE_ENDINGS = [u'\n', u'\r', u'\r\n']

@given(txt=st.text(), newline=st.sampled_from(['', '\n']))
def test_simple(txt, newline):
    sio = StringIO(txt, newline=newline)
    assert sio.getvalue() == txt

@given(values=st.lists(
    st.tuples(
        st.text(st.characters(blacklist_characters='\r\n'), min_size=1),
        st.sampled_from(LINE_ENDINGS))))
def test_universal(values):
    output_lines = [line + linesep for line, ending in values]
    output = u''.join(output_lines)

    input = u''.join(line + ending for line, ending in values)
    sio = StringIO(input, newline=None)
    sio.seek(0)
    assert list(sio) == output_lines
    assert sio.getvalue() == output

    sio2 = StringIO(newline=None)
    for line, ending in values:
        sio2.write(line)
        sio2.write(ending)
    sio2.seek(0)
    assert list(sio2) == output_lines
    assert sio2.getvalue() == output

@given(
    lines=st.lists(st.text(st.characters(blacklist_characters='\r\n'))),
    newline=st.sampled_from(['\r', '\r\n']))
def test_crlf(lines, newline):
    output_lines = [line + newline for line in lines]
    output = u''.join(output_lines)

    input = u''.join(line + '\n' for line in lines)
    sio = StringIO(input, newline=newline)
    sio.seek(0)
    assert list(sio) == output_lines
    assert sio.getvalue() == output

    sio2 = StringIO(newline=newline)
    for line in lines:
        sio2.write(line)
        sio2.write(u'\n')
    sio2.seek(0)
    assert list(sio2) == output_lines
    assert sio2.getvalue() == ''.join(output_lines)