ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_autoexpand.py

Python code coverage for Lib/idlelib/idle_test/test_autoexpand.py

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