2 Tests for fileinput module.
6 from test_support
import verify
, verbose
, TESTFN
8 from StringIO
import StringIO
9 from fileinput
import FileInput
11 # The fileinput module has 2 interfaces: the FileInput class which does
12 # all the work, and a few functions (input, etc.) that use a global _state
13 # variable. We only test the FileInput class, since the other functions
14 # only provide a thin facade over FileInput.
16 # Write lines (a list of lines) to temp file number i, and return the
18 def writeTmp(i
, lines
):
19 name
= TESTFN
+ str(i
)
25 pat
= re
.compile(r
'LINE (\d+) OF FILE (\d+)')
27 def remove_tempfiles(*names
):
34 def runTests(t1
, t2
, t3
, t4
, bs
=0, round=0):
37 print '%s. Simple iteration (bs=%s)' % (start
+0, bs
)
38 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
41 verify(len(lines
) == 31)
42 verify(lines
[4] == 'Line 5 of file 1\n')
43 verify(lines
[30] == 'Line 1 of file 4\n')
44 verify(fi
.lineno() == 31)
45 verify(fi
.filename() == t4
)
48 print '%s. Status variables (bs=%s)' % (start
+1, bs
)
49 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
51 while s
and s
!= 'Line 6 of file 2\n':
53 verify(fi
.filename() == t2
)
54 verify(fi
.lineno() == 21)
55 verify(fi
.filelineno() == 6)
56 verify(not fi
.isfirstline())
57 verify(not fi
.isstdin())
60 print '%s. Nextfile (bs=%s)' % (start
+2, bs
)
62 verify(fi
.readline() == 'Line 1 of file 3\n')
63 verify(fi
.lineno() == 22)
67 print '%s. Stdin (bs=%s)' % (start
+3, bs
)
68 fi
= FileInput(files
=(t1
, t2
, t3
, t4
, '-'), bufsize
=bs
)
71 sys
.stdin
= StringIO("Line 1 of stdin\nLine 2 of stdin\n")
73 verify(len(lines
) == 33)
74 verify(lines
[32] == 'Line 2 of stdin\n')
75 verify(fi
.filename() == '<stdin>')
81 print '%s. Boundary conditions (bs=%s)' % (start
+4, bs
)
82 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
83 verify(fi
.lineno() == 0)
84 verify(fi
.filename() == None)
86 verify(fi
.lineno() == 0)
87 verify(fi
.filename() == None)
90 print '%s. Inplace (bs=%s)' % (start
+5, bs
)
91 savestdout
= sys
.stdout
93 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), inplace
=1, bufsize
=bs
)
95 line
= line
[:-1].upper()
99 sys
.stdout
= savestdout
101 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
103 verify(line
[-1] == '\n')
104 m
= pat
.match(line
[:-1])
106 verify(int(m
.group(1)) == fi
.filelineno())
111 global t1
, t2
, t3
, t4
112 t1
= writeTmp(1, ["Line %s of file 1\n" % (i
+1) for i
in range(15)])
113 t2
= writeTmp(2, ["Line %s of file 2\n" % (i
+1) for i
in range(10)])
114 t3
= writeTmp(3, ["Line %s of file 3\n" % (i
+1) for i
in range(5)])
115 t4
= writeTmp(4, ["Line %s of file 4\n" % (i
+1) for i
in range(1)])
117 # First, run the tests with default and teeny buffer size.
118 for round, bs
in (0, 0), (1, 30):
121 runTests(t1
, t2
, t3
, t4
, bs
, round)
123 remove_tempfiles(t1
, t2
, t3
, t4
)
125 # Next, check for proper behavior with 0-byte files.
127 print "13. 0-byte files"
129 t1
= writeTmp(1, [""])
130 t2
= writeTmp(2, [""])
131 t3
= writeTmp(3, ["The only line there is.\n"])
132 t4
= writeTmp(4, [""])
133 fi
= FileInput(files
=(t1
, t2
, t3
, t4
))
135 verify(line
== 'The only line there is.\n')
136 verify(fi
.lineno() == 1)
137 verify(fi
.filelineno() == 1)
138 verify(fi
.filename() == t3
)
141 verify(fi
.lineno() == 1)
142 verify(fi
.filelineno() == 0)
143 verify(fi
.filename() == t4
)
146 remove_tempfiles(t1
, t2
, t3
, t4
)
149 print "14. Files that don't end with newline"
151 t1
= writeTmp(1, ["A\nB\nC"])
152 t2
= writeTmp(2, ["D\nE\nF"])
153 fi
= FileInput(files
=(t1
, t2
))
155 verify(lines
== ["A\n", "B\n", "C", "D\n", "E\n", "F"])
156 verify(fi
.filelineno() == 3)
157 verify(fi
.lineno() == 6)
159 remove_tempfiles(t1
, t2
)