Home | History | Annotate | Download | only in idle_test
      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         cls.auto_expand.bell = lambda: None
     26 
     27     @classmethod
     28     def tearDownClass(cls):
     29         del cls.text, cls.auto_expand
     30         if hasattr(cls, 'tk'):
     31             cls.tk.destroy()
     32             del cls.tk
     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):
     82         # Also add punctuation 'noise' that should be ignored.
     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 
    141 
    142 if __name__ == '__main__':
    143     unittest.main(verbosity=2)
    144