1 """ !Changing this line will break Test_findfile.test_found! 2 Non-gui unit tests for idlelib.GrepDialog methods. 3 dummy_command calls grep_it calls findfiles. 4 An exception raised in one method will fail callers. 5 Otherwise, tests are mostly independent. 6 *** Currently only test grep_it. 7 """ 8 import unittest 9 from test.test_support import captured_stdout, findfile 10 from idlelib.idle_test.mock_tk import Var 11 from idlelib.GrepDialog import GrepDialog 12 import re 13 14 __file__ = findfile('idlelib/idle_test') + '/test_grep.py' 15 16 class Dummy_searchengine: 17 '''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the 18 passed in SearchEngine instance as attribute 'engine'. Only a few of the 19 many possible self.engine.x attributes are needed here. 20 ''' 21 def getpat(self): 22 return self._pat 23 24 searchengine = Dummy_searchengine() 25 26 class Dummy_grep: 27 # Methods tested 28 #default_command = GrepDialog.default_command 29 grep_it = GrepDialog.grep_it.im_func 30 findfiles = GrepDialog.findfiles.im_func 31 # Other stuff needed 32 recvar = Var(False) 33 engine = searchengine 34 def close(self): # gui method 35 pass 36 37 grep = Dummy_grep() 38 39 class FindfilesTest(unittest.TestCase): 40 # findfiles is really a function, not a method, could be iterator 41 # test that filename return filename 42 # test that idlelib has many .py files 43 # test that recursive flag adds idle_test .py files 44 pass 45 46 class Grep_itTest(unittest.TestCase): 47 # Test captured reports with 0 and some hits. 48 # Should test file names, but Windows reports have mixed / and \ separators 49 # from incomplete replacement, so 'later'. 50 51 def report(self, pat): 52 grep.engine._pat = pat 53 with captured_stdout() as s: 54 grep.grep_it(re.compile(pat), __file__) 55 lines = s.getvalue().split('\n') 56 lines.pop() # remove bogus '' after last \n 57 return lines 58 59 def test_unfound(self): 60 pat = 'xyz*'*7 61 lines = self.report(pat) 62 self.assertEqual(len(lines), 2) 63 self.assertIn(pat, lines[0]) 64 self.assertEqual(lines[1], 'No hits.') 65 66 def test_found(self): 67 68 pat = '""" !Changing this line will break Test_findfile.test_found!' 69 lines = self.report(pat) 70 self.assertEqual(len(lines), 5) 71 self.assertIn(pat, lines[0]) 72 self.assertIn('py: 1:', lines[1]) # line number 1 73 self.assertIn('2', lines[3]) # hits found 2 74 self.assertTrue(lines[4].startswith('(Hint:')) 75 76 class Default_commandTest(unittest.TestCase): 77 # To write this, mode OutputWindow import to top of GrepDialog 78 # so it can be replaced by captured_stdout in class setup/teardown. 79 pass 80 81 if __name__ == '__main__': 82 unittest.main(verbosity=2, exit=False) 83