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

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

#countcontent
1n/a'''Test percolator.py.'''
2n/afrom test.support import requires
3n/arequires('gui')
4n/a
5n/aimport unittest
6n/afrom tkinter import Text, Tk, END
7n/afrom idlelib.percolator import Percolator, Delegator
8n/a
9n/a
10n/aclass MyFilter(Delegator):
11n/a def __init__(self):
12n/a Delegator.__init__(self, None)
13n/a
14n/a def insert(self, *args):
15n/a self.insert_called_with = args
16n/a self.delegate.insert(*args)
17n/a
18n/a def delete(self, *args):
19n/a self.delete_called_with = args
20n/a self.delegate.delete(*args)
21n/a
22n/a def uppercase_insert(self, index, chars, tags=None):
23n/a chars = chars.upper()
24n/a self.delegate.insert(index, chars)
25n/a
26n/a def lowercase_insert(self, index, chars, tags=None):
27n/a chars = chars.lower()
28n/a self.delegate.insert(index, chars)
29n/a
30n/a def dont_insert(self, index, chars, tags=None):
31n/a pass
32n/a
33n/a
34n/aclass PercolatorTest(unittest.TestCase):
35n/a
36n/a @classmethod
37n/a def setUpClass(cls):
38n/a cls.root = Tk()
39n/a cls.text = Text(cls.root)
40n/a
41n/a @classmethod
42n/a def tearDownClass(cls):
43n/a del cls.text
44n/a cls.root.destroy()
45n/a del cls.root
46n/a
47n/a def setUp(self):
48n/a self.percolator = Percolator(self.text)
49n/a self.filter_one = MyFilter()
50n/a self.filter_two = MyFilter()
51n/a self.percolator.insertfilter(self.filter_one)
52n/a self.percolator.insertfilter(self.filter_two)
53n/a
54n/a def tearDown(self):
55n/a self.percolator.close()
56n/a self.text.delete('1.0', END)
57n/a
58n/a def test_insertfilter(self):
59n/a self.assertIsNotNone(self.filter_one.delegate)
60n/a self.assertEqual(self.percolator.top, self.filter_two)
61n/a self.assertEqual(self.filter_two.delegate, self.filter_one)
62n/a self.assertEqual(self.filter_one.delegate, self.percolator.bottom)
63n/a
64n/a def test_removefilter(self):
65n/a filter_three = MyFilter()
66n/a self.percolator.removefilter(self.filter_two)
67n/a self.assertEqual(self.percolator.top, self.filter_one)
68n/a self.assertIsNone(self.filter_two.delegate)
69n/a
70n/a filter_three = MyFilter()
71n/a self.percolator.insertfilter(self.filter_two)
72n/a self.percolator.insertfilter(filter_three)
73n/a self.percolator.removefilter(self.filter_one)
74n/a self.assertEqual(self.percolator.top, filter_three)
75n/a self.assertEqual(filter_three.delegate, self.filter_two)
76n/a self.assertEqual(self.filter_two.delegate, self.percolator.bottom)
77n/a self.assertIsNone(self.filter_one.delegate)
78n/a
79n/a def test_insert(self):
80n/a self.text.insert('insert', 'foo')
81n/a self.assertEqual(self.text.get('1.0', END), 'foo\n')
82n/a self.assertTupleEqual(self.filter_one.insert_called_with,
83n/a ('insert', 'foo', None))
84n/a
85n/a def test_modify_insert(self):
86n/a self.filter_one.insert = self.filter_one.uppercase_insert
87n/a self.text.insert('insert', 'bAr')
88n/a self.assertEqual(self.text.get('1.0', END), 'BAR\n')
89n/a
90n/a def test_modify_chain_insert(self):
91n/a filter_three = MyFilter()
92n/a self.percolator.insertfilter(filter_three)
93n/a self.filter_two.insert = self.filter_two.uppercase_insert
94n/a self.filter_one.insert = self.filter_one.lowercase_insert
95n/a self.text.insert('insert', 'BaR')
96n/a self.assertEqual(self.text.get('1.0', END), 'bar\n')
97n/a
98n/a def test_dont_insert(self):
99n/a self.filter_one.insert = self.filter_one.dont_insert
100n/a self.text.insert('insert', 'foo bar')
101n/a self.assertEqual(self.text.get('1.0', END), '\n')
102n/a self.filter_one.insert = self.filter_one.dont_insert
103n/a self.text.insert('insert', 'foo bar')
104n/a self.assertEqual(self.text.get('1.0', END), '\n')
105n/a
106n/a def test_without_filter(self):
107n/a self.text.insert('insert', 'hello')
108n/a self.assertEqual(self.text.get('1.0', 'end'), 'hello\n')
109n/a
110n/a def test_delete(self):
111n/a self.text.insert('insert', 'foo')
112n/a self.text.delete('1.0', '1.2')
113n/a self.assertEqual(self.text.get('1.0', END), 'o\n')
114n/a self.assertTupleEqual(self.filter_one.delete_called_with,
115n/a ('1.0', '1.2'))
116n/a
117n/aif __name__ == '__main__':
118n/a unittest.main(verbosity=2)