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