diff -r 0bb9346665e9 Lib/idlelib/IdleHistory.py --- a/Lib/idlelib/IdleHistory.py Tue Aug 13 19:51:04 2013 -0400 +++ b/Lib/idlelib/IdleHistory.py Tue Aug 13 22:33:52 2013 -0400 @@ -106,3 +106,9 @@ self.history.append(source) self.pointer = None self.prefix = None + +if __name__ == "__main__": + from test import support + support.use_resources = ['gui'] + from unittest import main + main('idlelib.idle_test.test_idlehistory', verbosity=2, exit=False) diff -r 0bb9346665e9 Lib/idlelib/idle_test/test_idlehistory.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/idlelib/idle_test/test_idlehistory.py Tue Aug 13 22:33:52 2013 -0400 @@ -0,0 +1,124 @@ +import unittest +from test.support import requires + +import tkinter as tk +from tkinter import Text as tkText +from idlelib.idle_test.mock_tk import Text as mkText +from idlelib.IdleHistory import History +from idlelib.configHandler import idleConf + +line1 = 'a = 7' +line2 = 'b = a' + +class StoreTest(unittest.TestCase): + '''Tests of __init__ and store, which can be done using mock Text''' + + @classmethod + def setUpClass(cls): + cls.text = mkText() + cls.history = History(cls.text) + + def tearDown(self): + self.text.delete('1.0', 'end') + self.history.history = [] + + def test_init(self): + self.assertIs(self.history.text, self.text) + self.assertEqual(self.history.history, []) + self.assertIsNone(self.history.prefix) + self.assertIsNone(self.history.pointer) + self.assertEqual(self.history.cyclic, + idleConf.GetOption("main", "History", "cyclic", 1, "bool")) + + def test_store_short(self): + self.history.store('a') + self.assertEqual(self.history.history, []) + self.history.store(' a ') + self.assertEqual(self.history.history, []) + + def test_store_dup(self): + self.history.store(line1) + self.assertEqual(self.history.history, [line1]) + self.history.store(line2) + self.assertEqual(self.history.history, [line1, line2]) + self.history.store(line1) + self.assertEqual(self.history.history, [line2, line1]) + + def test_store_reset(self): + self.history.prefix = line1 + self.history.pointer = 0 + self.history.store(line2) + self.assertIsNone(self.history.prefix) + self.assertIsNone(self.history.pointer) + + +class TextWrapper: + def __init__(self, master): + self.text = tkText(master=master) + self._bell = False + def __getattr__(self, name): + return getattr(self.text, name) + def bell(self): + self._bell = True + +class FetchTest(unittest.TestCase): + '''Tests of store and event methods, which currently needs wrapped tk.Text. + ''' + @classmethod + def setUpClass(cls): + requires('gui') + cls.root = tk.Tk() + cls.text = text = TextWrapper(cls.root) +# cls.text = text = tkText(cls.root) + text.insert('1.0', ">>> ") + text.mark_set('iomark', '1.4') + text.mark_gravity('iomark', 'left') + cls.history = History(text) + + @classmethod + def tearDownClass(cls): + cls.root.destroy() + + def fetch_test(self, reverse, line, prefix, index): + History = self.history + History.fetch(reverse) + Equal = self.assertEqual + Equal(self.text.get('iomark', 'end-1c'), line) + Equal(History.prefix, prefix) + Equal(History.pointer, index) + + def test_fetch_prev_cyclic(self): + prefix = '' + self.history.history = [line1, line2] + test = self.fetch_test + test(True, line2, prefix, 1) + test(True, line1, prefix, 0) + test(True, prefix, None, None) + + def test_fetch_next_cyclic(self): + prefix = '' + self.history.history = [line1, line2] + test = self.fetch_test + test(False, line1, prefix, 0) + test(False, line2, prefix, 1) + test(False, prefix, None, None) + + def test_fetch_prev_prefix(self): + prefix = 'a' + self.text.insert('iomark', prefix) + self.history.history = [line1, line2] + self.fetch_test(True, line1, prefix, 0) + self.fetch_test(True, prefix, None, None) + self.text.delete('iomark', 'end') + + def test_fetch_next_prefix(self): + prefix = 'a' + self.text.insert('iomark', prefix) + self.history.history = [line1, line2] + self.fetch_test(False, line1, prefix, 0) + self.fetch_test(False, prefix, None, None) + self.text.delete('iomark', 'end') + + +if __name__ == '__main__': + unittest.main(verbosity=2, exit=2)