You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.5 KiB

  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. """
  4. Tests autojump.
  5. """
  6. from __future__ import division
  7. import autojump
  8. import contextlib
  9. import random
  10. import os
  11. import shutil
  12. import sys
  13. import tempfile
  14. import unittest
  15. @contextlib.contextmanager
  16. def no_stderr():
  17. savestderr = sys.stderr
  18. class DevNull(object):
  19. def write(self, _): pass
  20. sys.stderr = DevNull()
  21. yield
  22. sys.stderr = savestderr
  23. # test suite
  24. class TestAutojump(unittest.TestCase):
  25. def setUp(self):
  26. autojump.CONFIG_DIR = tempfile.mkdtemp()
  27. autojump.TESTING = True
  28. self.fd, self.fname = tempfile.mkstemp()
  29. self.db = autojump.Database(self.fname)
  30. random.seed()
  31. def tearDown(self):
  32. os.remove(self.fname)
  33. if os.path.isfile(self.fname + ".bak"):
  34. os.remove(self.fname + ".bak")
  35. if (os.path.exists(autojump.CONFIG_DIR) and
  36. ('tmp' in autojump.CONFIG_DIR or 'temp' in autojump.CONFIG_DIR)):
  37. shutil.rmtree(autojump.CONFIG_DIR)
  38. def test_config(self):
  39. self.assertEqual(autojump.COMPLETION_SEPARATOR, '__')
  40. def test_db_add(self):
  41. self.db.add('/1', 100)
  42. self.assertEqual(self.db.get_weight('/1'), 100)
  43. self.db.add('/2', 10)
  44. self.assertEqual(self.db.get_weight('/2'), 10)
  45. self.db.add('/2', 10)
  46. self.assertEqual(self.db.get_weight('/2'), 14.142135623730951)
  47. def test_db_get_weight(self):
  48. self.assertEqual(self.db.get_weight('/'), 0)
  49. def test_db_decay(self):
  50. self.db.add('/1', 10)
  51. self.db.decay()
  52. self.assertTrue(self.db.get_weight('/1') < 10)
  53. def test_db_load_existing(self):
  54. self.db = autojump.Database('tests/database.txt')
  55. self.assertTrue(len(self.db) > 0)
  56. def test_db_load_empty(self):
  57. # setup
  58. _, fname = tempfile.mkstemp()
  59. db = autojump.Database(fname)
  60. try:
  61. # test
  62. self.assertEquals(len(self.db), 0)
  63. finally:
  64. # teardown
  65. os.remove(fname)
  66. def test_db_load_backup(self):
  67. # setup
  68. fname = '/tmp/autojump_test_db_load_backup_' + str(random.randint(0,32678))
  69. db = autojump.Database(fname)
  70. db.add('/1')
  71. os.rename(fname, fname + '.bak')
  72. try:
  73. # test
  74. with no_stderr():
  75. db = autojump.Database(fname)
  76. self.assertTrue(len(db.data) > 0)
  77. self.assertTrue(os.path.isfile(fname))
  78. finally:
  79. # teardown
  80. os.remove(fname)
  81. os.remove(fname + '.bak')
  82. def test_db_purge(self):
  83. self.db.add('/1')
  84. self.db.purge()
  85. self.assertEquals(len(self.db), 0)
  86. def test_db_save(self):
  87. # setup
  88. fname = '/tmp/autojump_test_db_save_' + str(random.randint(0,32678)) + '.txt'
  89. db = autojump.Database(fname)
  90. try:
  91. # test
  92. db.save()
  93. self.assertTrue(os.path.isfile(fname))
  94. finally:
  95. # teardown
  96. os.remove(fname)
  97. os.remove(fname + '.bak')
  98. def test_db_trim(self):
  99. self.db.add('/1')
  100. self.db.add('/2')
  101. self.db.add('/3')
  102. self.db.add('/4')
  103. self.db.add('/5')
  104. self.db.add('/6')
  105. self.db.add('/7')
  106. self.db.add('/8')
  107. self.db.add('/9')
  108. self.db.add('/10')
  109. self.assertEquals(len(self.db), 10)
  110. self.db.trim()
  111. self.assertEquals(len(self.db), 9)
  112. def test_db_decode(self):
  113. #FIXME
  114. self.assertEquals(autojump.decode('foo'), 'foo')
  115. def test_db_unico(self):
  116. #FIXME
  117. self.assertEquals(autojump.unico('foo'), u'foo')
  118. def test_match_normal(self):
  119. max_matches = 1
  120. self.db.add('/foo', 10)
  121. self.db.add('/foo/bar', 20)
  122. patterns = [u'']
  123. results = autojump.find_matches(self.db, patterns, max_matches)
  124. self.assertEquals(results[0], '/foo/bar')
  125. patterns = [u'random']
  126. results = autojump.find_matches(self.db, patterns, max_matches)
  127. self.assertTrue(len(results) == 0)
  128. patterns = [u'fo']
  129. results = autojump.find_matches(self.db, patterns, max_matches)
  130. self.assertEquals(results[0], '/foo')
  131. self.db.add('/foo/bat', 15)
  132. patterns = [u'ba']
  133. results = autojump.find_matches(self.db, patterns, max_matches)
  134. self.assertEquals(results[0], '/foo/bar')
  135. self.db.add('/code/inbox', 5)
  136. self.db.add('/home/user/inbox', 10)
  137. patterns = [u'inbox']
  138. results = autojump.find_matches(self.db, patterns, max_matches)
  139. self.assertEquals(results[0], '/home/user/inbox')
  140. patterns = [u'co', u'in']
  141. results = autojump.find_matches(self.db, patterns, max_matches)
  142. self.assertEquals(results[0], '/code/inbox')
  143. def test_match_completion(self):
  144. max_matches = 9
  145. ignore_case = True
  146. self.db.add('/1')
  147. self.db.add('/2')
  148. self.db.add('/3')
  149. self.db.add('/4')
  150. self.db.add('/5', 20)
  151. self.db.add('/6', 15)
  152. self.db.add('/7')
  153. self.db.add('/8')
  154. self.db.add('/9')
  155. patterns = [u'']
  156. results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
  157. self.assertEquals(results, ['/5', '/6', '/9', '/8', '/7', '/4', '/3', '/2', '/1'])
  158. def test_match_case_insensitive(self):
  159. max_matches = 1
  160. ignore_case = True
  161. self.db.add('/FOO', 20)
  162. self.db.add('/foo', 10)
  163. patterns = [u'fo']
  164. results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
  165. self.assertEquals(results[0], '/FOO')
  166. def test_match_fuzzy(self):
  167. max_matches = 1
  168. ignore_case = True
  169. fuzzy_search = True
  170. self.db.add('/foo', 10)
  171. self.db.add('/foo/bar', 20)
  172. self.db.add('/abcdefg', 10)
  173. patterns = [u'random']
  174. results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
  175. self.assertTrue(len(results) == 0)
  176. patterns = [u'abcdefg']
  177. results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
  178. self.assertEquals(results[0], '/abcdefg')
  179. patterns = [u'abcefg']
  180. results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
  181. self.assertEquals(results[0], '/abcdefg')
  182. patterns = [u'bacef']
  183. results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
  184. self.assertEquals(results[0], '/abcdefg')
  185. if __name__ == '__main__':
  186. unittest.main()