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