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