Coverage for idle_test/test_grep.py: 98%
95 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
1""" !Changing this line will break Test_findfile.test_found!
2Non-gui unit tests for grep.GrepDialog methods.
3dummy_command calls grep_it calls findfiles.
4An exception raised in one method will fail callers.
5Otherwise, tests are mostly independent.
6Currently only test grep_it, coverage 51%.
7"""
8from idlelib import grep
9import unittest
10from test.support import captured_stdout
11from idlelib.idle_test.mock_tk import Var
12import os
13import re
16class 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 1cd
24searchengine = Dummy_searchengine()
27class Dummy_grep:
28 # Methods tested
29 #default_command = GrepDialog.default_command
30 grep_it = grep.GrepDialog.grep_it
31 # Other stuff needed
32 recvar = Var(False)
33 engine = searchengine
34 def close(self): # gui method
35 pass 1cd
37_grep = Dummy_grep()
40class FindfilesTest(unittest.TestCase):
42 @classmethod
43 def setUpClass(cls):
44 cls.realpath = os.path.realpath(__file__)
45 cls.path = os.path.dirname(cls.realpath)
47 @classmethod
48 def tearDownClass(cls):
49 del cls.realpath, cls.path
51 def test_invaliddir(self):
52 with captured_stdout() as s: 1g
53 filelist = list(grep.findfiles('invaliddir', '*.*', False)) 1g
54 self.assertEqual(filelist, []) 1g
55 self.assertIn('invalid', s.getvalue()) 1g
57 def test_curdir(self):
58 # Test os.curdir.
59 ff = grep.findfiles 1f
60 save_cwd = os.getcwd() 1f
61 os.chdir(self.path) 1f
62 filename = 'test_grep.py' 1f
63 filelist = list(ff(os.curdir, filename, False)) 1f
64 self.assertIn(os.path.join(os.curdir, filename), filelist) 1f
65 os.chdir(save_cwd) 1f
67 def test_base(self):
68 ff = grep.findfiles 1e
69 readme = os.path.join(self.path, 'README.txt') 1e
71 # Check for Python files in path where this file lives.
72 filelist = list(ff(self.path, '*.py', False)) 1e
73 # This directory has many Python files.
74 self.assertGreater(len(filelist), 10) 1e
75 self.assertIn(self.realpath, filelist) 1e
76 self.assertNotIn(readme, filelist) 1e
78 # Look for .txt files in path where this file lives.
79 filelist = list(ff(self.path, '*.txt', False)) 1e
80 self.assertNotEqual(len(filelist), 0) 1e
81 self.assertNotIn(self.realpath, filelist) 1e
82 self.assertIn(readme, filelist) 1e
84 # Look for non-matching pattern.
85 filelist = list(ff(self.path, 'grep.*', False)) 1e
86 self.assertEqual(len(filelist), 0) 1e
87 self.assertNotIn(self.realpath, filelist) 1e
89 def test_recurse(self):
90 ff = grep.findfiles 1b
91 parent = os.path.dirname(self.path) 1b
92 grepfile = os.path.join(parent, 'grep.py') 1b
93 pat = '*.py' 1b
95 # Get Python files only in parent directory.
96 filelist = list(ff(parent, pat, False)) 1b
97 parent_size = len(filelist) 1b
98 # Lots of Python files in idlelib.
99 self.assertGreater(parent_size, 20) 1b
100 self.assertIn(grepfile, filelist) 1b
101 # Without subdirectories, this file isn't returned.
102 self.assertNotIn(self.realpath, filelist) 1b
104 # Include subdirectories.
105 filelist = list(ff(parent, pat, True)) 1b
106 # More files found now.
107 self.assertGreater(len(filelist), parent_size) 1b
108 self.assertIn(grepfile, filelist) 1b
109 # This file exists in list now.
110 self.assertIn(self.realpath, filelist) 1b
112 # Check another level up the tree.
113 parent = os.path.dirname(parent) 1b
114 filelist = list(ff(parent, '*.py', True)) 1b
115 self.assertIn(self.realpath, filelist) 1b
118class Grep_itTest(unittest.TestCase):
119 # Test captured reports with 0 and some hits.
120 # Should test file names, but Windows reports have mixed / and \ separators
121 # from incomplete replacement, so 'later'.
123 def report(self, pat):
124 _grep.engine._pat = pat 1cd
125 with captured_stdout() as s: 1cd
126 _grep.grep_it(re.compile(pat), __file__) 1cd
127 lines = s.getvalue().split('\n') 1cd
128 lines.pop() # remove bogus '' after last \n 1cd
129 return lines 1cd
131 def test_unfound(self):
132 pat = 'xyz*'*7 1d
133 lines = self.report(pat) 1d
134 self.assertEqual(len(lines), 2) 1d
135 self.assertIn(pat, lines[0]) 1d
136 self.assertEqual(lines[1], 'No hits.') 1d
138 def test_found(self):
140 pat = '""" !Changing this line will break Test_findfile.test_found!' 1c
141 lines = self.report(pat) 1c
142 self.assertEqual(len(lines), 5) 1c
143 self.assertIn(pat, lines[0]) 1c
144 self.assertIn('py: 1:', lines[1]) # line number 1 1c
145 self.assertIn('2', lines[3]) # hits found 2 1c
146 self.assertTrue(lines[4].startswith('(Hint:')) 1c
149class Default_commandTest(unittest.TestCase):
150 # To write this, move outwin import to top of GrepDialog
151 # so it can be replaced by captured_stdout in class setup/teardown.
152 pass
155if __name__ == '__main__': 155 ↛ 156line 155 didn't jump to line 156, because the condition on line 155 was never true
156 unittest.main(verbosity=2)