• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Unit tests for idlelib.AutoExpand"""
2import unittest
3from test.test_support import requires
4from Tkinter import Text, Tk
5#from idlelib.idle_test.mock_tk import Text
6from idlelib.AutoExpand import AutoExpand
7
8
9class Dummy_Editwin:
10    # AutoExpand.__init__ only needs .text
11    def __init__(self, text):
12        self.text = text
13
14class AutoExpandTest(unittest.TestCase):
15
16    @classmethod
17    def setUpClass(cls):
18        if 'Tkinter' in str(Text):
19            requires('gui')
20            cls.tk = Tk()
21            cls.text = Text(cls.tk)
22        else:
23            cls.text = Text()
24        cls.auto_expand = AutoExpand(Dummy_Editwin(cls.text))
25
26    @classmethod
27    def tearDownClass(cls):
28        del cls.text, cls.auto_expand
29        if hasattr(cls, 'tk'):
30            cls.tk.destroy()
31            del cls.tk
32
33    def tearDown(self):
34        self.text.delete('1.0', 'end')
35
36    def test_get_prevword(self):
37        text = self.text
38        previous = self.auto_expand.getprevword
39        equal = self.assertEqual
40
41        equal(previous(), '')
42
43        text.insert('insert', 't')
44        equal(previous(), 't')
45
46        text.insert('insert', 'his')
47        equal(previous(), 'this')
48
49        text.insert('insert', ' ')
50        equal(previous(), '')
51
52        text.insert('insert', 'is')
53        equal(previous(), 'is')
54
55        text.insert('insert', '\nsample\nstring')
56        equal(previous(), 'string')
57
58        text.delete('3.0', 'insert')
59        equal(previous(), '')
60
61        text.delete('1.0', 'end')
62        equal(previous(), '')
63
64    def test_before_only(self):
65        previous = self.auto_expand.getprevword
66        expand = self.auto_expand.expand_word_event
67        equal = self.assertEqual
68
69        self.text.insert('insert', 'ab ac bx ad ab a')
70        equal(self.auto_expand.getwords(), ['ab', 'ad', 'ac', 'a'])
71        expand('event')
72        equal(previous(), 'ab')
73        expand('event')
74        equal(previous(), 'ad')
75        expand('event')
76        equal(previous(), 'ac')
77        expand('event')
78        equal(previous(), 'a')
79
80    def test_after_only(self):
81        # Also add punctuation 'noise' that shoud be ignored.
82        text = self.text
83        previous = self.auto_expand.getprevword
84        expand = self.auto_expand.expand_word_event
85        equal = self.assertEqual
86
87        text.insert('insert', 'a, [ab] ac: () bx"" cd ac= ad ya')
88        text.mark_set('insert', '1.1')
89        equal(self.auto_expand.getwords(), ['ab', 'ac', 'ad', 'a'])
90        expand('event')
91        equal(previous(), 'ab')
92        expand('event')
93        equal(previous(), 'ac')
94        expand('event')
95        equal(previous(), 'ad')
96        expand('event')
97        equal(previous(), 'a')
98
99    def test_both_before_after(self):
100        text = self.text
101        previous = self.auto_expand.getprevword
102        expand = self.auto_expand.expand_word_event
103        equal = self.assertEqual
104
105        text.insert('insert', 'ab xy yz\n')
106        text.insert('insert', 'a ac by ac')
107
108        text.mark_set('insert', '2.1')
109        equal(self.auto_expand.getwords(), ['ab', 'ac', 'a'])
110        expand('event')
111        equal(previous(), 'ab')
112        expand('event')
113        equal(previous(), 'ac')
114        expand('event')
115        equal(previous(), 'a')
116
117    def test_other_expand_cases(self):
118        text = self.text
119        expand = self.auto_expand.expand_word_event
120        equal = self.assertEqual
121
122        # no expansion candidate found
123        equal(self.auto_expand.getwords(), [])
124        equal(expand('event'), 'break')
125
126        text.insert('insert', 'bx cy dz a')
127        equal(self.auto_expand.getwords(), [])
128
129        # reset state by successfully expanding once
130        # move cursor to another position and expand again
131        text.insert('insert', 'ac xy a ac ad a')
132        text.mark_set('insert', '1.7')
133        expand('event')
134        initial_state = self.auto_expand.state
135        text.mark_set('insert', '1.end')
136        expand('event')
137        new_state = self.auto_expand.state
138        self.assertNotEqual(initial_state, new_state)
139
140if __name__ == '__main__':
141    unittest.main(verbosity=2)
142