File: test_file_newlines.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (373 lines) | stat: -rw-r--r-- 9,997 bytes parent folder | download | duplicates (8)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""Test handling of newlines via file's read and readline

Made for Jython.
"""
import os
import sys
import tempfile
import test.test_support as test_support
import unittest

assert not os.linesep == '\r', ('os.linesep of  %r is not supported' %
                                os.linesep)

LF = os.linesep == '\n'
CRLF = os.linesep == '\r\n'

CRLF_TEST = 'CR\rLF\nCRLF\r\nEOF'

if sys.platform.startswith('java'):
    from org.python.core.io import TextIOBase
    READAHEAD_SIZE = TextIOBase.CHUNK_SIZE
else:
    READAHEAD_SIZE = 300

class BaseTestCase(unittest.TestCase):

    data = CRLF_TEST
    write_mode = 'wb'
    mode = 'r'
    bufsize = -1

    def setUp(self):
        self.filename = tempfile.mktemp()
        self.write_fp = open(self.filename, self.write_mode, self.bufsize)
        self.write_fp.write(self.data)
        self.write_fp.flush()
        self.fp = open(self.filename, self.mode, self.bufsize)

    def tearDown(self):
        if self.write_fp:
            self.write_fp.close()
        if self.fp:
            self.fp.close()
        os.remove(self.filename)


class ReadBinaryNewlinesTestCase(BaseTestCase):

    mode = 'rb'

    def test_binary_read(self):
        read(self.fp, CRLF_TEST)

        self.fp.seek(0)
        read(self.fp, CRLF_TEST, len(CRLF_TEST))

        self.fp.seek(0)
        read(self.fp, 'CR\r', 3)
        read(self.fp, 'LF\n', 3)
        read(self.fp, 'CRLF\r\n', 6)
        read(self.fp, 'EOF', 3)

    def test_binary_readline(self):
        readline(self.fp, 'CR\rLF\n')
        readline(self.fp, 'CRLF\r\n')
        readline(self.fp, 'EOF')

        self.fp.seek(0)
        readline(self.fp, 'CR\rLF\n', 6)
        readline(self.fp, 'CRLF\r\n', 6)
        readline(self.fp, 'EOF', 3)


if LF:
    class ReadTextNewlinesTestCase(ReadBinaryNewlinesTestCase):

        mode = 'r'

else:
    class ReadTextNewlinesTestCase(BaseTestCase):

        def test_text_read(self):
            read(self.fp, 'CR\rLF\nCRLF\nEOF')

            self.fp.seek(0)
            read(self.fp, 'CR\rLF\nCRLF\nEOF', len('CR\rLF\nCRLF\nEOF'))

            self.fp.seek(0)
            read(self.fp, 'CR\r', 3)
            read(self.fp, 'LF\n', 3)
            read(self.fp, 'CRLF\n', 5)
            read(self.fp, 'EOF', 3)

        def test_text_readline(self):
            readline(self.fp, 'CR\rLF\n')
            readline(self.fp, 'CRLF\n')
            readline(self.fp, 'EOF')

            self.fp.seek(0)
            readline(self.fp, 'CR\rLF\n', 6)
            readline(self.fp, 'CRLF\n', 5)
            readline(self.fp, 'EOF', 3)


class ReadTextBasicBoundaryTestCase(BaseTestCase):

    data = 'CR\r'
    read_data = 'CR\r'

    def test_read_basic_boundary(self):
        read(self.fp, self.read_data)
        self.fp.seek(0)
        read(self.fp, self.read_data, 3)

    def test_readline_basic_boundary(self):
        readline(self.fp, self.read_data)
        self.fp.seek(0)
        readline(self.fp, self.read_data, 3)


class ReadUniversalBasicBoundaryTestCase(ReadTextBasicBoundaryTestCase):

    mode = 'U'
    read_data = 'CR\n'


class BinaryReadaheadBoundaryTestCase(BaseTestCase):

    mode = 'rb'
    data = 'foo\n' + ('x' * READAHEAD_SIZE)

    def test_read_boundary(self):
        readline(self.fp, 'foo\n')
        read(self.fp, 'x' * READAHEAD_SIZE, READAHEAD_SIZE)

    def test_readline_boundary(self):
        readline(self.fp, 'foo\n')
        readline(self.fp, 'x' * READAHEAD_SIZE, READAHEAD_SIZE)


class TextReadaheadBoundaryTestCase(BinaryReadaheadBoundaryTestCase):

    mode = 'r'


class UniversalReadaheadBoundaryTestCase(BinaryReadaheadBoundaryTestCase):

    mode = 'U'


class TextReadaheadBoundary2TestCase(BaseTestCase):
    """For CRLF platforms only"""

    data = ('x' * (READAHEAD_SIZE + 1)) + '\r\n'

    def test_read_boundary2(self):
        read(self.fp, 'x' * (READAHEAD_SIZE + 1), READAHEAD_SIZE + 1)
        read(self.fp, '\n')


class UniversalReadaheadBoundary2TestCase(TextReadaheadBoundary2TestCase):

    mode = 'U'


class TextReadaheadBoundary3TestCase(BaseTestCase):
    """For CRLF platforms only"""

    mode = 'r'
    data = ('x' * (READAHEAD_SIZE - 1)) + '\r\n'

    def test_read_boundary3(self):
        size = READAHEAD_SIZE - 1
        read(self.fp, 'x' * size, size)
        read(self.fp, '\n')
        self.fp.seek(0)
        read(self.fp, ('x' * size) + '\n', READAHEAD_SIZE)

    def test_readline_boundary3(self):
        size = READAHEAD_SIZE - 1
        readline(self.fp, 'x' * size, size)
        readline(self.fp, '\n')

    def test_read_boundary3_with_extra(self):
        self.write_fp.seek(0, 2)
        self.write_fp.write('foo')
        self.write_fp.flush()
        self.write_fp.seek(0)

        size = READAHEAD_SIZE - 1
        read(self.fp, ('x' * size) + '\nfoo', READAHEAD_SIZE + 3)


class UniversalReadaheadBoundary3TestCase(TextReadaheadBoundary3TestCase):

    mode = 'U'


class TextReadaheadBoundary4TestCase(BaseTestCase):
    """For CRLF platforms only"""

    mode = 'r'
    data = ('x' * (READAHEAD_SIZE - 2)) + '\n\r'

    def test_read_boundary4(self):
        readline(self.fp, 'x' * (READAHEAD_SIZE - 2) + '\n')

        self.write_fp.write('\n')
        self.write_fp.flush()

        read(self.fp, '\n')

    def test_readline_boundary4(self):
        readline(self.fp, 'x' * (READAHEAD_SIZE - 2) + '\n')

        self.write_fp.write('\n')
        self.write_fp.flush()

        readline(self.fp, '\n')


class UniversalReadaheadBoundary4TestCase(TextReadaheadBoundary4TestCase):

    mode = 'U'
    data = ('x' * (READAHEAD_SIZE - 2)) + '\n\r'


class TextReadaheadBoundary5TestCase(BaseTestCase):
    """For CRLF platforms only"""

    mode = 'r'
    data = 'foobar\n' + ('x' * (READAHEAD_SIZE + 1))

    def test_boundary5(self):
        readline(self.fp, 'foobar\n')
        read(self.fp, 'x' * (READAHEAD_SIZE + 1), READAHEAD_SIZE + 1)


class UniversalReadaheadBoundary5TestCase(TextReadaheadBoundary5TestCase):

    mode = 'U'


class TextCRAtReadheadBoundaryTestCase(BaseTestCase):
    """For CRLF platforms only"""

    data = ('x' * (READAHEAD_SIZE - 1)) + '\rfoo'
    read_data = data

    def test_readline_cr_at_boundary(self):
        readline(self.fp, self.read_data, len(self.read_data))
        self.fp.seek(0)
        readline(self.fp, self.read_data)


class TextCRAtReadheadBoundary2TestCase(TextCRAtReadheadBoundaryTestCase):
    """For CRLF platforms only"""

    data = ('x' * (READAHEAD_SIZE - 1)) + '\r' + ('x' * 300)
    read_data = data


class UniversalCRAtReadaheadBoundaryTestCase(BaseTestCase):

    mode = 'U'
    bufsize = 0
    data = ('-' * 1023) + '\r\n' + ('-' * 10233)

    def test_read_cr_at_boundary(self):
        # Used to raise a BufferOverflowException w/ bufsize of 0
        read(self.fp, ('-' * 1023) + '\n', 1024)


class WriteTextNewlinesTestCase(BaseTestCase):

    write_mode = 'w'
    mode = 'rb'

    def test_text_written(self):
        if LF:
            readline(self.fp, 'CR\rLF\n')
            readline(self.fp, 'CRLF\r\n')
        elif CRLF:
            readline(self.fp, 'CR\rLF\r\n')
            readline(self.fp, 'CRLF\r\r\n')
        readline(self.fp, 'EOF')


class ReadUniversalNewlinesTestCase(BaseTestCase):

    mode = 'rU'

    def test_read(self):
        read(self.fp, 'CR\nLF\nCRLF\nEOF')

        self.fp.seek(0)
        read(self.fp, 'CR\nLF\nCRLF\nEOF', 14)

    def test_readline(self):
        readline(self.fp, 'CR\n')
        assert self.fp.newlines == None, repr(self.fp.newlines)
        readline(self.fp, 'LF\n')
        assert self.fp.newlines == ('\r', '\n'), repr(self.fp.newlines)
        readline(self.fp, 'CRLF\n')
        assert self.fp.newlines == ('\r', '\n'), repr(self.fp.newlines)
        readline(self.fp, 'EOF')
        assert self.fp.newlines == ('\r', '\n', '\r\n'), repr(self.fp.newlines)

        self.fp.seek(0)
        readline(self.fp, 'CR\n', 3)
        readline(self.fp, 'LF\n', 3)
        readline(self.fp, 'CRLF\n', 5)
        readline(self.fp, 'EOF', 3)

    def test_seek(self):
        # Ensure seek doesn't confuse CRLF newline identification
        self.fp.seek(6)
        readline(self.fp, 'CRLF\n')
        assert self.fp.newlines == None
        self.fp.seek(5)
        readline(self.fp, '\n')
        assert self.fp.newlines == '\n'


class WriteUniversalNewlinesTestCase(unittest.TestCase):

    def test_fails(self):
        try:
            open(tempfile.mktemp(), 'wU')
        except ValueError:
            pass
        else:
            raise AssertionError("file mode 'wU' did not raise a "
                                 "ValueError")


def read(fp, data, size=-1):
    line = fp.read(size)
    assert line == data, 'read: %r expected: %r' % (line, data)


def readline(fp, data, size=-1):
    line = fp.readline(size)
    assert line == data, 'readline: %r expected: %r' % (line, data)


def test_main():
    tests = [ReadBinaryNewlinesTestCase,
             ReadTextNewlinesTestCase,
             ReadTextBasicBoundaryTestCase,
             ReadUniversalBasicBoundaryTestCase,
             BinaryReadaheadBoundaryTestCase,
             TextReadaheadBoundaryTestCase,
             UniversalReadaheadBoundaryTestCase,
             UniversalReadaheadBoundary2TestCase,
             UniversalReadaheadBoundary3TestCase,
             UniversalReadaheadBoundary4TestCase,
             UniversalReadaheadBoundary5TestCase,
             UniversalCRAtReadaheadBoundaryTestCase,
             WriteTextNewlinesTestCase,
             ReadUniversalNewlinesTestCase,
             WriteUniversalNewlinesTestCase]
    if CRLF:
        tests.extend([TextReadaheadBoundary2TestCase,
                      TextReadaheadBoundary3TestCase,
                      TextReadaheadBoundary4TestCase,
                      TextReadaheadBoundary5TestCase,
                      TextCRAtReadheadBoundaryTestCase,
                      TextCRAtReadheadBoundary2TestCase])
    test_support.run_unittest(*tests)

if __name__ == '__main__':
    test_main()