aboutsummaryrefslogtreecommitdiff
blob: 9ae47477b9e90fff5560582bea9011ef20ef23b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
class AppTestStringIO:
    def test_stringio(self):
        import io
        sio = io.StringIO()
        sio.write(u'Hello ')
        sio.write(u'world')
        assert sio.getvalue() == u'Hello world'

        assert io.StringIO(u"hello").read() == u'hello'

    def test_capabilities(self):
        import io
        sio = io.StringIO()
        assert sio.readable()
        assert sio.writable()
        assert sio.seekable()
        assert not sio.isatty()
        assert not sio.closed
        assert not sio.line_buffering
        sio.close()
        raises(ValueError, sio.readable)
        raises(ValueError, sio.writable)
        raises(ValueError, sio.seekable)
        raises(ValueError, sio.isatty)
        assert sio.closed
        assert sio.errors is None

    def test_closed(self):
        import io
        sio = io.StringIO()
        sio.close()
        raises(ValueError, sio.read, 1)
        raises(ValueError, sio.write, u"text")

    def test_read(self):
        import io
        buf = u"1234567890"
        sio = io.StringIO(buf)

        assert buf[:1] == sio.read(1)
        assert buf[1:5] == sio.read(4)
        assert buf[5:] == sio.read(900)
        assert u"" == sio.read()

    def test_readline(self):
        import io
        sio = io.StringIO(u'123\n456')
        assert sio.readline(2) == '12'
        assert sio.readline(None) == '3\n'
        assert sio.readline() == '456'

    def test_seek(self):
        import io

        s = u"1234567890"
        sio = io.StringIO(s)

        sio.read(5)
        sio.seek(0)
        r = sio.read()
        assert r == s

        sio.seek(3)
        r = sio.read()
        assert r == s[3:]
        raises(TypeError, sio.seek, 0.0)

        exc_info = raises(ValueError, sio.seek, -3)
        assert exc_info.value.args[0] == "negative seek position: -3"

        raises(ValueError, sio.seek, 3, -1)
        raises(ValueError, sio.seek, 3, -3)

        sio.close()
        raises(ValueError, sio.seek, 0)

    def test_overseek(self):
        import io

        s = u"1234567890"
        sio = io.StringIO(s)

        res = sio.seek(11)
        assert res == 11
        res = sio.read()
        assert res == u""
        assert sio.tell() == 11
        assert sio.getvalue() == s
        sio.write(u"")
        assert sio.getvalue() == s
        sio.write(s)
        assert sio.getvalue() == s + u"\0" + s

    def test_tell(self):
        import io

        s = u"1234567890"
        sio = io.StringIO(s)

        assert sio.tell() == 0
        sio.seek(5)
        assert sio.tell() == 5
        sio.seek(10000)
        assert sio.tell() == 10000

        sio.close()
        raises(ValueError, sio.tell)

    def test_truncate(self):
        import io

        s = u"1234567890"
        sio = io.StringIO(s)

        raises(ValueError, sio.truncate, -1)
        sio.seek(6)
        res = sio.truncate()
        assert res == 6
        assert sio.getvalue() == s[:6]
        res = sio.truncate(4)
        assert res == 4
        assert sio.getvalue() == s[:4]
        # truncate() accepts long objects
        res = sio.truncate(4L)
        assert res == 4
        assert sio.getvalue() == s[:4]
        assert sio.tell() == 6
        sio.seek(0, 2)
        sio.write(s)
        assert sio.getvalue() == s[:4] + s
        pos = sio.tell()
        res = sio.truncate(None)
        assert res == pos
        assert sio.tell() == pos
        raises(TypeError, sio.truncate, '0')
        sio.close()
        raises(ValueError, sio.truncate, 0)

    def test_write_error(self):
        import io

        exc_info = raises(TypeError, io.StringIO, 3)
        assert "int" in exc_info.value.args[0]

        sio = io.StringIO(u"")
        exc_info = raises(TypeError, sio.write, 3)
        assert "int" in exc_info.value.args[0]

    def test_newline_none(self):
        import io

        sio = io.StringIO(u"a\nb\r\nc\rd", newline=None)
        res = list(sio)
        assert res == [u"a\n", u"b\n", u"c\n", u"d"]
        sio.seek(0)
        res = sio.read(1)
        assert res == u"a"
        res = sio.read(2)
        assert res == u"\nb"
        res = sio.read(2)
        assert res == u"\nc"
        res = sio.read(1)
        assert res == u"\n"

        sio = io.StringIO(newline=None)
        res = sio.write(u"a\n")
        assert res == 2
        res = sio.write(u"b\r\n")
        assert res == 3
        res = sio.write(u"c\rd")
        assert res == 3
        sio.seek(0)
        res = sio.read()
        assert res == u"a\nb\nc\nd"
        sio = io.StringIO(u"a\r\nb", newline=None)
        res = sio.read(3)
        assert res == u"a\nb"

    def test_newline_empty(self):
        import io

        sio = io.StringIO(u"a\nb\r\nc\rd", newline="")
        res = list(sio)
        assert res == [u"a\n", u"b\r\n", u"c\r", u"d"]
        sio.seek(0)
        res = sio.read(4)
        assert res == u"a\nb\r"
        res = sio.read(2)
        assert res == u"\nc"
        res = sio.read(1)
        assert res == u"\r"

        sio = io.StringIO(newline="")
        res = sio.write(u"a\n")
        assert res == 2
        res = sio.write(u"b\r")
        assert res == 2
        res = sio.write(u"\nc")
        assert res == 2
        res = sio.write(u"\rd")
        assert res == 2
        sio.seek(0)
        res = list(sio)
        assert res == [u"a\n", u"b\r\n", u"c\r", u"d"]

    def test_newline_lf(self):
        import io

        sio = io.StringIO(u"a\nb\r\nc\rd")
        res = list(sio)
        assert res == [u"a\n", u"b\r\n", u"c\rd"]

    def test_newline_cr(self):
        import io

        sio = io.StringIO(u"a\nb\r\nc\rd", newline="\r")
        res = sio.read()
        assert res == u"a\rb\r\rc\rd"
        sio.seek(0)
        res = list(sio)
        assert res == [u"a\r", u"b\r", u"\r", u"c\r", u"d"]

    def test_newline_crlf(self):
        import io

        sio = io.StringIO(u"a\nb\r\nc\rd", newline="\r\n")
        res = sio.read()
        assert res == u"a\r\nb\r\r\nc\rd"
        sio.seek(0)
        res = list(sio)
        assert res == [u"a\r\n", u"b\r\r\n", u"c\rd"]

    def test_newline_property(self):
        import io

        sio = io.StringIO(newline=None)
        assert sio.newlines is None
        sio.write(u"a\n")
        assert sio.newlines == "\n"
        sio.write(u"b\r\n")
        assert sio.newlines == ("\n", "\r\n")
        sio.write(u"c\rd")
        assert sio.newlines == ("\r", "\n", "\r\n")

    def test_iterator(self):
        import io

        s = u"1234567890\n"
        sio = io.StringIO(s * 10)

        assert iter(sio) is sio
        assert hasattr(sio, "__iter__")
        assert hasattr(sio, "next")

        i = 0
        for line in sio:
            assert line == s
            i += 1
        assert i == 10
        sio.seek(0)
        i = 0
        for line in sio:
            assert line == s
            i += 1
        assert i == 10
        sio = io.StringIO(s * 2)
        sio.close()
        raises(ValueError, next, sio)

    def test_getstate(self):
        import io

        sio = io.StringIO()
        state = sio.__getstate__()
        assert len(state) == 4
        assert isinstance(state[0], unicode)
        assert isinstance(state[1], str)
        assert isinstance(state[2], int)
        assert isinstance(state[3], dict)
        sio.close()
        raises(ValueError, sio.__getstate__)

    def test_setstate(self):
        import io

        sio = io.StringIO()
        sio.__setstate__((u"no error", u"\n", 0, None))
        sio.__setstate__((u"no error", u"", 0, {"spam": 3}))
        raises(ValueError, sio.__setstate__, (u"", u"f", 0, None))
        raises(ValueError, sio.__setstate__, (u"", u"", -1, None))
        raises(TypeError, sio.__setstate__, ("", u"", 0, None))
        raises(TypeError, sio.__setstate__, (u"", u"", 0.0, None))
        raises(TypeError, sio.__setstate__, (u"", u"", 0, 0))
        raises(TypeError, sio.__setstate__, (u"len-test", 0))
        raises(TypeError, sio.__setstate__)
        raises(TypeError, sio.__setstate__, 0)
        sio.close()
        raises(ValueError, sio.__setstate__, (u"closed", u"", 0, None))