def test_get_selection(self): # text = Text(master=self.root) text = mockText() text.insert("1.0", "Hello World!") # fix text.index result when called in get_selection def sel(s): # select entire text, cursor irrelevant if s == "sel.first": return "1.0" if s == "sel.last": return "1.12" raise TclError text.index = sel # replaces .tag_add('sel', '1.0, '1.12') self.assertEqual(se.get_selection(text), ("1.0", "1.12")) def mark(s): # no selection, cursor after 'Hello' if s == "insert": return "1.5" raise TclError text.index = mark # replaces .mark_set('insert', '1.5') self.assertEqual(se.get_selection(text), ("1.5", "1.5"))
def test_search_reverse(self): Equal = self.assertEqual line = "Here is an 'is' test text." prog = re.compile('is') Equal(se.search_reverse(prog, line, len(line)).span(), (12, 14)) Equal(se.search_reverse(prog, line, 14).span(), (12, 14)) Equal(se.search_reverse(prog, line, 13).span(), (5, 7)) Equal(se.search_reverse(prog, line, 7).span(), (5, 7)) Equal(se.search_reverse(prog, line, 6), None)
def test_get(self): saved_Engine = se.SearchEngine se.SearchEngine = Mock try: root = Mock() engine = se.get(root) self.assertIsInstance(engine, se.SearchEngine) self.assertIs(root._searchengine, engine) self.assertIs(se.get(root), engine) finally: se.SearchEngine = saved_Engine
def test_get(self): saved_Engine = se.SearchEngine se.SearchEngine = Mock # monkey-patch class try: root = Mock() engine = se.get(root) self.assertIsInstance(engine, se.SearchEngine) self.assertIs(root._searchengine, engine) self.assertIs(se.get(root), engine) finally: se.SearchEngine = saved_Engine # restore class to module
def _setup(text): "Create or find the singleton SearchDialog instance." root = text._root() engine = SearchEngine.get(root) if not hasattr(engine, "_searchdialog"): engine._searchdialog = SearchDialog(root, engine) return engine._searchdialog
def do_replace(self): prog = self.engine.getprog() if not prog: return False text = self.text try: first = pos = text.index("sel.first") last = text.index("sel.last") except TclError: pos = None if not pos: first = last = pos = text.index("insert") line, col = SearchEngine.get_line_col(pos) chars = text.get("%d.0" % line, "%d.0" % (line + 1)) m = prog.match(chars, col) if not prog: return False new = self._replace_expand(m, self.replvar.get()) if new is None: return False text.mark_set("insert", first) text.undo_block_start() if m.group(): text.delete(first, last) if new: text.insert(first, new) text.undo_block_stop() self.show_hit(first, text.index("insert")) self.ok = 0 return True
def replace(text): root = text._root() engine = SearchEngine.get(root) if not hasattr(engine, "_replacedialog"): engine._replacedialog = ReplaceDialog(root, engine) dialog = engine._replacedialog dialog.open(text)
def grep(text, io=None, flist=None): root = text._root() engine = SearchEngine.get(root) if not hasattr(engine, "_grepdialog"): engine._grepdialog = GrepDialog(root, engine, flist) dialog = engine._grepdialog searchphrase = text.get("sel.first", "sel.last") dialog.open(text, searchphrase, io)
def grep(text, io=None, flist=None): root = text._root() engine = SearchEngine.get(root) if not hasattr(engine, '_grepdialog'): engine._grepdialog = GrepDialog(root, engine, flist) dialog = engine._grepdialog searchphrase = text.get('sel.first', 'sel.last') dialog.open(text, searchphrase, io)
def replace(text): """Returns a singleton ReplaceDialog instance.The single dialog saves user entries and preferences across instances.""" root = text._root() engine = SearchEngine.get(root) if not hasattr(engine, "_replacedialog"): engine._replacedialog = ReplaceDialog(root, engine) dialog = engine._replacedialog dialog.open(text)
def setUpClass(cls): cls.text = mockText() test_text = 'First line\nLine with target\nLast line\n' cls.text.insert('1.0', test_text) cls.pat = re.compile('target') cls.engine = se.SearchEngine(None) cls.engine.search_forward = lambda *args: ('f', args) cls.engine.search_backward = lambda *args: ('b', args) return
def test_get_selection(self): # text = Text(master=self.root) text = mockText() text.insert('1.0', 'Hello World!') # fix text.index result when called in get_selection def sel(s): # select entire text, cursor irrelevant if s == 'sel.first': return '1.0' if s == 'sel.last': return '1.12' raise TclError text.index = sel # replaces .tag_add('sel', '1.0, '1.12') self.assertEqual(se.get_selection(text), ('1.0', '1.12')) def mark(s): # no selection, cursor after 'Hello' if s == 'insert': return '1.5' raise TclError text.index = mark # replaces .mark_set('insert', '1.5') self.assertEqual(se.get_selection(text), ('1.5', '1.5'))
def setUpClass(cls): cls.root = Tk() cls.root.withdraw() se.tkMessageBox = Mbox cls.engine = se.SearchEngine(cls.root) cls.dialog = rd.ReplaceDialog(cls.root, cls.engine) cls.dialog.ok = Mock() cls.text = Text(cls.root) cls.text.undo_block_start = Mock() cls.text.undo_block_stop = Mock() cls.dialog.text = cls.text
def setUpClass(cls): cls.engine = se.SearchEngine(None) cls.text = mockText() cls.text.index = lambda index: '4.0' test_text = 'First line\nLine with target\nLast line\n' cls.text.insert('1.0', test_text) cls.pat = re.compile('target') cls.res = (2, (10, 16)) cls.failpat = re.compile('xyz') cls.emptypat = re.compile('\\w*') return
def test_get_selection(self): text = mockText() text.insert('1.0', 'Hello World!') def sel(s): if s == 'sel.first': return '1.0' if s == 'sel.last': return '1.12' raise TclError text.index = sel self.assertEqual(se.get_selection(text), ('1.0', '1.12')) def mark(s): if s == 'insert': return '1.5' raise TclError text.index = mark self.assertEqual(se.get_selection(text), ('1.5', '1.5'))
def setUpClass(cls): ## requires('gui') ## cls.root = Tk() ## cls.text = Text(master=cls.root) cls.text = mockText() test_text = ('First line\n' 'Line with target\n' 'Last line\n') cls.text.insert('1.0', test_text) cls.pat = re.compile('target') cls.engine = se.SearchEngine(None) cls.engine.search_forward = lambda *args: ('f', args) cls.engine.search_backward = lambda *args: ('b', args)
def test_get_selection(self): text = mockText() text.insert("1.0", "Hello World!") def sel(s): if s == "sel.first": return "1.0" if s == "sel.last": return "1.12" raise TclError text.index = sel self.assertEqual(se.get_selection(text), ("1.0", "1.12")) def mark(s): if s == "insert": return "1.5" raise TclError text.index = mark self.assertEqual(se.get_selection(text), ("1.5", "1.5"))
def setUpClass(cls): cls.engine = se.SearchEngine(None) ## requires('gui') ## cls.root = Tk() ## cls.text = Text(master=cls.root) cls.text = mockText() # search_backward calls index('end-1c') cls.text.index = lambda index: '4.0' test_text = ('First line\n' 'Line with target\n' 'Last line\n') cls.text.insert('1.0', test_text) cls.pat = re.compile('target') cls.res = (2, (10, 16)) # line, slice indexes of 'target' cls.failpat = re.compile('xyz') # not in text cls.emptypat = re.compile('\w*') # empty match possible
def _setup(text): root = text._root() engine = SearchEngine.get(root) if not hasattr(engine, "_searchdialog"): engine._searchdialog = SearchDialog(root, engine) return engine._searchdialog
def setUp(self): self.engine = se.SearchEngine(self.root) self.dialog = sd.SearchDialog(self.root, self.engine) self.text = tk.Text(self.root) self.text.insert('1.0', 'Hello World!')
def setUp(self): self.engine = se.SearchEngine(self.root) # None also seems to work self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine)
def test_get_line_col(self): self.assertEqual(se.get_line_col("1.0"), (1, 0)) self.assertEqual(se.get_line_col("1.11"), (1, 11)) self.assertRaises(ValueError, se.get_line_col, ("1.0 lineend")) self.assertRaises(ValueError, se.get_line_col, ("end"))
def _setup(text): root = text._root() engine = SearchEngine.get(root) if not hasattr(engine, '_searchdialog'): engine._searchdialog = SearchDialog(root, engine) return engine._searchdialog
def test_get_line_col(self): self.assertEqual(se.get_line_col('1.0'), (1, 0)) self.assertEqual(se.get_line_col('1.11'), (1, 11)) self.assertRaises(ValueError, se.get_line_col, '1.0 lineend') self.assertRaises(ValueError, se.get_line_col, 'end')
def test_get_line_col(self): self.assertEqual(se.get_line_col('1.0'), (1, 0)) self.assertEqual(se.get_line_col('1.11'), (1, 11)) self.assertRaises(ValueError, se.get_line_col, ('1.0 lineend')) self.assertRaises(ValueError, se.get_line_col, ('end'))
def setUp(self): self.engine = se.SearchEngine(root=None) return