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