PageRenderTime 36ms CodeModel.GetById 2ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 1ms

/IronPython_Main/Languages/IronPython/Tests/modules/io_related/cStringIO_test.py

#
Python | 361 lines | 334 code | 8 blank | 19 comment | 0 complexity | a58353b18e623c04de6fcc8d497506e3 MD5 | raw file
  1#####################################################################################
  2#
  3#  Copyright (c) Microsoft Corporation. All rights reserved.
  4#
  5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6# copy of the license can be found in the License.html file at the root of this distribution. If
  7# you cannot locate the  Apache License, Version 2.0, please send an email to
  8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9# by the terms of the Apache License, Version 2.0.
 10#
 11# You must not remove this notice, or any other, from this software.
 12#
 13#
 14#####################################################################################
 15
 16##
 17## Test the cStringIO module
 18##
 19
 20from iptest.assert_util import *
 21import cStringIO
 22
 23text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5"
 24
 25# close
 26def call_close(i):
 27    AreEqual(i.closed, False)
 28    i.close()
 29    AreEqual(i.closed, True)
 30    i.close()
 31    AreEqual(i.closed, True)
 32    i.close()
 33    AreEqual(i.closed, True)
 34    
 35
 36def call_isatty(i):
 37    AreEqual(i.isatty(), False)
 38
 39
 40# read
 41def call_read(i):
 42    AreEqual(i.read(), text)
 43    AreEqual(i.read(), "")
 44    AreEqual(i.read(), "")
 45    i.close()
 46    i.close()
 47    AssertError(ValueError, i.read)
 48    
 49   
 50
 51# readline
 52def call_readline(i):
 53    AreEqual(i.readline(), "Line 1\n")
 54    AreEqual(i.readline(), "Line 2\n")
 55    AreEqual(i.readline(), "Line 3\n")
 56    AreEqual(i.readline(), "Line 4\n")
 57    AreEqual(i.readline(), "Line 5")
 58    AreEqual(i.readline(), "")
 59    i.close()
 60    AssertError(ValueError, i.readline)
 61
 62def call_readline_n(i):
 63    AreEqual(i.readline(50), "Line 1\n")
 64    AreEqual(i.readline(0), "")
 65    AreEqual(i.readline(1), "L")
 66    AreEqual(i.readline(9), "ine 2\n")
 67    AreEqual(i.readline(50), "Line 3\n")
 68    AreEqual(i.readline(6), "Line 4")
 69    AreEqual(i.readline(50), "\n")
 70    AreEqual(i.readline(50), "Line 5")
 71    i.close()
 72    AssertError(ValueError, i.readline)    
 73
 74# readlines
 75def call_readlines(i):
 76    AreEqual(i.readlines(), ["Line 1\n", "Line 2\n", "Line 3\n", "Line 4\n", "Line 5"])
 77    AreEqual(i.readlines(), [])
 78    i.close()
 79    AssertError(ValueError, i.readlines)
 80
 81def call_readlines_n(i):
 82    AreEqual(i.readlines(10), ["Line 1\n", "Line 2\n"])
 83    AreEqual(i.readlines(50), ["Line 3\n", "Line 4\n", "Line 5"])
 84    AreEqual(i.readlines(50), [])
 85    i.close()
 86    AssertError(ValueError, i.readlines)
 87    
 88
 89# getvalue
 90def call_getvalue(i):
 91    AreEqual(i.getvalue(), text)
 92    AreEqual(i.read(6), "Line 1")
 93    AreEqual(i.getvalue(True), "Line 1")
 94    AreEqual(i.getvalue(), text)
 95    i.close()
 96    AssertError(ValueError, i.getvalue)
 97    
 98    
 99
