Back to index

python3.2  3.2.2
Classes | Functions
test.test_fileinput Namespace Reference

Classes

class  BufferSizesTests
class  FileInputTests

Functions

def writeTmp
def remove_tempfiles
def test_fileno
 def test_unicode_filenames(self):

XXX A unicode string is always returned by writeTmp.


def test_opening_mode
def test_file_opening_hook
def test_context_manager
 try: t1 = writeTmp(1, ["A\nB"], mode="wb") fi = FileInput(files=t1, openhook=hook_encoded("rot13")) lines = list(fi) self.assertEqual(lines, ["N\n", "O"]) finally: remove_tempfiles(t1)
def test_close_on_exception
def test_main

Detailed Description

Tests for fileinput module.
Nick Mathewson

Function Documentation

Definition at line 28 of file test_fileinput.py.

00028 
00029 def remove_tempfiles(*names):
00030     for name in names:
00031         if name:
00032             safe_unlink(name)

Here is the caller graph for this function:

Definition at line 247 of file test_fileinput.py.

00247 
00248     def test_close_on_exception(self):
00249         try:
00250             t1 = writeTmp(1, [""])
00251             with FileInput(files=t1) as fi:
00252                 raise IOError
00253         except IOError:
00254             self.assertEqual(fi._files, ())
00255         finally:
00256             remove_tempfiles(t1)
00257 

Here is the call graph for this function:

try: t1 = writeTmp(1, ["A\nB"], mode="wb") fi = FileInput(files=t1, openhook=hook_encoded("rot13")) lines = list(fi) self.assertEqual(lines, ["N\n", "O"]) finally: remove_tempfiles(t1)

Definition at line 234 of file test_fileinput.py.

00234 
00235     def test_context_manager(self):
00236         try:
00237             t1 = writeTmp(1, ["A\nB\nC"])
00238             t2 = writeTmp(2, ["D\nE\nF"])
00239             with FileInput(files=(t1, t2)) as fi:
00240                 lines = list(fi)
00241             self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
00242             self.assertEqual(fi.filelineno(), 3)
00243             self.assertEqual(fi.lineno(), 6)
00244             self.assertEqual(fi._files, ())
00245         finally:
00246             remove_tempfiles(t1, t2)

Here is the call graph for this function:

Definition at line 209 of file test_fileinput.py.

00209 
00210     def test_file_opening_hook(self):
00211         try:
00212             # cannot use openhook and inplace mode
00213             fi = FileInput(inplace=1, openhook=lambda f, m: None)
00214             self.fail("FileInput should raise if both inplace "
00215                              "and openhook arguments are given")
00216         except ValueError:
00217             pass
00218         try:
00219             fi = FileInput(openhook=1)
00220             self.fail("FileInput should check openhook for being callable")
00221         except ValueError:
00222             pass
00223         # XXX The rot13 codec was removed.
00224         #     So this test needs to be changed to use something else.
00225         #     (Or perhaps the API needs to change so we can just pass
        #     an encoding rather than using a hook?)

def test_unicode_filenames(self):

XXX A unicode string is always returned by writeTmp.

So is this needed?

try: t1 = writeTmp(1, ["A\nB"]) encoding = sys.getfilesystemencoding() if encoding is None: encoding = 'ascii' fi = FileInput(files=str(t1, encoding)) lines = list(fi) self.assertEqual(lines, ["A\n", "B"]) finally: remove_tempfiles(t1)

Definition at line 176 of file test_fileinput.py.

00176 
00177     def test_fileno(self):
00178         t1 = t2 = None
00179         try:
00180             t1 = writeTmp(1, ["A\nB"])
00181             t2 = writeTmp(2, ["C\nD"])
00182             fi = FileInput(files=(t1, t2))
00183             self.assertEqual(fi.fileno(), -1)
00184             line =next( fi)
00185             self.assertNotEqual(fi.fileno(), -1)
00186             fi.nextfile()
00187             self.assertEqual(fi.fileno(), -1)
00188             line = list(fi)
00189             self.assertEqual(fi.fileno(), -1)
00190         finally:
00191             remove_tempfiles(t1, t2)

Here is the call graph for this function:

Definition at line 258 of file test_fileinput.py.

00258 
00259 def test_main():
00260     run_unittest(BufferSizesTests, FileInputTests)

Here is the call graph for this function:

Definition at line 192 of file test_fileinput.py.

00192 
00193     def test_opening_mode(self):
00194         try:
00195             # invalid mode, should raise ValueError
00196             fi = FileInput(mode="w")
00197             self.fail("FileInput should reject invalid mode argument")
00198         except ValueError:
00199             pass
00200         t1 = None
00201         try:
00202             # try opening in universal newline mode
00203             t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb")
00204             fi = FileInput(files=t1, mode="U")
00205             lines = list(fi)
00206             self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
00207         finally:
00208             remove_tempfiles(t1)

Here is the call graph for this function:

def test.test_fileinput.writeTmp (   i,
  lines,
  mode = 'w' 
)

Definition at line 20 of file test_fileinput.py.

00020 
00021 def writeTmp(i, lines, mode='w'):  # opening in text mode is the default
00022     name = TESTFN + str(i)
00023     f = open(name, mode)
00024     for line in lines:
00025         f.write(line)
00026     f.close()
00027     return name

Here is the caller graph for this function: