Permalink
Browse files

add unit tests, run using: make test

  • Loading branch information...
1 parent 13e385a commit f25d7ba99647fdaa99cf1e3624d516a0d85307d6 @wting committed May 13, 2012
Showing with 167 additions and 1 deletion.
  1. +4 −1 Makefile
  2. +1 −0 tests/autojump.py
  3. +1 −0 tests/database.txt
  4. +161 −0 tests/runtests.py
View
@@ -1,7 +1,7 @@
VERSION = v20
TAGNAME = release-$(VERSION)
-.PHONY: docs install uninstall
+.PHONY: docs install uninstall test
install:
install.sh
@@ -28,3 +28,6 @@ release:
# Create tagged archive
git archive --format=tar --prefix autojump_$(VERSION)/ $(TAGNAME) | gzip > autojump_$(VERSION).tar.gz
+
+test:
+ python tests/runtests.py
View
View
@@ -0,0 +1 @@
+91.5647698216 /home/user/code/autojump
View
@@ -0,0 +1,161 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+"""
+ Tests autojump.
+"""
+
+from __future__ import division
+import autojump
+import tempfile
+import unittest
+
+# test suite
+class TestAutojump(unittest.TestCase):
+
+ def setUp(self):
+ autojump.config(True)
+ self.fd, DB_FILE = tempfile.mkstemp()
+ self.db = autojump.Database(DB_FILE)
+ pass
+
+ def tearDown(self):
+ pass
+
+ def test_config(self):
+ self.assertEqual(autojump.COMPLETION_SEPARATOR, '__')
+
+ def test_db_add(self):
+ self.db.add('/1', 100)
+ self.assertEqual(self.db.get_weight('/1'), 100)
+ self.db.add('/2', 10)
+ self.assertEqual(self.db.get_weight('/2'), 10)
+ self.db.add('/2', 10)
+ self.assertEqual(self.db.get_weight('/2'), 14.142135623730951)
+
+ def test_db_get_weight(self):
+ self.assertEqual(self.db.get_weight('/'), 0)
+
+ def test_db_decay(self):
+ self.db.add('/1', 10)
+ self.db.decay()
+ self.assertTrue(self.db.get_weight('/1') < 10)
+
+ def test_db_load(self):
+ self.db = autojump.Database('tests/database.txt')
+ self.assertTrue(len(self.db) > 0)
+
+ def test_db_purge(self):
+ self.db.add('/1')
+ self.db.purge()
+ self.assertEquals(len(self.db), 0)
+
+ def test_db_save(self):
+ pass
+
+ def test_db_trim(self):
+ self.db.add('/1')
+ self.db.add('/2')
+ self.db.add('/3')
+ self.db.add('/4')
+ self.db.add('/5')
+ self.db.add('/6')
+ self.db.add('/7')
+ self.db.add('/8')
+ self.db.add('/9')
+ self.db.add('/10')
+ self.assertEquals(len(self.db), 10)
+ self.db.trim()
+ self.assertEquals(len(self.db), 9)
+
+ def test_db_decode(self):
+ #FIXME
+ self.assertEquals(autojump.decode('foo'), 'foo')
+
+ def test_db_unico(self):
+ #FIXME
+ self.assertEquals(autojump.unico('foo'), u'foo')
+
+ def test_match_normal(self):
+ max_matches = 1
+ self.db.add('/foo', 10)
+ self.db.add('/foo/bar', 20)
+
+ patterns = [u'']
+ results = autojump.find_matches(self.db, patterns, max_matches)
+ self.assertEquals(results[0], '/foo/bar')
+
+ patterns = [u'random']
+ results = autojump.find_matches(self.db, patterns, max_matches)
+ self.assertTrue(len(results) == 0)
+
+ patterns = [u'fo']
+ results = autojump.find_matches(self.db, patterns, max_matches)
+ self.assertEquals(results[0], '/foo')
+
+ self.db.add('/foo/bat', 15)
+ patterns = [u'ba']
+ results = autojump.find_matches(self.db, patterns, max_matches)
+ self.assertEquals(results[0], '/foo/bar')
+
+ self.db.add('/code/inbox', 5)
+ self.db.add('/home/user/inbox', 10)
+ patterns = [u'inbox']
+ results = autojump.find_matches(self.db, patterns, max_matches)
+ self.assertEquals(results[0], '/home/user/inbox')
+ patterns = [u'co', u'in']
+ results = autojump.find_matches(self.db, patterns, max_matches)
+ self.assertEquals(results[0], '/code/inbox')
+
+ def test_match_completion(self):
+ max_matches = 9
+ ignore_case = True
+ self.db.add('/1')
+ self.db.add('/2')
+ self.db.add('/3')
+ self.db.add('/4')
+ self.db.add('/5', 20)
+ self.db.add('/6', 15)
+ self.db.add('/7')
+ self.db.add('/8')
+ self.db.add('/9')
+
+ patterns = [u'']
+ results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
+ self.assertEquals(results, ['/5', '/6', '/9', '/8', '/7', '/4', '/3', '/2', '/1'])
+
+ def test_match_case_insensitive(self):
+ max_matches = 1
+ ignore_case = True
+ self.db.add('/FOO', 20)
+ self.db.add('/foo', 10)
+
+ patterns = [u'fo']
+ results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
+ self.assertEquals(results[0], '/FOO')
+
+ def test_match_fuzzy(self):
+ max_matches = 1
+ ignore_case = True
+ fuzzy_search = True
+ self.db.add('/foo', 10)
+ self.db.add('/foo/bar', 20)
+ self.db.add('/abcdefg', 10)
+
+ patterns = [u'random']
+ results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
+ self.assertTrue(len(results) == 0)
+
+ patterns = [u'abcdefg']
+ results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
+ self.assertEquals(results[0], '/abcdefg')
+
+ patterns = [u'abcefg']
+ results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
+ self.assertEquals(results[0], '/abcdefg')
+
+ patterns = [u'bacef']
+ results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
+ self.assertEquals(results[0], '/abcdefg')
+
+if __name__ == '__main__':
+ unittest.main()

0 comments on commit f25d7ba

Please sign in to comment.