Best Python code snippet using lettuce-tools_python
test_autoexpand.py
Source:test_autoexpand.py
1"Test autoexpand, coverage 100%."2from idlelib.autoexpand import AutoExpand3import unittest4from test.support import requires5from tkinter import Text, Tk6class DummyEditwin:7 # AutoExpand.__init__ only needs .text8 def __init__(self, text):9 self.text = text10class AutoExpandTest(unittest.TestCase):11 @classmethod12 def setUpClass(cls):13 requires('gui')14 cls.tk = Tk()15 cls.text = Text(cls.tk)16 cls.auto_expand = AutoExpand(DummyEditwin(cls.text))17 cls.auto_expand.bell = lambda: None18# If mock_tk.Text._decode understood indexes 'insert' with suffixed 'linestart',19# 'wordstart', and 'lineend', used by autoexpand, we could use the following20# to run these test on non-gui machines (but check bell).21## try:22## requires('gui')23## #raise ResourceDenied() # Uncomment to test mock.24## except ResourceDenied:25## from idlelib.idle_test.mock_tk import Text26## cls.text = Text()27## cls.text.bell = lambda: None28## else:29## from tkinter import Tk, Text30## cls.tk = Tk()31## cls.text = Text(cls.tk)32 @classmethod33 def tearDownClass(cls):34 del cls.text, cls.auto_expand35 if hasattr(cls, 'tk'):36 cls.tk.destroy()37 del cls.tk38 def tearDown(self):39 self.text.delete('1.0', 'end')40 def test_get_prevword(self):41 text = self.text42 previous = self.auto_expand.getprevword43 equal = self.assertEqual44 equal(previous(), '')45 text.insert('insert', 't')46 equal(previous(), 't')47 text.insert('insert', 'his')48 equal(previous(), 'this')49 text.insert('insert', ' ')50 equal(previous(), '')51 text.insert('insert', 'is')52 equal(previous(), 'is')53 text.insert('insert', '\nsample\nstring')54 equal(previous(), 'string')55 text.delete('3.0', 'insert')56 equal(previous(), '')57 text.delete('1.0', 'end')58 equal(previous(), '')59 def test_before_only(self):60 previous = self.auto_expand.getprevword61 expand = self.auto_expand.expand_word_event62 equal = self.assertEqual63 self.text.insert('insert', 'ab ac bx ad ab a')64 equal(self.auto_expand.getwords(), ['ab', 'ad', 'ac', 'a'])65 expand('event')66 equal(previous(), 'ab')67 expand('event')68 equal(previous(), 'ad')69 expand('event')70 equal(previous(), 'ac')71 expand('event')72 equal(previous(), 'a')73 def test_after_only(self):74 # Also add punctuation 'noise' that should be ignored.75 text = self.text76 previous = self.auto_expand.getprevword77 expand = self.auto_expand.expand_word_event78 equal = self.assertEqual79 text.insert('insert', 'a, [ab] ac: () bx"" cd ac= ad ya')80 text.mark_set('insert', '1.1')81 equal(self.auto_expand.getwords(), ['ab', 'ac', 'ad', 'a'])82 expand('event')83 equal(previous(), 'ab')84 expand('event')85 equal(previous(), 'ac')86 expand('event')87 equal(previous(), 'ad')88 expand('event')89 equal(previous(), 'a')90 def test_both_before_after(self):91 text = self.text92 previous = self.auto_expand.getprevword93 expand = self.auto_expand.expand_word_event94 equal = self.assertEqual95 text.insert('insert', 'ab xy yz\n')96 text.insert('insert', 'a ac by ac')97 text.mark_set('insert', '2.1')98 equal(self.auto_expand.getwords(), ['ab', 'ac', 'a'])99 expand('event')100 equal(previous(), 'ab')101 expand('event')102 equal(previous(), 'ac')103 expand('event')104 equal(previous(), 'a')105 def test_other_expand_cases(self):106 text = self.text107 expand = self.auto_expand.expand_word_event108 equal = self.assertEqual109 # no expansion candidate found110 equal(self.auto_expand.getwords(), [])111 equal(expand('event'), 'break')112 text.insert('insert', 'bx cy dz a')113 equal(self.auto_expand.getwords(), [])114 # reset state by successfully expanding once115 # move cursor to another position and expand again116 text.insert('insert', 'ac xy a ac ad a')117 text.mark_set('insert', '1.7')118 expand('event')119 initial_state = self.auto_expand.state120 text.mark_set('insert', '1.end')121 expand('event')122 new_state = self.auto_expand.state123 self.assertNotEqual(initial_state, new_state)124if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!