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