[pypy-commit] pypy default: Add some tests for DecodeBuffer
rlamy
pypy.commits at gmail.com
Sat Nov 25 20:24:27 EST 2017
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch:
Changeset: r93176:e8e611955c9a
Date: 2017-11-26 01:22 +0000
http://bitbucket.org/pypy/pypy/changeset/e8e611955c9a/
Log: Add some tests for DecodeBuffer
diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -290,8 +290,8 @@
class DecodeBuffer(object):
- def __init__(self):
- self.text = None
+ def __init__(self, text=None):
+ self.text = text
self.pos = 0
def set(self, space, w_decoded):
diff --git a/pypy/module/_io/test/test_interp_textio.py b/pypy/module/_io/test/test_interp_textio.py
--- a/pypy/module/_io/test/test_interp_textio.py
+++ b/pypy/module/_io/test/test_interp_textio.py
@@ -1,6 +1,10 @@
-from hypothesis import given, strategies as st, assume
+import pytest
+try:
+ from hypothesis import given, strategies as st, assume
+except ImportError:
+ pytest.skip("hypothesis required")
from pypy.module._io.interp_bytesio import W_BytesIO
-from pypy.module._io.interp_textio import W_TextIOWrapper
+from pypy.module._io.interp_textio import W_TextIOWrapper, DecodeBuffer
LINESEP = ['', '\r', '\n', '\r\n']
@@ -31,3 +35,34 @@
else:
break
assert u''.join(lines) == txt
+
+ at given(st.text())
+def test_read_buffer(text):
+ buf = DecodeBuffer(text)
+ assert buf.get_chars(-1) == text
+ assert buf.exhausted()
+
+ at given(st.text(), st.lists(st.integers(min_value=0)))
+def test_readn_buffer(text, sizes):
+ buf = DecodeBuffer(text)
+ strings = []
+ for n in sizes:
+ s = buf.get_chars(n)
+ if not buf.exhausted():
+ assert len(s) == n
+ else:
+ assert len(s) <= n
+ strings.append(s)
+ assert ''.join(strings) == text[:sum(sizes)]
+
+ at given(st.text())
+def test_next_char(text):
+ buf = DecodeBuffer(text)
+ chars = []
+ try:
+ while True:
+ chars.append(buf.next_char())
+ except StopIteration:
+ pass
+ assert buf.exhausted()
+ assert u''.join(chars) == text
More information about the pypy-commit
mailing list