1 """ Tests for the linecache module """
6 from test
import test_support
as support
9 FILENAME
= linecache
.__file
__
10 INVALID_NAME
= '!@$)(!@#_1'
12 TESTS
= 'cjkencodings_test inspect_fodder inspect_fodder2 mapping_tests'
14 TEST_PATH
= os
.path
.dirname(support
.__file
__)
15 MODULES
= "linecache abc".split()
16 MODULE_PATH
= os
.path
.dirname(FILENAME
)
34 class LineCacheTests(unittest
.TestCase
):
36 def test_getline(self
):
37 getline
= linecache
.getline
39 # Bad values for line number should return an empty string
40 self
.assertEquals(getline(FILENAME
, 2**15), EMPTY
)
41 self
.assertEquals(getline(FILENAME
, -1), EMPTY
)
43 # Float values currently raise TypeError, should it?
44 self
.assertRaises(TypeError, getline
, FILENAME
, 1.1)
46 # Bad filenames should return an empty string
47 self
.assertEquals(getline(EMPTY
, 1), EMPTY
)
48 self
.assertEquals(getline(INVALID_NAME
, 1), EMPTY
)
50 # Check whether lines correspond to those from file iteration
52 filename
= os
.path
.join(TEST_PATH
, entry
) + '.py'
53 for index
, line
in enumerate(open(filename
)):
54 self
.assertEquals(line
, getline(filename
, index
+ 1))
56 # Check module loading
58 filename
= os
.path
.join(MODULE_PATH
, entry
) + '.py'
59 for index
, line
in enumerate(open(filename
)):
60 self
.assertEquals(line
, getline(filename
, index
+ 1))
62 # Check that bogus data isn't returned (issue #1309567)
63 empty
= linecache
.getlines('a/b/c/__init__.py')
64 self
.assertEquals(empty
, [])
66 def test_clearcache(self
):
69 filename
= os
.path
.join(TEST_PATH
, entry
) + '.py'
70 cached
.append(filename
)
71 linecache
.getline(filename
, 1)
73 # Are all files cached?
74 cached_empty
= [fn
for fn
in cached
if fn
not in linecache
.cache
]
75 self
.assertEquals(cached_empty
, [])
77 # Can we clear the cache?
78 linecache
.clearcache()
79 cached_empty
= [fn
for fn
in cached
if fn
in linecache
.cache
]
80 self
.assertEquals(cached_empty
, [])
82 def test_checkcache(self
):
83 getline
= linecache
.getline
85 # Create a source file and cache its contents
86 source_name
= support
.TESTFN
+ '.py'
87 with
open(source_name
, 'w') as source
:
88 source
.write(SOURCE_1
)
90 getline(source_name
, 1)
92 # Keep a copy of the old contents
94 source
= open(source_name
)
95 for index
, line
in enumerate(source
):
96 self
.assertEquals(line
, getline(source_name
, index
+ 1))
97 source_list
.append(line
)
100 source
= open(source_name
, 'w')
101 source
.write(SOURCE_2
)
104 # Try to update a bogus cache entry
105 linecache
.checkcache('dummy')
107 # Check that the cache matches the old contents
108 for index
, line
in enumerate(source_list
):
109 self
.assertEquals(line
, getline(source_name
, index
+ 1))
111 # Update the cache and check whether it matches the new source file
112 linecache
.checkcache(source_name
)
113 source
= open(source_name
)
114 for index
, line
in enumerate(source
):
115 self
.assertEquals(line
, getline(source_name
, index
+ 1))
116 source_list
.append(line
)
119 support
.unlink(source_name
)
122 support
.run_unittest(LineCacheTests
)
124 if __name__
== "__main__":