From f25d7ba99647fdaa99cf1e3624d516a0d85307d6 Mon Sep 17 00:00:00 2001 From: William Ting Date: Sat, 12 May 2012 16:31:58 -1000 Subject: [PATCH] add unit tests, run using: make test --- Makefile | 5 +- tests/autojump.py | 1 + tests/database.txt | 1 + tests/runtests.py | 161 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 1 deletion(-) create mode 120000 tests/autojump.py create mode 100644 tests/database.txt create mode 100755 tests/runtests.py diff --git a/Makefile b/Makefile index 55f33d8..8e9c977 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/tests/autojump.py b/tests/autojump.py new file mode 120000 index 0000000..4581d70 --- /dev/null +++ b/tests/autojump.py @@ -0,0 +1 @@ +../bin/autojump \ No newline at end of file diff --git a/tests/database.txt b/tests/database.txt new file mode 100644 index 0000000..13238ea --- /dev/null +++ b/tests/database.txt @@ -0,0 +1 @@ +91.5647698216 /home/user/code/autojump diff --git a/tests/runtests.py b/tests/runtests.py new file mode 100755 index 0000000..10b7646 --- /dev/null +++ b/tests/runtests.py @@ -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()