| 1 | from hypothesis import given, strategies as st |
| 2 | |
| 3 | from io import StringIO |
| 4 | from os import linesep |
| 5 | |
| 6 | LINE_ENDINGS = [u'\n', u'\r', u'\r\n'] |
| 7 | |
| 8 | @given(txt=st.text(), newline=st.sampled_from(['', '\n'])) |
| 9 | def test_simple(txt, newline): |
| 10 | sio = StringIO(txt, newline=newline) |
| 11 | assert sio.getvalue() == txt |
| 12 | |
| 13 | @given(values=st.lists( |
| 14 | st.tuples( |
| 15 | st.text(st.characters(blacklist_characters='\r\n'), min_size=1), |
| 16 | st.sampled_from(LINE_ENDINGS)))) |
| 17 | def test_universal(values): |
| 18 | output_lines = [line + linesep for line, ending in values] |
| 19 | output = u''.join(output_lines) |
| 20 | |
| 21 | input = u''.join(line + ending for line, ending in values) |
| 22 | sio = StringIO(input, newline=None) |
| 23 | sio.seek(0) |
| 24 | assert list(sio) == output_lines |
| 25 | assert sio.getvalue() == output |
| 26 | |
| 27 | sio2 = StringIO(newline=None) |
| 28 | for line, ending in values: |
| 29 | sio2.write(line) |
| 30 | sio2.write(ending) |
| 31 | sio2.seek(0) |
| 32 | assert list(sio2) == output_lines |
| 33 | assert sio2.getvalue() == output |
| 34 | |
| 35 | @given( |
| 36 | lines=st.lists(st.text(st.characters(blacklist_characters='\r\n'))), |
| 37 | newline=st.sampled_from(['\r', '\r\n'])) |
| 38 | def test_crlf(lines, newline): |
| 39 | output_lines = [line + newline for line in lines] |
| 40 | output = u''.join(output_lines) |
| 41 | |
| 42 | input = u''.join(line + '\n' for line in lines) |
| 43 | sio = StringIO(input, newline=newline) |
| 44 | sio.seek(0) |
| 45 | assert list(sio) == output_lines |
| 46 | assert sio.getvalue() == output |
| 47 | |
| 48 | sio2 = StringIO(newline=newline) |
| 49 | for line in lines: |
| 50 | sio2.write(line) |
| 51 | sio2.write(u'\n') |
| 52 | sio2.seek(0) |
| 53 | assert list(sio2) == output_lines |
| 54 | assert sio2.getvalue() == ''.join(output_lines) |