diff -r e7ebf94629d2 Lib/idlelib/AutoExpand.py --- a/Lib/idlelib/AutoExpand.py Mon Jun 02 00:30:48 2014 +0100 +++ b/Lib/idlelib/AutoExpand.py Mon Jun 02 19:45:08 2014 +0530 @@ -1,3 +1,8 @@ +"""Use AutoExpand to automatically complete the current word without +having to type the entire word. +AutoExpand searches for possible candidates in the text widget. +If there is more than one, it cycles through all of them. +""" import string import re @@ -81,3 +86,9 @@ while i > 0 and line[i-1] in self.wordchars: i = i-1 return line[i:] + +if __name__ == '__main__': + from test import support; support.use_resources = ['gui'] + import unittest + unittest.main('idlelib.idle_test.test_auto_expand', + verbosity=2, exit=False) diff -r e7ebf94629d2 Lib/idlelib/idle_test/test_auto_expand.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/idlelib/idle_test/test_auto_expand.py Mon Jun 02 19:45:08 2014 +0530 @@ -0,0 +1,134 @@ +"""Unit tests for idlelib.AutoExpand""" +import unittest +#from idlelib.idle_test.mock_tk import Text +from test.support import requires +from tkinter import Text +from idlelib.AutoExpand import AutoExpand + +Expand = AutoExpand + + +class Dummy_Editwin: + """ + Mock editor window class + """ + + def __init__(self, text): + self.text = text + + +class AutoExpandTest(unittest.TestCase): + + @classmethod + def setUpClass(cls): + requires('gui') + cls.text = Text() + cls.editwin = Dummy_Editwin(cls.text) + cls.auto_expand = Expand(cls.editwin) + + @classmethod + def tearDownClass(cls): + cls.text.destroy() + del cls.text + + def tearDown(self): + self.text.delete('1.0', 'end') + + def test_get_prevword(self): + text = self.editwin.text + previous = self.auto_expand.getprevword + equal = self.assertEqual + + equal(previous(), '') + + text.insert('insert', 't') + equal(previous(), 't') + + text.insert('insert', 'his') + equal(previous(), 'this') + + text.insert('insert', ' ') + equal(previous(), '') + + text.insert('insert', 'is') + equal(previous(), 'is') + + text.insert('insert', '\nsample\nstring') + equal(previous(), 'string') + + text.delete('3.0', 'insert') + equal(previous(), '') + + text.delete('1.0', 'end') + equal(previous(), '') + + def test_get_words(self): + text = self.editwin.text + words = self.auto_expand.getwords + equal = self.assertCountEqual + + def delete_all(): + text.delete('1.0', 'end') + + equal(words(), []) + + text.insert('insert', 'a ab') + equal(words(), []) + delete_all() + + text.insert('insert', 'ab a') + equal(words(), ['ab', 'a']) + delete_all() + + text.insert('insert', 'aab aba\n acb\n') + text.insert('insert', 'a') + equal(words(), ['aab', 'aba', 'acb', 'a']) + text.insert('insert', 'a') + equal(words(), ['aab', 'aa']) + text.insert('insert', 'b') + equal(words(), []) + delete_all() + + text.insert('insert', 'a\nb\nc\nd') + text.insert('insert', 'abc') + equal(words(), []) + + def test_expand_event(self): + text = self.editwin.text + expand = self.auto_expand.expand_word_event + previous = self.auto_expand.getprevword + equal = self.assertEqual + + expand('event') + equal(previous(), '') + + text.insert('insert', 'abc a') + expand('event') + equal(previous(), 'abc') + + text.insert('insert', 'b ac ad a') + expand('event') + equal(previous(), 'ad') + expand('event') + equal(previous(), 'ac') + + text.insert('insert', '\na') + text.insert('insert', ' eab') + expand('event') + equal(previous(), 'eab') + + text.delete('1.0', 'end') + + text.insert('insert', 'a\n \nab abc abcd \na') + expand('event') + equal(previous(), 'abcd') + expand('event') + equal(previous(), 'abc') + expand('event') + equal(previous(), 'ab') + expand('event') + equal(previous(), 'a') + + +if __name__ == '__main__': + unittest.main(verbosity=2, exit=2)