From 726804ea405483872ebc93772aea0ab0bfce0bcf Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 20 Jun 2018 17:11:58 +1000 Subject: tests: Move non-filesystem io tests to basics dir with io_ prefix. --- tests/basics/io_buffered_writer.py | 27 ++++++++++++++++++++ tests/basics/io_buffered_writer.py.exp | 5 ++++ tests/basics/io_bytesio_cow.py | 20 +++++++++++++++ tests/basics/io_bytesio_ext.py | 28 +++++++++++++++++++++ tests/basics/io_bytesio_ext2.py | 13 ++++++++++ tests/basics/io_bytesio_ext2.py.exp | 1 + tests/basics/io_iobase.py | 19 ++++++++++++++ tests/basics/io_stringio1.py | 45 ++++++++++++++++++++++++++++++++++ tests/basics/io_stringio_with.py | 9 +++++++ tests/basics/io_write_ext.py | 26 ++++++++++++++++++++ tests/basics/io_write_ext.py.exp | 5 ++++ tests/io/buffered_writer.py | 27 -------------------- tests/io/buffered_writer.py.exp | 5 ---- tests/io/bytesio_cow.py | 20 --------------- tests/io/bytesio_ext.py | 28 --------------------- tests/io/bytesio_ext2.py | 13 ---------- tests/io/bytesio_ext2.py.exp | 1 - tests/io/iobase.py | 19 -------------- tests/io/stringio1.py | 45 ---------------------------------- tests/io/stringio_with.py | 9 ------- tests/io/write_ext.py | 26 -------------------- tests/io/write_ext.py.exp | 5 ---- 22 files changed, 198 insertions(+), 198 deletions(-) create mode 100644 tests/basics/io_buffered_writer.py create mode 100644 tests/basics/io_buffered_writer.py.exp create mode 100644 tests/basics/io_bytesio_cow.py create mode 100644 tests/basics/io_bytesio_ext.py create mode 100644 tests/basics/io_bytesio_ext2.py create mode 100644 tests/basics/io_bytesio_ext2.py.exp create mode 100644 tests/basics/io_iobase.py create mode 100644 tests/basics/io_stringio1.py create mode 100644 tests/basics/io_stringio_with.py create mode 100644 tests/basics/io_write_ext.py create mode 100644 tests/basics/io_write_ext.py.exp delete mode 100644 tests/io/buffered_writer.py delete mode 100644 tests/io/buffered_writer.py.exp delete mode 100644 tests/io/bytesio_cow.py delete mode 100644 tests/io/bytesio_ext.py delete mode 100644 tests/io/bytesio_ext2.py delete mode 100644 tests/io/bytesio_ext2.py.exp delete mode 100644 tests/io/iobase.py delete mode 100644 tests/io/stringio1.py delete mode 100644 tests/io/stringio_with.py delete mode 100644 tests/io/write_ext.py delete mode 100644 tests/io/write_ext.py.exp 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 @@ + 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' diff --git a/tests/io/buffered_writer.py b/tests/io/buffered_writer.py deleted file mode 100644 index c2cedb991..000000000 --- a/tests/io/buffered_writer.py +++ /dev/null @@ -1,27 +0,0 @@ -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/io/buffered_writer.py.exp b/tests/io/buffered_writer.py.exp deleted file mode 100644 index d086935a9..000000000 --- a/tests/io/buffered_writer.py.exp +++ /dev/null @@ -1,5 +0,0 @@ -b'' -b'foobarfo' -b'foobarfoobar' -b'foobarfoobar' -b'foo' diff --git a/tests/io/bytesio_cow.py b/tests/io/bytesio_cow.py deleted file mode 100644 index 92654a000..000000000 --- a/tests/io/bytesio_cow.py +++ /dev/null @@ -1,20 +0,0 @@ -# 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/io/bytesio_ext.py b/tests/io/bytesio_ext.py deleted file mode 100644 index e454b2fd9..000000000 --- a/tests/io/bytesio_ext.py +++ /dev/null @@ -1,28 +0,0 @@ -# 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/io/bytesio_ext2.py b/tests/io/bytesio_ext2.py deleted file mode 100644 index 8f624fd58..000000000 --- a/tests/io/bytesio_ext2.py +++ /dev/null @@ -1,13 +0,0 @@ -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/io/bytesio_ext2.py.exp b/tests/io/bytesio_ext2.py.exp deleted file mode 100644 index 724aaf63a..000000000 --- a/tests/io/bytesio_ext2.py.exp +++ /dev/null @@ -1 +0,0 @@ - True diff --git a/tests/io/iobase.py b/tests/io/iobase.py deleted file mode 100644 index 6f554b00f..000000000 --- a/tests/io/iobase.py +++ /dev/null @@ -1,19 +0,0 @@ -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/io/stringio1.py b/tests/io/stringio1.py deleted file mode 100644 index 9f7c1e44e..000000000 --- a/tests/io/stringio1.py +++ /dev/null @@ -1,45 +0,0 @@ -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/io/stringio_with.py b/tests/io/stringio_with.py deleted file mode 100644 index c35975445..000000000 --- a/tests/io/stringio_with.py +++ /dev/null @@ -1,9 +0,0 @@ -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/io/write_ext.py b/tests/io/write_ext.py deleted file mode 100644 index 5a6eaa35c..000000000 --- a/tests/io/write_ext.py +++ /dev/null @@ -1,26 +0,0 @@ -# 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/io/write_ext.py.exp b/tests/io/write_ext.py.exp deleted file mode 100644 index 0f9c6bfbc..000000000 --- a/tests/io/write_ext.py.exp +++ /dev/null @@ -1,5 +0,0 @@ -b'fo' -b'fofoo' -b'fofoooob' -b'fofooooboobar' -b'fofooooboobar' -- cgit v1.2.3