diff options
| author | Damien George <damien.p.george@gmail.com> | 2018-06-20 17:11:58 +1000 |
|---|---|---|
| committer | Damien George <damien.p.george@gmail.com> | 2018-06-27 16:55:05 +1000 |
| commit | 726804ea405483872ebc93772aea0ab0bfce0bcf (patch) | |
| tree | f3ee9544b5ea83dfa671e40a4341bf91524e785c /tests/basics | |
| parent | bc6c56d75d1b954b42b01dc8e10e283523b2e902 (diff) | |
tests: Move non-filesystem io tests to basics dir with io_ prefix.
Diffstat (limited to 'tests/basics')
| -rw-r--r-- | tests/basics/io_buffered_writer.py | 27 | ||||
| -rw-r--r-- | tests/basics/io_buffered_writer.py.exp | 5 | ||||
| -rw-r--r-- | tests/basics/io_bytesio_cow.py | 20 | ||||
| -rw-r--r-- | tests/basics/io_bytesio_ext.py | 28 | ||||
| -rw-r--r-- | tests/basics/io_bytesio_ext2.py | 13 | ||||
| -rw-r--r-- | tests/basics/io_bytesio_ext2.py.exp | 1 | ||||
| -rw-r--r-- | tests/basics/io_iobase.py | 19 | ||||
| -rw-r--r-- | tests/basics/io_stringio1.py | 45 | ||||
| -rw-r--r-- | tests/basics/io_stringio_with.py | 9 | ||||
| -rw-r--r-- | tests/basics/io_write_ext.py | 26 | ||||
| -rw-r--r-- | tests/basics/io_write_ext.py.exp | 5 |
11 files changed, 198 insertions, 0 deletions
diff --git a/tests/basics/io_buffered_writer.py b/tests/basics/io_buffered_writer.py new file mode 100644 index 000000000..c2cedb991 --- /dev/null +++ b/tests/basics/io_buffered_writer.py @@ -0,0 +1,27 @@ +import uio as io + +try: + io.BytesIO + io.BufferedWriter +except AttributeError: + print('SKIP') + raise SystemExit + +bts = io.BytesIO() +buf = io.BufferedWriter(bts, 8) + +buf.write(b"foobar") +print(bts.getvalue()) +buf.write(b"foobar") +# CPython has different flushing policy, so value below is different +print(bts.getvalue()) +buf.flush() +print(bts.getvalue()) +buf.flush() +print(bts.getvalue()) + +# special case when alloc is a factor of total buffer length +bts = io.BytesIO() +buf = io.BufferedWriter(bts, 1) +buf.write(b"foo") +print(bts.getvalue()) diff --git a/tests/basics/io_buffered_writer.py.exp b/tests/basics/io_buffered_writer.py.exp new file mode 100644 index 000000000..d086935a9 --- /dev/null +++ b/tests/basics/io_buffered_writer.py.exp @@ -0,0 +1,5 @@ +b'' +b'foobarfo' +b'foobarfoobar' +b'foobarfoobar' +b'foo' diff --git a/tests/basics/io_bytesio_cow.py b/tests/basics/io_bytesio_cow.py new file mode 100644 index 000000000..92654a000 --- /dev/null +++ b/tests/basics/io_bytesio_cow.py @@ -0,0 +1,20 @@ +# Make sure that write operations on io.BytesIO don't +# change original object it was constructed from. +try: + import uio as io +except ImportError: + import io + +b = b"foobar" + +a = io.BytesIO(b) +a.write(b"1") +print(b) +print(a.getvalue()) + +b = bytearray(b"foobar") + +a = io.BytesIO(b) +a.write(b"1") +print(b) +print(a.getvalue()) diff --git a/tests/basics/io_bytesio_ext.py b/tests/basics/io_bytesio_ext.py new file mode 100644 index 000000000..e454b2fd9 --- /dev/null +++ b/tests/basics/io_bytesio_ext.py @@ -0,0 +1,28 @@ +# Extended stream operations on io.BytesIO +try: + import uio as io +except ImportError: + import io + +a = io.BytesIO(b"foobar") +a.seek(10) +print(a.read(10)) + +a = io.BytesIO() +print(a.seek(8)) +a.write(b"123") +print(a.getvalue()) + +print(a.seek(0, 1)) + +print(a.seek(-1, 2)) +a.write(b"0") +print(a.getvalue()) + +a.flush() +print(a.getvalue()) + +a.seek(0) +arr = bytearray(10) +print(a.readinto(arr)) +print(arr) diff --git a/tests/basics/io_bytesio_ext2.py b/tests/basics/io_bytesio_ext2.py new file mode 100644 index 000000000..8f624fd58 --- /dev/null +++ b/tests/basics/io_bytesio_ext2.py @@ -0,0 +1,13 @@ +try: + import uio as io +except ImportError: + import io + +a = io.BytesIO(b"foobar") +try: + a.seek(-10) +except Exception as e: + # CPython throws ValueError, but MicroPython has consistent stream + # interface, so BytesIO raises the same error as a real file, which + # is OSError(EINVAL). + print(type(e), e.args[0] > 0) diff --git a/tests/basics/io_bytesio_ext2.py.exp b/tests/basics/io_bytesio_ext2.py.exp new file mode 100644 index 000000000..724aaf63a --- /dev/null +++ b/tests/basics/io_bytesio_ext2.py.exp @@ -0,0 +1 @@ +<class 'OSError'> True diff --git a/tests/basics/io_iobase.py b/tests/basics/io_iobase.py new file mode 100644 index 000000000..6f554b00f --- /dev/null +++ b/tests/basics/io_iobase.py @@ -0,0 +1,19 @@ +try: + import uio as io +except: + import io + +try: + io.IOBase +except AttributeError: + print('SKIP') + raise SystemExit + + +class MyIO(io.IOBase): + def write(self, buf): + # CPython and uPy pass in different types for buf (str vs bytearray) + print('write', len(buf)) + return len(buf) + +print('test', file=MyIO()) diff --git a/tests/basics/io_stringio1.py b/tests/basics/io_stringio1.py new file mode 100644 index 000000000..9f7c1e44e --- /dev/null +++ b/tests/basics/io_stringio1.py @@ -0,0 +1,45 @@ +try: + import uio as io +except ImportError: + import io + +a = io.StringIO() +print('io.StringIO' in repr(a)) +print(a.getvalue()) +print(a.read()) + +a = io.StringIO("foobar") +print(a.getvalue()) +print(a.read()) +print(a.read()) + +a = io.StringIO() +a.write("foo") +print(a.getvalue()) + +a = io.StringIO("foo") +a.write("12") +print(a.getvalue()) + +a = io.StringIO("foo") +a.write("123") +print(a.getvalue()) + +a = io.StringIO("foo") +a.write("1234") +print(a.getvalue()) + +a = io.StringIO() +a.write("foo") +print(a.read()) + +a = io.StringIO() +a.close() +for f in [a.read, a.getvalue, lambda:a.write("")]: + # CPython throws for operations on closed I/O, MicroPython makes + # the underlying string empty unless MICROPY_CPYTHON_COMPAT defined + try: + f() + print("ValueError") + except ValueError: + print("ValueError") diff --git a/tests/basics/io_stringio_with.py b/tests/basics/io_stringio_with.py new file mode 100644 index 000000000..c35975445 --- /dev/null +++ b/tests/basics/io_stringio_with.py @@ -0,0 +1,9 @@ +try: + import uio as io +except ImportError: + import io + +# test __enter__/__exit__ +with io.StringIO() as b: + b.write("foo") + print(b.getvalue()) diff --git a/tests/basics/io_write_ext.py b/tests/basics/io_write_ext.py new file mode 100644 index 000000000..5a6eaa35c --- /dev/null +++ b/tests/basics/io_write_ext.py @@ -0,0 +1,26 @@ +# This tests extended (MicroPython-specific) form of write: +# write(buf, len) and write(buf, offset, len) +import uio + +try: + uio.BytesIO +except AttributeError: + print('SKIP') + raise SystemExit + +buf = uio.BytesIO() + +buf.write(b"foo", 2) +print(buf.getvalue()) + +buf.write(b"foo", 100) +print(buf.getvalue()) + +buf.write(b"foobar", 1, 3) +print(buf.getvalue()) + +buf.write(b"foobar", 1, 100) +print(buf.getvalue()) + +buf.write(b"foobar", 100, 100) +print(buf.getvalue()) diff --git a/tests/basics/io_write_ext.py.exp b/tests/basics/io_write_ext.py.exp new file mode 100644 index 000000000..0f9c6bfbc --- /dev/null +++ b/tests/basics/io_write_ext.py.exp @@ -0,0 +1,5 @@ +b'fo' +b'fofoo' +b'fofoooob' +b'fofooooboobar' +b'fofooooboobar' |