100# __iter__, next
101def call_next(i):
102    AreEqual(i.__iter__(), i)
103    AreEqual(i.next(), "Line 1\n")
104    AreEqual(i.next(), "Line 2\n")
105    AreEqual([l for l in i], ["Line 3\n", "Line 4\n", "Line 5"])
106    i.close()
107    AssertError(ValueError, i.readlines)
108    
109    
110
111# read, readline, reset
112def call_reset(i):
113    AreEqual(i.read(0), "")
114    AreEqual(i.read(4), "Line")
115    AreEqual(i.readline(), " 1\n")
116    i.reset()
117    AreEqual(i.read(4), "Line")
118    AreEqual(i.readline(), " 1\n")
119    i.reset()
120    AreEqual(i.read(37),text)
121    i.reset()
122    AreEqual(i.read(38),text)
123    i.close()
124    AssertError(ValueError, i.read, 5)
125    AssertError(ValueError, i.readline)
126    
127    
128    
129    
130
131# seek, tell, read
132def call_seek_tell(i):
133    AreEqual(i.read(4), "Line")
134    AreEqual(i.tell(), 4)
135    i.seek(10)
136    AreEqual(i.tell(), 10)
137    AreEqual(i.read(3), "e 2")
138    i.seek(15, 0)
139    AreEqual(i.tell(), 15)
140    AreEqual(i.read(5), "ine 3")
141    i.seek(3, 1)
142    AreEqual(i.read(4), "ne 4")
143    i.seek(-5, 2)
144    AreEqual(i.tell(), len(text) - 5)
145    AreEqual(i.read(), "ine 5")
146    i.seek(1000)
147    AreEqual(i.tell(), 1000)
148    AreEqual(i.read(), "")
149    i.seek(2000, 0)
150    AreEqual(i.tell(), 2000)
151    AreEqual(i.read(), "")
152    i.seek(400, 1)
153    AreEqual(i.tell(), 2400)
154    AreEqual(i.read(), "")
155    i.seek(100, 2)
156    AreEqual(i.tell(), len(text) + 100)
157    AreEqual(i.read(), "")
158    i.close()
159    AssertError(ValueError, i.tell)
160    AssertError(ValueError, i.seek, 0)
161    AssertError(ValueError, i.seek, 0, 2)
162    
163# truncate
164def call_truncate(i):
165    AreEqual(i.read(6), "Line 1")
166    i.truncate(20)
167    AreEqual(i.tell(), 20)
168    AreEqual(i.getvalue(), "Line 1\nLine 2\nLine 3")
169    i.truncate(30)
170    AreEqual(i.tell(), 20)
171    AreEqual(i.getvalue(), "Line 1\nLine 2\nLine 3")
172    i.reset()
173    AreEqual(i.tell(), 0)
174    AreEqual(i.read(6), "Line 1")
175    i.truncate()
176    AreEqual(i.getvalue(), "Line 1")
177    i.close()
178    AssertError(ValueError, i.truncate)
179    AssertError(ValueError, i.truncate, 10)
180    
181    
182    
183   
184
185# write
186def call_write(o):
187    AreEqual(o.getvalue(), text)
188    o.write("Data")
189    o.write(buffer(' 1'))
190    AssertError(TypeError, o.write, None)
191    AreEqual(o.read(7), "\nLine 2")
192    AreEqual(o.getvalue(), "Data 1\nLine 2\nLine 3\nLine 4\nLine 5")
193    o.close()
194    AssertError(ValueError, o.write, "Hello")
195
196# writelines
197def call_writelines(o):
198    AreEqual(o.getvalue(), text)
199    o.writelines(["Data 1", "Data 2"])
200    AreEqual(o.read(8), "2\nLine 3")
201    AreEqual(o.getvalue(), "Data 1Data 22\nLine 3\nLine 4\nLine 5")
202    AssertError(TypeError, o.writelines, [buffer('foo')])
203    AssertError(TypeError, o.writelines, [None])
204    o.close()
205    AssertError(ValueError, o.writelines, "Hello")
206    AssertError(ValueError, o.writelines, ['foo', buffer('foo')])
207    AssertError(TypeError, o.writelines, [buffer('foo')])
208
209# softspace
210def call_softspace(o):
211    o.write("Hello")
212    o.write("Hi")
213    o.softspace = 1
214    AreEqual(o.softspace, 1)
215    AreEqual(o.getvalue(), "HelloHiLine 2\nLine 3\nLine 4\nLine 5")
216
217# flush
218def call_flush(i):
219    i.flush()
220    AreEqual(i,i)
221
222def init_StringI():
223    return cStringIO.StringIO(text)
224
225def init_StringO():
226    o = cStringIO.StringIO()
227    o.write(text)
228    o.reset()
229    return o
230
231def init_emptyStringI():
232    return cStringIO.StringIO("")
233    
234def test_empty():
235    i = init_emptyStringI()
236    
237    # test closed
238    AreEqual(i.closed,False)
239    i.close()
240    AreEqual(i.closed,True)
241    
242    
243    #test read
244    i = init_emptyStringI()
245    AreEqual(i.read(),"")
246    i.close()
247    AssertError(ValueError, i.read)
248    i.close()
249    AssertError(ValueError, i.read, 2)
250    
251    #test readline
252    i = init_emptyStringI()
253    AreEqual(i.readline(),"")
254    i.close()
255    AssertError(ValueError, i.readline)
256    
257    i = init_emptyStringI()
258    AreEqual(i.readline(0),"")
259    i.close()
260    AssertError(ValueError, i.readline)
261    
262    #test readlines
263    i = init_emptyStringI()
264    AreEqual(i.readlines(),[])
265    
266    i = init_emptyStringI()
267    AreEqual(i.readlines(0),[])
268    
269    #test getvalue
270    i = init_emptyStringI()
271    AreEqual(i.getvalue(),"")
272    AreEqual(i.getvalue(True),"")
273    i.close()
274    AssertError(ValueError, i.getvalue)
275    
276    #test iter
277    i = init_emptyStringI()
278    AreEqual(i.__iter__(), i)
279    
280    #test reset
281    i = init_emptyStringI()
282    AreEqual(i.read(0), "")
283    i.reset()
284    AreEqual(i.read(1), "")
285    i.reset()
286    AreEqual(i.readline(), "")
287    i.close()
288    AssertError(ValueError, i.read, 2)
289    AssertError(ValueError, i.readline)
290    
291    #test seek,tell,read
292    i = init_emptyStringI()
293    AreEqual(i.read(0), "")
294    AreEqual(i.tell(), 0)
295    AreEqual(i.read(1), "")
296    AreEqual(i.tell(), 0)
297    i.seek(2)
298    AreEqual(i.tell(), 2)
299    AreEqual(i.read(),"")
300    i.close()
301    AssertError(ValueError, i.tell)
302    AssertError(ValueError, i.seek, 0)
303    AssertError(ValueError, i.seek, 0, 2)
304    
305    #test truncate
306    i = init_emptyStringI()
307    i.truncate(0)
308    AreEqual(i.tell(), 0)
309    i.truncate(1)
310    AreEqual(i.tell(), 0)
311    i.close()
312    AssertError(ValueError, i.truncate)
313    
314def test_cp8567():
315    for x in ["", "1", "12", "12345", 
316                #u"123", #CodePlex 19220
317                ]:
318        for i in [5, 6, 7, 2**8, 100, 2**16-1, 2**16, 2**16, 2**31-2, 2**31-1]:
319            cio = cStringIO.StringIO(x)
320            cio.truncate(i)
321            AreEqual(cio.tell(), len(x))
322            cio.close()
323    
324    
325    
326def test_i_o():
327    for t in [  call_close,
328                call_isatty,
329                call_read,
330                call_readline,
331                call_readline_n,
332                call_readlines,
333                call_readlines_n,
334                call_getvalue,
335                call_next,
336                call_reset,
337                call_seek_tell,
338                call_truncate,
339                call_flush ]:
340        i = init_StringI()
341        t(i)
342        
343        o= init_StringO()
344        t(o)
345
346def test_o():
347    for t in [  call_write,
348                call_writelines,
349                call_softspace ]:
350        o = init_StringO()
351        t(o)
352
353def test_cp22017():
354    m = cStringIO.StringIO()
355    m.seek(2)
356    m.write("hello!")
357    AreEqual(m.getvalue(), '\x00\x00hello!')
358    m.seek(2)
359    AreEqual(m.getvalue(), '\x00\x00hello!')
360
361run_test(__name__)