/Lib/test/test_linecache.py

http://unladen-swallow.googlecode.com/ · Python · 129 lines · 90 code · 25 blank · 14 comment · 17 complexity · d6f6cd34788876ee71e6c037ccce5345 MD5 · raw file

  1. """ Tests for the linecache module """
  2. import linecache
  3. import unittest
  4. import os.path
  5. from test import test_support as support
  6. FILENAME = linecache.__file__
  7. INVALID_NAME = '!@$)(!@#_1'
  8. EMPTY = ''
  9. TESTS = 'cjkencodings_test inspect_fodder inspect_fodder2 mapping_tests'
  10. TESTS = TESTS.split()
  11. TEST_PATH = os.path.dirname(support.__file__)
  12. MODULES = "linecache unittest".split()
  13. MODULE_PATH = os.path.dirname(FILENAME)
  14. SOURCE_1 = '''
  15. " Docstring "
  16. def function():
  17. return result
  18. '''
  19. SOURCE_2 = '''
  20. def f():
  21. return 1 + 1
  22. a = f()
  23. '''
  24. class LineCacheTests(unittest.TestCase):
  25. def test_getline(self):
  26. getline = linecache.getline
  27. # Bad values for line number should return an empty string
  28. self.assertEquals(getline(FILENAME, 2**15), EMPTY)
  29. self.assertEquals(getline(FILENAME, -1), EMPTY)
  30. # Float values currently raise TypeError, should it?
  31. self.assertRaises(TypeError, getline, FILENAME, 1.1)
  32. # Bad filenames should return an empty string
  33. self.assertEquals(getline(EMPTY, 1), EMPTY)
  34. self.assertEquals(getline(INVALID_NAME, 1), EMPTY)
  35. # Check whether lines correspond to those from file iteration
  36. for entry in TESTS:
  37. filename = os.path.join(TEST_PATH, entry) + '.py'
  38. for index, line in enumerate(open(filename)):
  39. self.assertEquals(line, getline(filename, index + 1))
  40. # Check module loading
  41. for entry in MODULES:
  42. filename = os.path.join(MODULE_PATH, entry) + '.py'
  43. for index, line in enumerate(open(filename)):
  44. self.assertEquals(line, getline(filename, index + 1))
  45. # Check that bogus data isn't returned (issue #1309567)
  46. empty = linecache.getlines('a/b/c/__init__.py')
  47. self.assertEquals(empty, [])
  48. def test_clearcache(self):
  49. cached = []
  50. for entry in TESTS:
  51. filename = os.path.join(TEST_PATH, entry) + '.py'
  52. cached.append(filename)
  53. linecache.getline(filename, 1)
  54. # Are all files cached?
  55. cached_empty = [fn for fn in cached if fn not in linecache.cache]
  56. self.assertEquals(cached_empty, [])
  57. # Can we clear the cache?
  58. linecache.clearcache()
  59. cached_empty = [fn for fn in cached if fn in linecache.cache]
  60. self.assertEquals(cached_empty, [])
  61. def test_checkcache(self):
  62. getline = linecache.getline
  63. try:
  64. # Create a source file and cache its contents
  65. source_name = os.path.join(TEST_PATH, 'linecache_test.py')
  66. source = open(source_name, 'w')
  67. source.write(SOURCE_1)
  68. source.close()
  69. getline(source_name, 1)
  70. # Keep a copy of the old contents
  71. source_list = []
  72. source = open(source_name)
  73. for index, line in enumerate(source):
  74. self.assertEquals(line, getline(source_name, index + 1))
  75. source_list.append(line)
  76. source.close()
  77. source = open(source_name, 'w')
  78. source.write(SOURCE_2)
  79. source.close()
  80. # Try to update a bogus cache entry
  81. linecache.checkcache('dummy')
  82. # Check that the cache matches the old contents
  83. for index, line in enumerate(source_list):
  84. self.assertEquals(line, getline(source_name, index + 1))
  85. # Update the cache and check whether it matches the new source file
  86. linecache.checkcache(source_name)
  87. source = open(source_name)
  88. for index, line in enumerate(source):
  89. self.assertEquals(line, getline(source_name, index + 1))
  90. source_list.append(line)
  91. source.close()
  92. finally:
  93. try:
  94. source.close()
  95. finally:
  96. support.unlink(source_name)
  97. def test_main():
  98. support.run_unittest(LineCacheTests)
  99. if __name__ == "__main__":
  100. test_main()