Skip to content

Commit fee9c99

Browse files
Merge pull request RustPython#1026 from rmliddle/BytesIO
String/BytesIO
2 parents 2f75562 + 9bc392c commit fee9c99

File tree

5 files changed

+338
-38
lines changed

5 files changed

+338
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ crate and webpack.
197197

198198
# Code style
199199

200-
The code style used is the default rustfmt codestyle. Please format your code accordingly.
200+
The code style used is the default [rustfmt](https://github.com/rust-lang/rustfmt) codestyle. Please format your code accordingly.
201201

202202
# Community
203203

tests/snippets/bytes_io.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
from io import BytesIO
3+
4+
def test_01():
5+
bytes_string = b'Test String 1'
6+
7+
f = BytesIO()
8+
f.write(bytes_string)
9+
10+
assert f.getvalue() == bytes_string
11+
12+
def test_02():
13+
bytes_string = b'Test String 2'
14+
f = BytesIO(bytes_string)
15+
16+
assert f.read() == bytes_string
17+
assert f.read() == b''
18+
19+
def test_03():
20+
"""
21+
Tests that the read method (integer arg)
22+
returns the expected value
23+
"""
24+
string = b'Test String 3'
25+
f = BytesIO(string)
26+
27+
assert f.read(1) == b'T'
28+
assert f.read(1) == b'e'
29+
assert f.read(1) == b's'
30+
assert f.read(1) == b't'
31+
32+
def test_04():
33+
"""
34+
Tests that the read method increments the
35+
cursor position and the seek method moves
36+
the cursor to the appropriate position
37+
"""
38+
string = b'Test String 4'
39+
f = BytesIO(string)
40+
41+
assert f.read(4) == b'Test'
42+
assert f.seek(0) == 0
43+
assert f.read(4) == b'Test'
44+
45+
if __name__ == "__main__":
46+
test_01()
47+
test_02()
48+
test_03()
49+
test_04()
50+

tests/snippets/string_io.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
from io import StringIO
3+
4+
def test_01():
5+
"""
6+
Test that the constructor and getvalue
7+
method return expected values
8+
"""
9+
string = 'Test String 1'
10+
f = StringIO()
11+
f.write(string)
12+
13+
assert f.getvalue() == string
14+
15+
def test_02():
16+
"""
17+
Test that the read method (no arg)
18+
results the expected value
19+
"""
20+
string = 'Test String 2'
21+
f = StringIO(string)
22+
23+
assert f.read() == string
24+
assert f.read() == ''
25+
26+
def test_03():
27+
"""
28+
Tests that the read method (integer arg)
29+
returns the expected value
30+
"""
31+
string = 'Test String 3'
32+
f = StringIO(string)
33+
34+
assert f.read(1) == 'T'
35+
assert f.read(1) == 'e'
36+
assert f.read(1) == 's'
37+
assert f.read(1) == 't'
38+
39+
def test_04():
40+
"""
41+
Tests that the read method increments the
42+
cursor position and the seek method moves
43+
the cursor to the appropriate position
44+
"""
45+
string = 'Test String 4'
46+
f = StringIO(string)
47+
48+
assert f.read(4) == 'Test'
49+
assert f.seek(0) == 0
50+
assert f.read(4) == 'Test'
51+
52+
if __name__ == "__main__":
53+
test_01()
54+
test_02()
55+
test_03()
56+
test_04()

tests/snippets/test_io.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)