0001 ''' 0002 Tests for fileinput module. 0003 Nick Mathewson 0004 ''' 0005 0006 from test.test_support import verify, verbose, TESTFN 0007 import sys, os, re 0008 from StringIO import StringIO 0009 from fileinput import FileInput 0010 0011 # The fileinput module has 2 interfaces: the FileInput class which does 0012 # all the work, and a few functions (input, etc.) that use a global _state 0013 # variable. We only test the FileInput class, since the other functions 0014 # only provide a thin facade over FileInput. 0015 0016 # Write lines (a list of lines) to temp file number i, and return the 0017 # temp file's name. 0018 def writeTmp(i, lines): 0019 name = TESTFN + str(i) 0020 f = open(name, 'w') 0021 f.writelines(lines) 0022 f.close() 0023 return name 0024 0025 pat = re.compile(r'LINE (\d+) OF FILE (\d+)') 0026 0027 def remove_tempfiles(*names): 0028 for name in names: 0029 try: 0030 os.unlink(name) 0031 except: 0032 pass 0033 0034 def runTests(t1, t2, t3, t4, bs=0, round=0): 0035 start = 1 + round*6 0036 if verbose: 0037 print '%s. Simple iteration (bs=%s)' % (start+0, bs) 0038 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) 0039 lines = list(fi) 0040 fi.close() 0041 verify(len(lines) == 31) 0042 verify(lines[4] == 'Line 5 of file 1\n') 0043 verify(lines[30] == 'Line 1 of file 4\n') 0044 verify(fi.lineno() == 31) 0045 verify(fi.filename() == t4) 0046 0047 if verbose: 0048 print '%s. Status variables (bs=%s)' % (start+1, bs) 0049 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) 0050 s = "x" 0051 while s and s != 'Line 6 of file 2\n': 0052 s = fi.readline() 0053 verify(fi.filename() == t2) 0054 verify(fi.lineno() == 21) 0055 verify(fi.filelineno() == 6) 0056 verify(not fi.isfirstline()) 0057 verify(not fi.isstdin()) 0058 0059 if verbose: 0060 print '%s. Nextfile (bs=%s)' % (start+2, bs) 0061 fi.nextfile() 0062 verify(fi.readline() == 'Line 1 of file 3\n') 0063 verify(fi.lineno() == 22) 0064 fi.close() 0065 0066 if verbose: 0067 print '%s. Stdin (bs=%s)' % (start+3, bs) 0068 fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs) 0069 savestdin = sys.stdin 0070 try: 0071 sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n") 0072 lines = list(fi) 0073 verify(len(lines) == 33) 0074 verify(lines[32] == 'Line 2 of stdin\n') 0075 verify(fi.filename() == '<stdin>') 0076 fi.nextfile() 0077 finally: 0078 sys.stdin = savestdin 0079 0080 if verbose: 0081 print '%s. Boundary conditions (bs=%s)' % (start+4, bs) 0082 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) 0083 verify(fi.lineno() == 0) 0084 verify(fi.filename() == None) 0085 fi.nextfile() 0086 verify(fi.lineno() == 0) 0087 verify(fi.filename() == None) 0088 0089 if verbose: 0090 print '%s. Inplace (bs=%s)' % (start+5, bs) 0091 savestdout = sys.stdout 0092 try: 0093 fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs) 0094 for line in fi: 0095 line = line[:-1].upper() 0096 print line 0097 fi.close() 0098 finally: 0099 sys.stdout = savestdout 0100 0101 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) 0102 for line in fi: 0103 verify(line[-1] == '\n') 0104 m = pat.match(line[:-1]) 0105 verify(m != None) 0106 verify(int(m.group(1)) == fi.filelineno()) 0107 fi.close() 0108 0109 0110 def writeFiles(): 0111 global t1, t2, t3, t4 0112 t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)]) 0113 t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)]) 0114 t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)]) 0115 t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)]) 0116 0117 # First, run the tests with default and teeny buffer size. 0118 for round, bs in (0, 0), (1, 30): 0119 try: 0120 writeFiles() 0121 runTests(t1, t2, t3, t4, bs, round) 0122 finally: 0123 remove_tempfiles(t1, t2, t3, t4) 0124 0125 # Next, check for proper behavior with 0-byte files. 0126 if verbose: 0127 print "13. 0-byte files" 0128 try: 0129 t1 = writeTmp(1, [""]) 0130 t2 = writeTmp(2, [""]) 0131 t3 = writeTmp(3, ["The only line there is.\n"]) 0132 t4 = writeTmp(4, [""]) 0133 fi = FileInput(files=(t1, t2, t3, t4)) 0134 line = fi.readline() 0135 verify(line == 'The only line there is.\n') 0136 verify(fi.lineno() == 1) 0137 verify(fi.filelineno() == 1) 0138 verify(fi.filename() == t3) 0139 line = fi.readline() 0140 verify(not line) 0141 verify(fi.lineno() == 1) 0142 verify(fi.filelineno() == 0) 0143 verify(fi.filename() == t4) 0144 fi.close() 0145 finally: 0146 remove_tempfiles(t1, t2, t3, t4) 0147 0148 if verbose: 0149 print "14. Files that don't end with newline" 0150 try: 0151 t1 = writeTmp(1, ["A\nB\nC"]) 0152 t2 = writeTmp(2, ["D\nE\nF"]) 0153 fi = FileInput(files=(t1, t2)) 0154 lines = list(fi) 0155 verify(lines == ["A\n", "B\n", "C", "D\n", "E\n", "F"]) 0156 verify(fi.filelineno() == 3) 0157 verify(fi.lineno() == 6) 0158 finally: 0159 remove_tempfiles(t1, t2) 0160
Generated by PyXR 0.9.4